Difference between defining SAS macrovariables via %let and call symput?
Solution 1
%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.
Solution 2
The only difference is the method for invoking either %LET
or call symput
.
%LET
is used in open code, not inside a datastep or proc.
call symput
is used inside a datastep, and not in open code.
user1626092
Updated on June 04, 2022Comments
-
user1626092 over 1 year
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?
-
Andy K over 9 yearsThanks for the explanation, Joe.
-
Andy K over 9 yearsI did the test. It is just nice. Thanks again for that knowledge.