Hello team,
Why do we use call symput('something', apersonname) inside a sas statement? Can't we use %let
something = apersonname?
Regards,
Blue Blue
Yes, you have got it!
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.
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.
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 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.
@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.
Yes, you have got it!
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.
Ready to level-up your skills? Choose your own adventure.