How to use call execute when parameters are global variables

Accepted Solution Solved
Reply
Contributor
Posts: 30
Accepted Solution

How to use call execute when parameters are global variables

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: 

 

image.png


Accepted Solutions
Solution
Friday
Super User
Posts: 13,500

Re: How to use call execute when parameters are global variables

Posted in reply to UdayGuntupalli

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(&param1.)');
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.

View solution in original post


All Replies
Solution
Friday
Super User
Posts: 13,500

Re: How to use call execute when parameters are global variables

Posted in reply to UdayGuntupalli

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(&param1.)');
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.

Super User
Super User
Posts: 8,070

Re: How to use call execute when parameters are global variables

[ Edited ]
Posted in reply to UdayGuntupalli

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)('||"&param1,&param2,&param3,&param4"||')' )
  ;
  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(&param1,&param2,&param3,&param4)
%end; 
☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 2 replies
  • 81 views
  • 1 like
  • 3 in conversation