Hi all,
I can't get my head around why calling a macro directly or via call execute() returns different results for below sample code.
It's clearly a timing issue but I don't get why this is happening. What am I missing?
%macro sample(method);
%local mvar;
%let mvar=0;
data _null_;
call symputx('mvar','1','l');
stop;
run;
data sample;
length method $15;
method="&method";
var=symget("mvar");
mvar="&mvar";
run;
proc append base=collect data=sample;
run;
%mend;
%sample(direct)
data _null_;
rc=dosubl('%sample(dosubl)');
stop;
run;
data _null_;
call execute('%sample(call execute)');
stop;
run;
proc print data=collect;
run;
proc delete data=collect;
run;
Sir @Patrick delaying should help
data _null_;
call execute('%nrstr(%sample(call execute))');
stop;
run;
Sir @Patrick delaying should help
data _null_;
call execute('%nrstr(%sample(call execute))');
stop;
run;
Initially I wanted to reply to you that I know how to code around it but that I don't understand what's happening. ...and then I've consulted the SAS documentation and actually the "why" is fully explained there - RTM....
Hmm... I guess I have to become more careful using call execute() for macro calls when I just want these calls to behave the same as if I'd call the macro directly.
Oh i'm sorry
The tip in that document, and the answer posted, are slightly off in my opinion.
Tip -The following example uses the %NRSTR macro quoting function to mask the macro statement. This function will delay the execution of macro statements until after a step boundary.call execute('%nrstr(%sales('||month||'))');
The reason is that the I find that you just need to enclose the macro name in the %NRSTR() function call. Mainly because I find it much easier to read and type correctly.
call execute(cats('%nrstr(%sales)(',month,')'));
Note that there might still be some rare situations where you need to enclose some of the parameters inside of %NRSTR(), for example if you are referencing a macro variable that will be calculated by other code you have pushed with CALL EXECUTE. But even then I would prefer to quote those independently to make it clearer.
call execute(cats('%nrstr(%mymacro)(month=',month,',previous=%nrstr(&result)',')'));
Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!
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.
Ready to level-up your skills? Choose your own adventure.