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

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

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

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

2 REPLIES 2
ballardw
Super User

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.

Tom
Super User Tom
Super User

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; 

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 2 replies
  • 511 views
  • 3 likes
  • 3 in conversation