All,
Wondering what is the correct way to call a SAS Macro using the call execute method when it has global variables as parameters.
The following is throwing some errors as shown below:
Code:
Data _null_;
If &Flag^= 1 Then
call execute('%nrstr(%MyMacro('||&Param1||','||&Param2||','||&Param3||','||&Param4||'))');
Else
put "**l Data Is Turned On" ;
Run;
Errors:
See if this gives some ideas:
%let param1= Sometext; %macro dummy(parm); %put Passed Parm value in macro Dummy was &parm.; %mend; data _null_; call execute('%dummy(¶m1.)'); run;
With the global macro variable inside the single quote it does not attempt to resolve until the call execute stack starts executing.
This specific code generates a note that Call Execute routine execute successfully but no SAS statements were generated because this is a very trivial macro with only %put, which does not generate any statements. About as bare bones as was possible demonstrate one potential route to a solution.
See if this gives some ideas:
%let param1= Sometext; %macro dummy(parm); %put Passed Parm value in macro Dummy was &parm.; %mend; data _null_; call execute('%dummy(¶m1.)'); run;
With the global macro variable inside the single quote it does not attempt to resolve until the call execute stack starts executing.
This specific code generates a note that Call Execute routine execute successfully but no SAS statements were generated because this is a very trivial macro with only %put, which does not generate any statements. About as bare bones as was possible demonstrate one potential route to a solution.
The error message is showing your mistake and it is really unrelated to CALL EXECUTE. Just to how macro variable references themselves work.
If you set a macro variable to string of characters longer than 32 characters.
%let x=this_is_too_long_to_be_a_valid_sas_variable_name;
And then try to expand that macro variable in the middle of a SAS statement.
data _null_;
x=&x ;
run;
Then you have tried to run this SAS code.
data _null_;
x=this_is_too_long_to_be_a_valid_sas_variable_name;
run;
Which is clearly invalid.
If the value you want to pass is based on macro variables instead of data set variables then make sure to enclose them in quotes.
data _null_;
if &Flag^= 1 then
call execute('%nrstr(%mymacro)('||"¶m1,¶m2,¶m3,¶m4"||')' )
;
else put "**l Data Is Turned On" ;
run;
If you use double quotes then the values of the macro variables will be expanded when the data step is compiled. If you use single quotes then they will be expanded when the code is pushed onto the stack by call execute. If you use single quotes and also wait to close the %NRSTR() macro function call until after the macro parameters then the macro variables will be expanded when the call is pull off of the stack to run after the data step.
Of course if you are running the latest version of SAS (9.4M5) then you can eliminate the data step.
%if (&flag = 1) %then %do;
%put "**l Data Is Turned On" ;
%end;
%else %do;
%mymacro(¶m1,¶m2,¶m3,¶m4)
%end;
Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.
Register today!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.
Select SAS Training centers are offering in-person courses. View upcoming courses for: