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

Hello team,

Why do we use call symput('something', apersonname) inside a sas statement? Can't we use %let

something = apersonname?

Regards,

Blue Blue

 

Blue Blue
1 ACCEPTED SOLUTION
9 REPLIES 9
Kurt_Bremser
Super User

Maxim 4. Try it.

%let something = apersonname;

works before any data step code runs, and will assign the text apersonname to the macro variable. CALL SYMPUT, OTOH, is a data step function and will assign the current content of data step variable apersonname to the macro variable.

ballardw
Super User

Use of %let pretty much means that you, as in the person writing code, know the value you need.

 

Call Symput and Call Symputx allow creating macro variables from values in a data set. So if the data changes the value of the macro variable can change. "Dynamic code" sometimes is used with this.

 

Note the both of the function do similar things but one is less likely to have issues with spaces (documentation to find which one). Also since the functions are executable you can test other variables and conditionally create the macro variable.

Caution: the assigned value when using a data set will come from the last time the Symput/Symputx executes. So if you aren't familiar with your data you might expect one result but get another.

 

Another reason to use the Symput function. The name of the macro variable could be the value of a character variable. So you might have a data set that contains a bunch of names and values to create a bunch of macro variables.

GN0001
Barite | Level 11
Why the code:
Data _NULL_;
Set SAShelp._CMPIDX_;
Call symput('ABC',key);
Run;
%put &ABC;
doesn't return all the values in variable key? It only returns the last observation in the key variable?

I don't understand this part:
"So you might have a data set that contains a bunch of names and values to create a bunch of macro variables."

I hope this makes sense.
Regards,
Blue Blue
Blue Blue
Kurt_Bremser
Super User

As I said previously, the CALL SYMPUT(X) routine stores the current content of its second argument into the macro variable. So the macro variable will contain what was in the second argument of the routine when it was last called.

For your second question, suppose you have a dataset

data have;
input name $ value $;
datalines;
a 1
b x
c f3
;

If you run

data _null_;
set have;
call symputx(name,value);
run;

you will create three macro variables with the contents of value.

This is a way to preserve macro variable values between SAS sessions.

 

 

Edit: fixed typo ("if" --> "of")

GN0001
Barite | Level 11
Hello KurtBremser,
This is the log, the macro variable name should show 3 values on the log: 1 x f3.
You can see the log doesn't show it.

< 1 OPTIONS NONOTES NOSTIMER NOSOURCE NOSYNTAXCHECK;
72
73 data _null_;
74 set have;
75 call symputx(name,value);
76 run;
>
Per what you said:
"you will create three macro variables with the contents if value"
I can't understand if value?
perhaps, there should be something to print the value of a macro on the log.
Regards,
Blue Blue
Blue Blue
ballardw
Super User

@GN0001 wrote:
Hello KurtBremser,
This is the log, the macro variable name should show 3 values on the log: 1 x f3.
You can see the log doesn't show it.

< 1 OPTIONS NONOTES NOSTIMER NOSOURCE NOSYNTAXCHECK;
72
73 data _null_;
74 set have;
75 call symputx(name,value);
76 run;
>
Per what you said:
"you will create three macro variables with the contents if value"
I can't understand if value?
perhaps, there should be something to print the value of a macro on the log.
Regards,
Blue Blue

%put _user_;

will place a list of all of the user (that's you) defined macro variables and the values into the Log.

 

That would be AFTER the data step.

 

Or you could use something like:

data _null_;
   set have;
   call symputx(name,value);
  put "created macro variable " name= " should have value " value=;
run;

to display them when created.

Kurt_Bremser
Super User

@GN0001 wrote:

Per what you said:
"you will create three macro variables with the contents if value"
I can't understand if value?

That was a typo, fixed in the meanwhile.

GN0001
Barite | Level 11
I think this is the most important difference:
Call Symput and Call Symputx allow creating macro variables from values in
a data set. So if the data changes the value of the macro variable can
change. "Dynamic code" sometimes is used with this.

Regards,
Blue Blue
Blue Blue

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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
  • 1534 views
  • 5 likes
  • 4 in conversation