BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Patrick
Opal | Level 21

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? 

Capture.JPG

 


%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;
1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20

 Sir  @Patrick  delaying should help

 

data _null_;
  call execute('%nrstr(%sample(call execute))');
  stop;
run;

View solution in original post

4 REPLIES 4
novinosrin
Tourmaline | Level 20

 Sir  @Patrick  delaying should help

 

data _null_;
  call execute('%nrstr(%sample(call execute))');
  stop;
run;
Patrick
Opal | Level 21

@novinosrin 

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....

http://support.sas.com/documentation/cdl/en/mcrolref/62978/HTML/default/viewer.htm#n1q1527d51eivsn1o...

 

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. 

novinosrin
Tourmaline | Level 20

Oh i'm sorry

Tom
Super User Tom
Super User

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)',')'));

 

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 1656 views
  • 0 likes
  • 3 in conversation