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

When to use symput or symget?

I just don't know what is the best way to use those functions.

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

You use CALL SYMPUT (or better CALL SYMPUTX as it is more flexible) to create macro variables from data step values.

You use SYMGET to get macro variable values. Usually you can just reference the macro variable instead.

Here are some situations where you would want to use SYMGET.

The values of the macro variable have been modified while the data step is running. For example by using CALL SYMPUTX.

To handle complex values that are hard to mask with macro quoting.

To hide the value from the SAS log (for example a password) without having to worry about SYMBOLGEN and MPRINT settings.

To delay when the macro value is evaluated, for example in the definition of a view.

View solution in original post

13 REPLIES 13
Linlin
Lapis Lazuli | Level 10

symput: creating macro variables in a datastep.

symget: getting macro variable value in a datastep.

Linlin
Lapis Lazuli | Level 10

example:

data names;

input name $ 8.;

cards;

Art

Cynthia

Ksharp

Tom

Hai-Kuo

;

run;

/* to create macro variables teacher1-teacher5 */

data _null_;

  set names;

  call symputx(cats('teacher',strip(_n_)),name);

run;

%put &teacher1 &teacher2 &teacher3 &teacher4 &teacher5;

/* to get the value of &teacher1 &teacher2 &teacher3 &teacher4 &teacher5 */

data want;

   do i=1 to 5;

   expert=symget(cats('teacher',i));

   output;

   end;

  run;

proc print data=want;run;

Obs    i    expert

1     1    Art

2     2    Cynthia

3     3    Ksharp

4     4    Tom

5     5    Hai-Kuo

ZRick
Obsidian | Level 7

awesome example, quick question, instead of call routine,

can you use:

x = symputx(cats('teacher',strip(_n_)),name);

This is another confusing point, when to use call routine, not using function directly.

Linlin
Lapis Lazuli | Level 10

As far as I know, you have to use "call symput" or "call symputx". I have never used "symput" without "call".

FriedEgg
SAS Employee

symput and symputx are only available as call routines and not as functions.  Call routines and functions are not interchangable ideas.  Call routines are similar to functions and perform similar operations however, they are not used in assignment statements (your example x = sysputx...).  Call routines can return values to multiple variables simulateously(call scan vrs. scan) or none (call symputx).  Typically call routines exist instead of a function to obtain more than one value from a single statement.  The only really expections I can think of are the call routines that deal with macros such as call exeute, symdel, symput, symputn and symputx.  You could argue that a functional version of the call rountines should exist and provide a return value, somewhat similar to how dot notation objects do but this is not available at this time.

Tom
Super User Tom
Super User

You use CALL SYMPUT (or better CALL SYMPUTX as it is more flexible) to create macro variables from data step values.

You use SYMGET to get macro variable values. Usually you can just reference the macro variable instead.

Here are some situations where you would want to use SYMGET.

The values of the macro variable have been modified while the data step is running. For example by using CALL SYMPUTX.

To handle complex values that are hard to mask with macro quoting.

To hide the value from the SAS log (for example a password) without having to worry about SYMBOLGEN and MPRINT settings.

To delay when the macro value is evaluated, for example in the definition of a view.

ZRick
Obsidian | Level 7

Can you please illustrate:

1. The values of the macro variable have been modified while the data step is running.

2. To handle complex values that are hard to mask with macro quoting.

3. To delay when the macro value is evaluated, for example in the definition of a view.

Apologize for my not understanding those points well.

Tom
Super User Tom
Super User

1. The values of the macro variable have been modified while the data step is running.

Here is a simple example.  Obviously the data step would need to much more complex to make it worth worrying about this.

%let mvar=before;

data _null_;

  var1="&mvar";

  var2=symget('mvar');

  call symput('mvar','after');

  var3=symget('mvar');

  put (var1-var3) (=);

run;

2. To handle complex values that are hard to mask with macro quoting.

Macro programs are hard enough to read. If you have to start worrying about %quote(), %bquote(), %nrstr() , %superq(), %unquote() etc it can becomes almost indecipherable.

If the purpose was to pass a value to data step then you can use SYMGET() function to pull the value and not need to worry whether the value includes &,%, or unbalanced quotes.

3. To delay when the macro value is evaluated, for example in the definition of a view.

Try this little example.  Do you see why the second time the data pulled by the two views do not match?

data studies;

  do study='A001','B002','C003';

    output;

  end;

run;

%let study=A001;

data view1/view=view1;

  set studies;

  if study="&study";

run;

data view2/view=view2;

  set studies;

  if study=symget('study');

run;

proc compare data=view1 compare=view2;

run;

%let study=B002;

proc compare data=view1 compare=view2;

run;

ZRick
Obsidian | Level 7

Hello Tom,

really appreciate the illustrations.

But for the 3rd point, why there is a delay for symget varaiable to be evaluated?

Tom
Super User Tom
Super User

SAS stores the function into the view, but does not run it when it is building the view.  Instead the SYMGET() function runs when you use the view.

You could use this to store the view as a permanent file in a SAS library.  Then the records returned by the view will be based on the current settings of the macro variable.

Haikuo
Onyx | Level 15

Thanks for the great lecture, Tom. Bookmarked!

PGStats
Opal | Level 21

ZRick, I think it would be useful to future readers if you signaled one of Tom's answers as the correct one. Thanks for an interesting question.

PG

PG
art297
Opal | Level 21

PGStats,  ZRick (like a number of others who ask questions here) never bother to mark answers as being either helpful or correct.  Besides the fact that it would help users when they search for something, I think it would also encourage people to respond when those same individuals post new questions.

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 13 replies
  • 24464 views
  • 3 likes
  • 7 in conversation