Hi, Folks.
I'm very new to SAS and am having a bit of trouble understanding the differences between SYMPUT, SYMPUTX, SYMPUTN, and SYMGET. I recognize that they all have a relationship to invoking macros but I'm not sure when each is appropriate. Could a more experienced user explain to me how these four functions overlap and how to determine which is most efficient?
Included below is some sample code from a program I am trying to interpret as part of a learning exercise. In this specific context, I'm wondering why SYMPUT would be used instead of the other options listed above.
data _null_;
year = year(date());
prev_year=year(date()) -1;
call symput('year', year);
call symput('prev_year', prev_year);
run;
Thank you for your help!
symput creates a macro variable, Symputx does so but removes leading and trailing spaces. Note that when use a numeric in Symput Symputx without explicitly converting to string using a format that the result is created with a default format that may include blanks.
SymputN is intended for use with SCL programming only as far as I can determine. Some of the SCL functions work in Base SAS but I get an error with symputn.
And macros only generate code text.
Please examine the log from this code:
data _null_;
year = year(date());
prev_year=year(date()) -1;
call symput('year', year);
call symputx('prev_year', prev_year);
run;
%put "&year" "&prev_year";
Note the spaces before 2016 (year) inside the quotes but not with 2015 (prev_year).
SYMGET is to return the value of a macro variable. There can be some slightly esoteric differences about what happens when using
var= symget('year')
or
Var= &year
or
Var="&year"
The main difference is that without quotes you can reference the name of datastep variable that contains names of macro variables and Symget will resolve the requested macro variable but the literal use when reference with an & you do not have that flexibility.
One reason SYMPUT may be used instead of others is that the Symputx and Symputn are relatively new and the code you look at may be "old". Second is habit for programmers that learned Symput when that was the only option. Third would be when you do not want to use the feature of Symputx to remove leading and trailing blanks because you need those blanks for some later purpose.
symput creates a macro variable, Symputx does so but removes leading and trailing spaces. Note that when use a numeric in Symput Symputx without explicitly converting to string using a format that the result is created with a default format that may include blanks.
SymputN is intended for use with SCL programming only as far as I can determine. Some of the SCL functions work in Base SAS but I get an error with symputn.
And macros only generate code text.
Please examine the log from this code:
data _null_;
year = year(date());
prev_year=year(date()) -1;
call symput('year', year);
call symputx('prev_year', prev_year);
run;
%put "&year" "&prev_year";
Note the spaces before 2016 (year) inside the quotes but not with 2015 (prev_year).
SYMGET is to return the value of a macro variable. There can be some slightly esoteric differences about what happens when using
var= symget('year')
or
Var= &year
or
Var="&year"
The main difference is that without quotes you can reference the name of datastep variable that contains names of macro variables and Symget will resolve the requested macro variable but the literal use when reference with an & you do not have that flexibility.
One reason SYMPUT may be used instead of others is that the Symputx and Symputn are relatively new and the code you look at may be "old". Second is habit for programmers that learned Symput when that was the only option. Third would be when you do not want to use the feature of Symputx to remove leading and trailing blanks because you need those blanks for some later purpose.
ballardw: Thank you for your response! It was particularly helpful to know that Symputn and Symputx are relatively new - this does explain why the code I was trying to understand would have used symput!
The only function you need to know in order to write your own SAS code is SYMPUTX. The other functions will help you understand older SAS code.
Just to play devil's advocate for a moment ... if you are relatively new to SAS, you should not spend time on macro language.
Macro language does not process data. Instead, it generates SAS statements, which will then process your data. You need more experience with SAS statements, to be able to picture the statements that could be generated. Only then will macro language be useful. Here is a list of topics that would be more worthy of your time at this point:
MERGE, and using in= variables to detect matches and mismatches
BY statement in a DATA step
How SAS handles dates
RETAIN statement, and sum statement
The list could be longer, but the real point is that many SAS language topics are more important than macro language when you are new to SAS.
Good luck.
SYMGET and SYMGETN are used to retrieve the value of a macro variable during execution of a data step. The N version returns a numeric value by converting the character macro variable value.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.