DATA Step, Macro, Functions and more

When to use symput or symget?

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 133
Accepted Solution

When to use symput or symget?

When to use symput or symget?

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


Accepted Solutions
Solution
‎08-30-2017 01:33 PM
Super User
Super User
Posts: 7,076

Re: When to use symput or symget?

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


All Replies
Super Contributor
Posts: 1,636

Re: When to use symput or symget?

symput: creating macro variables in a datastep.

symget: getting macro variable value in a datastep.

Super Contributor
Posts: 1,636

Re: When to use symput or symget?

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

Frequent Contributor
Posts: 133

Re: When to use symput or symget?

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.

Super Contributor
Posts: 1,636

Re: When to use symput or symget?

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

Trusted Advisor
Posts: 1,301

When to use symput or symget?

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.

Solution
‎08-30-2017 01:33 PM
Super User
Super User
Posts: 7,076

Re: When to use symput or symget?

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.

Frequent Contributor
Posts: 133

Re: When to use symput or symget?

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.

Super User
Super User
Posts: 7,076

Re: When to use symput or symget?

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;

Frequent Contributor
Posts: 133

Re: When to use symput or symget?

Hello Tom,

really appreciate the illustrations.

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

Super User
Super User
Posts: 7,076

Re: When to use symput or symget?

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.

Respected Advisor
Posts: 3,156

Re: When to use symput or symget?

Thanks for the great lecture, Tom. Bookmarked!

Respected Advisor
Posts: 4,934

Re: When to use symput or symget?

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
PROC Star
Posts: 7,492

Re: When to use symput or symget?

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.

☑ This topic is solved.

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

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