Macro variable created using call symput is not resoved in the data step where it was created? What is mechanism for this? While it resolves when used in call execute.
data _null_; call symputx('macvar','value'); %put &macvar; run;
data _null_; call symputx('macvar','value'); call execute('%put &macvar'); run;
CALL SYMPUT AND EXECUTE
The CALL SYMPUT allows the data step to add a new macro variable or overwrite an existing one to the macro space. This action is not performed until the data step is completed its processing. So it is a common knowledge that the macro variable created by CALL SYMPUT cannot be referenced inside the creating data step and you have to use another step outside this data step if you want to check the value of this macro variable or to use it. But CALL EXECUTE gives you the flexibility to reference the macro variable inside the data step used to create it.
Read more thist in CALL SYMPUT Routine doc. here: Problem Trying to Reference a SYMPUT-Assigned Value Before It Is Available
So what will happen when you run your first example for the first time, you get warning like:
WARNING: Apparent symbolic reference MACVAR not resolved.
Then if you run your example again you will not get this warning again, as from your first run the data step is completed and all statements that are stacked for later execution are exected.
But if you used CALL EXECUTE this will not happen, because macro references within CALL EXECUTE are executed immediately.
Still we have limitations for that, but that in more complicated examples. Like here: Can I CALL EXECUTE here?
CALL SYMPUT AND EXECUTE
The CALL SYMPUT allows the data step to add a new macro variable or overwrite an existing one to the macro space. This action is not performed until the data step is completed its processing. So it is a common knowledge that the macro variable created by CALL SYMPUT cannot be referenced inside the creating data step and you have to use another step outside this data step if you want to check the value of this macro variable or to use it. But CALL EXECUTE gives you the flexibility to reference the macro variable inside the data step used to create it.
Read more thist in CALL SYMPUT Routine doc. here: Problem Trying to Reference a SYMPUT-Assigned Value Before It Is Available
So what will happen when you run your first example for the first time, you get warning like:
WARNING: Apparent symbolic reference MACVAR not resolved.
Then if you run your example again you will not get this warning again, as from your first run the data step is completed and all statements that are stacked for later execution are exected.
But if you used CALL EXECUTE this will not happen, because macro references within CALL EXECUTE are executed immediately.
Still we have limitations for that, but that in more complicated examples. Like here: Can I CALL EXECUTE here?
Yes. You wouldn't be able to use it in the same data step, unless you are using symget() . but using symget() in the same data step with call symputx() is not meaningful .
data _null_;
call symputx('macvar','value');
x= symget('macvar');
put x=;
run;
@Ksharp Thanks. The CALL EXCECUTE I put in my example resolves the macro variable without generating SAS code. Isn't the primary function of CALL EXECUTE is to generate SAS code to be executed in the next boundary?
data _null_;
call symputx('macvar','value');
call execute('%put &macvar');
run;
NOTE: CALL EXECUTE routine executed successfully, but no SAS statements were generated.
Yes. The primary task of CALL EXECUTE() is dynamically generating code according to a dataset . and execute it after this data step.
But %put is macro statement ,not SAS statements I guesss, so you got that message in LOG .
data _null_;
call symputx('macvar','value');
call execute('%put &macvar;');
run;
<==>
data _null_;
call symputx('macvar','value');
run;
%put &macvar;
@Ksharp wrote:
Yes. The primary task of CALL EXECUTE() is dynamically generating code according to a dataset . and execute it after this data step.
But %put is macro statement ,not SAS statements I guesss, so you got that message in LOG .
Are you sure about that?
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.