Reputation: 703
My intent is to build a dataset in a two step process based on 'frame' dataset using call execute. I need 'a_dataset'. It doesn't exist. I read the row of 'frame' dataset: in the first row since 'a_dataset' doesn't exist i do macro nexds. Second row of 'frame' , i check if 'a_dataset' exist, i found it and i do macro exds.
Unfortunately my code is not working. For each row in 'frame' the condition of existence is always false and it runs two times the nexds macro.
DATA WORK.frame;
INFILE DATALINES4
/*DLM='7F'x*/
DLM=' '
MISSOVER
DSD ;
INPUT
from : $CHAR25.
tojoin : $CHAR7. ;
DATALINES4;
dataset join1
dataset join2
;;;;
proc delete data=a_dataset;run;
%macro exds(dsn,varname);
data &dsn.;
set &dsn.;
&varname. = 'exist';
run;
%mend;
%macro nexds(dsn,varname);
data &dsn.;
&varname. = 'notexist';
run;
%mend;
data _null_;
set frame;
name = strip(tojoin);
dsname = cat('a_',strip(from));
if ~exist(dsname) then put 'notexist';
else put 'exist';
if ~exist(dsname) then call execute('%nexds('||dsname||','||name||')');
else call execute('%exds('||dsname||','||name||')');
run;
It runs this two lines of code:
1 + data a_dataset; join1 = 'notexist'; run;
2 + data a_dataset; join2 = 'notexist'; run;
instead i want:
1 + data a_dataset; join1 = 'notexist'; run;
2 + data a_dataset; set a_dataset; join2 = 'exist'; run;
in the log at the put call :
notexist
notexist
it seems like the if condition is checked at the beginning for each row of 'frame' and not after each it read a single row of 'frame'.
Upvotes: 0
Views: 325
Reputation: 51566
You are not understanding how CALL EXECUTE() works. The code you generate is stored up to run after the current step finishes.
Since both observations in FRAME have the same value of FROM the test for whether or not the dataset exists will have the same value both times since any code generated by the first observation has not had a chance to run yet.
Move the test and branching logic into the macro instead.
DATA frame;
INPUT from :$25. tojoin :$7. ;
DATALINES4;
dataset join1
dataset join2
;;;;
%macro make(dsn,varname);
data &dsn ;
%if %sysfunc(exist(&dsn)) %then %do;
set &dsn;
&varname = 'exist';
%end;
%else %do;
&varname = 'notexist';
%end;
run;
%mend make;
proc delete data=a_dataset;run;
options mprint;
data _null_;
set frame;
call execute(cats('%nrstr(%make)(', 'a_', from, ',' , tojoin, ')' ));
run;
Upvotes: 1