DATA Step, Macro, Functions and more

Macro variable resolution

Accepted Solution Solved
Reply
Super Contributor
Posts: 271
Accepted Solution

Macro variable resolution

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;

Accepted Solutions
Solution
‎01-16-2016 11:41 PM
Super Contributor
Posts: 490

Re: Macro variable resolution

[ Edited ]
Posted in reply to SAS_inquisitive

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?

 

View solution in original post


All Replies
Solution
‎01-16-2016 11:41 PM
Super Contributor
Posts: 490

Re: Macro variable resolution

[ Edited ]
Posted in reply to SAS_inquisitive

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?

 

Super User
Posts: 10,044

Re: Macro variable resolution

Posted in reply to SAS_inquisitive

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;

 

Super Contributor
Posts: 271

Re: Macro variable resolution

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

Super User
Posts: 10,044

Re: Macro variable resolution

[ Edited ]
Posted in reply to SAS_inquisitive

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;

Respected Advisor
Posts: 3,799

Re: Macro variable resolution


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?

Super User
Posts: 10,044

Re: Macro variable resolution

Posted in reply to data_null__

I would be very happy to listen to the valuable advice from Mr. John King .  

Spoiler
Smiley Happy


 

☑ This topic is solved.

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

Discussion stats
  • 6 replies
  • 401 views
  • 2 likes
  • 4 in conversation