BookmarkSubscribeRSS Feed
Fisher
Quartz | Level 8
I have this simple data step to assign a value to a macro variable:

data _null_;

call symput("para1", 'hello');

putlog "para1 is &para1";

run;


As you expect, when I run this code, in the log window, 'para1 is hello' is displayed.
The weird thing is, if you change the value 'hello' to, for example, 'world' and run the code again, what you get is still 'para1 is hello', not 'para1 is world' as expected. But if you run the codes for the second time, then 'para1 is world' is displayed in the log window, which is what is expected.
This is very confusing to me, seems the SAS :remembers" or "caches" the last value, what is the problem with it?
Anyone can give me a piece of light on this problem? thanks.

Eric

Message was edited by: EricSK Message was edited by: EricSK
9 REPLIES 9
sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10
Correction - on the "first" execution you should get a SAS warning because during the DATA step execution, the SAS macro variable PARA1 does not have an assigned value - see code log below:

1 data _null_;
2
3 call symput("para1", 'hello');
4
5 putlog "para1 is &para1";
WARNING: Apparent symbolic reference PARA1 not resolved.
6
7 run;

para1 is &para1
NOTE: DATA statement used (Total process time):
real time 0.06 seconds
cpu time 0.00 seconds


So, what you should consider doing is reading up in the SAS.COM website after searching for the argument keywords "macro variable scope". Or consider using Google advanced search argument below against the SAS.COM website:

macro variable scope site:sas.com

Normally, SAS macro variable resolution will occur at SAS compilation time and value assignments will be reflected *AFTER* the DATA or PROC step is completed. By including a RUN; or QUIT; statement, you ensure that SAS compiles your program up to that point.

Scott Barry
SBBWorks, Inc.
DanielSantos
Barite | Level 11
Scott is right.

For macro assignment/resolution AT runtime you should use the symput and symget family functions/routines.

See the only doc at SAS:
http://support.sas.com/documentation/cdl/en/mcrolref/61885/HTML/default/a000210322.htm
http://support.sas.com/documentation/cdl/en/lrdict/62618/HTML/default/a000127861.htm

Cheers from Portugal

Daniel Santos @ www.cgd.pt
Fisher
Quartz | Level 8
Thanks for the responses, guys.
Sorry, I didn't make it clear. Copy this codes and run it first:

1 data x;
2 x=1;
3 call symput('var','hello');
4 *putlog "para is: &var";
5 run;

at this step, there should be no problem. Then uncomment out the line 4 and run the codes again, 'para is: hello' is displayed in the log window, which is expected.
The weird thing I mean is, if you change the 'hello' to 'world' and run the codes again, you will still get the 'para is: hello', but not 'para is: world' until you run the codes for the second times.
data_null__
Jade | Level 19
It is all about what happens when the step is compiled vs executed. Look back at macro documention but this example may help you understand. Once you get it you can't forget it, but it may not be obvious to those new to SAS and SAS/MACRO.

[pre]
3095 data x;
3096 x=1;
3097 call symput('var','hello');
3098 varAtExecution=symget('VAR');
3099 varAtCompile="&var";
WARNING: Apparent symbolic reference VAR not resolved.
3100 putlog "para at complile is: &var";
WARNING: Apparent symbolic reference VAR not resolved.
3101 putlog 'para at execution is' varAtExecution=;
3102
3103 run;

para at complile is: &var
para at execution isvarAtExecution=hello
NOTE: The data set WORK.X has 1 observations and 3 variables.
NOTE: DATA statement used (Total process time):
real time 0.05 seconds
cpu time 0.02 seconds


3104 data x;
3105 x=1;
3106 call symput('var','hello');
3107 varAtExecution=symget('VAR');
3108 varAtCompile="&var";
3109 putlog "para at complile is: &var";
3110 putlog 'para at execution is' varAtExecution=;
3111
3112 run;

para at complile is: hello
para at execution isvarAtExecution=hello
NOTE: The data set WORK.X has 1 observations and 3 variables.
[/pre]
sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10
To EricSK - read the suggested documentation and learn about how SAS macro variables get resolved and when. Also, read clearly the replies -- the exact code you executed is what I executed -- however I had started a new/fresh SAS session and so there were no prior GLOBAL MACRO VARIABLES around to prop-up your DATA step logic.

Take the opportunity to read and learn - it will help enhance your SAS experience up-front rather than taking yours and others' time on the forums.

Scott Barry
SBBWorks, Inc.
Fisher
Quartz | Level 8
To sbb: thanks for your time and recommendation. I don't expect to waste anyone's time, I will be more careful next time when I think posting a question here.
DanielSantos
Barite | Level 11
OK, I posted at the same time data _null_ did, but the content is similar to it's post.

So you've got there you're explanation 😄

Cheers from Portugal

Daniel Santos @ www.cgd.pt Message was edited by: Daniel Santos
Flip
Fluorite | Level 6
The whole problem boils down to where the macro variable is examined. In the on-line SAS documentation it states:
"You cannot use a macro variable reference to retrieve the value of a macro variable in the same program (or step) in which SYMPUT creates that macro variable and assigns it a value."

You need to wait until after your RUN: statement to examine the macro variable.
Fisher
Quartz | Level 8
Thanks guys.
Your replies are very helpful. It is clear to me now. 🙂

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 9 replies
  • 1533 views
  • 0 likes
  • 5 in conversation