Hello,
One can not invoke a macro using Call execute in oder to handle a created global macro var by call sypmut.
How is that ? Any minds is welcome.
Regards
I think the documentation explains how to do that
Post your code.
This works:
%macro mymac(varname);
%put Variable as received by macro: &&&varname;
%mend;
data _null_;
call symputx('myvar','mytext','g');
call execute('%mymac(myvar)');
run;
Log from that:
24 %macro mymac(varname); 25 %put Variable as received by macro: &&&varname; 26 %mend; 27 28 data _null_; 29 call symputx('myvar','mytext','g'); 30 call execute('%mymac(myvar)'); 31 run; Variable as received by macro: mytext NOTE: DATA statement used (Total process time): real time 0.00 seconds cpu time 0.00 seconds
Look at this example: val_m2 values are not updated form call execute.
Regards
data have;
do i=1 to 10;
have= i+1;
output;
end;
run;
%macro mymacro(nc);
%Global used mg1 mg2;
%let mg1= 20; %let mg2= 40;
data need; set have nobs= nobs; J= _n_;
var0= J * &mg1 / &mg2;
var1= var0 + 0.5 * &mg1 / &mg2;
var2= var0 + var1;
nexp= var2 * &mg1 / &mg2;
need + (((nexp - have)**2) / nexp); /*cumulative sum*/
if J= nobs then call symput('used',need);
run;
%mend;
data see;
do i= 1 to 3;
if i= 1 then do;
max= 40; min= 15; m1= 27.5;
val_m1= 2; m2= 33.75;
call execute('%mymacro('||m2||')');
val_m2= input(symget('used'), 30.); /*updating*/
end;
else do;
min= m1; m1= m2; val_m1= val_m2; m2= (m2 + max)/2; /*updating*/
call execute('%mymacro('||m2||')');
val_m2 = input(symget('used'), 30.);
end;
output;
end;
run;
The data step in the macro can only be compiled and run after the data step with the call execute has finished. Macro code in a macro that is call executed, OTOH, will be resolved immediately when pushed into the SAS interpreter queue.
If you want to use data from a second dataset dynamically, but in a way that can not be handled by a join, consider using a hash object.
Since you only use an overall statistic from dataset have, I'd prepare that beforehand. No need to reread the whole dataset in every iteration.
Thanks,
An overall statistics is not used. When the macro execute, it use the current variable have value.
Also, the macro executes with the last m2 value as argument.
Since dataset 'have' never changes, and only the results of the final iteration of the data step are put into the macro variable, and the code in the macro is not dynamic at all, there is no need to call the macro at all. Just set &need once.
If you plan on doing something completely different, show THAT code.
Where you use these two lines in the data step:
call execute('%mymacro('||m2||')'); <= creates a call to the macro that executes AFTER the data step
val_m2= input(symget('used'), 30.); <= so the value of USED is not changed prior to this line. It will be the same value for every iteration
Do you intentionally through away and decimal part of used?
No i don't thinnk so.
Your explanations are so clear, thanks for that.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.