In SAS, the call symput function allows you to create macro variables in a SAS data step. You can assign both numeric and character values to macro variables with call symput.
data example;
call symput('var1', "example value");
call symput('var2', 2);
run;
%put &var1;
/* Log Output: */
example value
2
When working with SAS, the SAS Macro Language allows you to create dynamic and powerful programs. At the core of the SAS Macro Language are macro variables and the ability to easily create macro variables is important.
One such way you can create macro variables, or assign values to macro variables, in SAS is with call symput in a data step.
You can assign both numeric and character values to macro variables with call symput.
Below is a simple example showing you how you can use call symput to assign a value to a macro variable in SAS.
data example;
call symput('var1', "example value");
call symput('var2', 2);
run;
%put &var1;
/* Log Output: */
example value
2
Format of Macro Variables After Using call symput in SAS
There are a few things that you should know about what happens when you use call symput in a data step.
First, for character values, call symput by default writes values with the same length as the original data step variable. If the length of the variable in the SAS data step is 20 and you have a value like “bobby”, then there will be 15 trailing blanks in the macro variable.
For numeric values, symput writes these values using the best12. format. The resulting output to the log is a 12-byte character representing the numeric value and is right aligned.
Using call symput in Loop in Data Step
You can use call symput in a loop if you have similarly named variables or are looping over a list of variables defined in a macro variable.
Then, in the loop, you can assign values to multiple macro variables.
Below shows an example of how you could use call symput in a loop in a SAS data step.
data example;
input var1 var2 var3;
datalines;
3 4 5
;
run;
data want;
set example;
array v[3] var1-var3;
do i = 1 to 3;
call symput(cats('var',i),v[i]);
end;
run;
%put &var1;
%put &var2;
%put &var3;
/* Log Output */
3
4
5
You can also create macro variables in a macro as shown below. Note, these macro variables will not be available for use outside of a macro if you do it in this way because call symput creates the macro variables in the most local macro variable table.
data example;
input var1 var2 var3;
datalines;
3 4 5
;
run;
%macro loop;
data want;
set example;
%do i = 1 %to 3;
call symput(cats('var',&i), var&i);
%end;
run;
%put &var1;
%put &var2;
%put &var3;
%mend;
%loop;
/* Log Output */
3
4
5
Hopefully this article has been useful for you to learn how to use call symput in SAS.