Desktop productivity for business analysts and programmers

Understanding SYMPUT and Similar Routines

Accepted Solution Solved
Reply
New Contributor
Posts: 4
Accepted Solution

Understanding SYMPUT and Similar Routines

 

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!

 


Accepted Solutions
Solution
‎06-15-2016 11:27 AM
Super User
Posts: 11,105

Re: Understanding SYMPUT and Similar Routines

[ Edited ]

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.

View solution in original post


All Replies
Solution
‎06-15-2016 11:27 AM
Super User
Posts: 11,105

Re: Understanding SYMPUT and Similar Routines

[ Edited ]

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.

New Contributor
Posts: 4

Re: Understanding SYMPUT and Similar Routines

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!

Respected Advisor
Posts: 4,804

Re: Understanding SYMPUT and Similar Routines

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.

PG
Super User
Posts: 5,353

Re: Understanding SYMPUT and Similar Routines

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.

Respected Advisor
Posts: 3,786

Re: Understanding SYMPUT and Similar Routines

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.

 

 

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 5 replies
  • 477 views
  • 5 likes
  • 5 in conversation