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

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;
1 ACCEPTED SOLUTION

Accepted Solutions
mohamed_zaki
Barite | Level 11

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

6 REPLIES 6
mohamed_zaki
Barite | Level 11

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?

 

Ksharp
Super User

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;

 

SAS_inquisitive
Lapis Lazuli | Level 10

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

Ksharp
Super User

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;

data_null__
Jade | Level 19

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

Ksharp
Super User

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

Spoiler
🙂


 

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
  • 6 replies
  • 1685 views
  • 2 likes
  • 4 in conversation