- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
When to use symput or symget?
I just don't know what is the best way to use those functions.
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
symput: creating macro variables in a datastep.
symget: getting macro variable value in a datastep.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
As far as I know, you have to use "call symput" or "call symputx". I have never used "symput" without "call".
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Hello Tom,
really appreciate the illustrations.
But for the 3rd point, why there is a delay for symget varaiable to be evaluated?
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Thanks for the great lecture, Tom. Bookmarked!
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.