Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between defining SAS macrovariables via %let and call symput?

I am not sure what the difference is between

%let m=product and cal symput('m', 'product') is?

Aren't both expressions creating af macrovariable, m, which has the value product?

like image 628
user1626092 Avatar asked Feb 01 '26 21:02

user1626092


1 Answers

%let is used to define a macro variable based on text or an expression that resolves to text. It is called in open code, or in a macro. %let automatically trims the macro variable of both leading and trailing blanks. Documentation can be found here.

call symput (documentation here) is used to assign the contents of a SAS dataset variable, an expression that resolves to a SAS dataset variable (or a PDV variable), a character value, or an expression that resolves to a character value (including a numeric value, which resolves to its character equivalent in the default format, normally BEST12.). call symput does not trim leading or trailing blanks, so for example the expression

%let x=5;
%put [&x];

gives you a somewhat different result than the expression

data _null_;
call symput('y',5);
run;
%put [&y];

call symputx (available since 9.2) has more similar results to %let, in that it trims leading and trailing blanks. That is particularly helpful for macro variables created from numbers, as those typically have several leading blanks, like in the above example.

%let and call symput\symputx also have some differences in scoping. Both when used in open code (or a data step not in a macro) will place the macro variable in the global table, but %let used in a macro will place the variable in the most local table that it exists already, if it does; ie, if you have a global variable &myvar then %let myvar=5; will modify the global variable &myvar, not create a locally scoped variable. call symput will place the variable in the most local nonempty symbol table, regardless of whether it exists in a global scope already or not. call symputx will do the same, unless you specify an optional argument indicating which table you wish it to be placed.

like image 121
Joe Avatar answered Feb 03 '26 10:02

Joe