CALL SYMPUT and CALL SYMPUTX
CALL SYMPUT and CALL SYMPUTX in SAS are the most commonly used DATA Step call routines to interact with the Macro Facility in SAS. Both are used to assign a value of the DATA step to a macro variable. In this article, we will demonstrate some of the uses and important facts of these two call Routines.
CALL SYMPUT in SAS
CALL SYMPUT(macro-variable, value);
Arguments are macro-variable and can be one of the following items:
macro-variable is a character string that is a SAS name, enclosed in quotation marks.
For example, to assign the character string ‘macrovar‘ to macro variable newMacro, submit the following statement in the data step:
???? It can contain the name of a character variable.
Using CALL SYMPUT to create macro variables of DATA step variables.
data currency_codes; input country $ code $4.; call symput('country', code); datalines; USA USD UK GBP Japan JPY Europe EUR India INR ; run;
This is an example of a character expression to create a macro variable name. You can create a series of macro variables using this form.
Creating a series of macro variable using CALL SYMPUT
In the below example, the CALL SYMPUT statement builds a series of macro variable names by combining the character string Country and the left-aligned value of _N_.
Values are assigned to the macro variables Country1, Country2, and so on.
data currency_codes; input country $ code $4.; call symput('Country'||left(_n_), country); datalines; USA USD UK GBP Japan JPY Europe EUR India INR ; run; %put &country2;
value is the value to be assigned, which can be a string enclosed in quotation marks.
For example, this statement assigns the string testing to the macro variable NEW:
data test; call symput('new', 'testing'); run; %put &new;
???? It can also be the name of a numeric or character variable. The macro variable is assigned with the value of the variable.
For numeric variables, SAS performs an automatic numeric-to-character conversion and writes a message in the log.
data test; a=2; b="a character variable"; call symput('a', a); call symput('b', b); run;
Use this form when macro-variable is also the name of a SAS variable or a character expression that contains a SAS variable.
A unique macro variable name and value can be created from each observation using the automatic data step variable _n_. See Example 2.
For character variables, SYMPUT creates only one macro variable, and its value changes in each iteration of the program. The macro variable contains only the value assigned in the last iteration of the data step.
data class; set sashelp.class; call symput('Name', name); run; %put &name;
It can be a DATA step expression. The value returned by the expression in the current observation is assigned as the value of macro-variable.
In this example, the macro variable named HOLDATE receives the value July 4, 1997:
data ex; input holiday mmddyy.; call symput('holdate', trim(left(put(holiday, worddate.)))); datalines; 070497 ; run;
If a macro-variable does not exist, SYMPUT creates it. SYMPUT makes a macro variable assignment when the program executes.
Creating many macro variables using do loop and array
There are situations where you need to convert all of the dataset’s variables into a macro variable.
data _null_; set sashelp.class; suffix=put(_n_, 5.); array num _NUMERIC_; do i=1 to dim(num); call symputx(cats(vname(num[i]), suffix), num[i]); end; array char _CHARACTER_; do i=1 to dim(char); call symputx(cats(vname(char[i]), suffix), char[i]); end; run; %put &name2; %put &age2;
As in the above example, we appended the suffix created from the automatic variable _n_ onto the variable name supplied by the function VNAME.
The SAS system requires separate array statements for characters and numeric.
Then, we can use the “*” in the array definition and the DIM function in the loop to the number of variables.
Scope of Variables Created with SYMPUT
SYMPUT puts the macro variable in the most local nonempty symbol table. A symbol table is nonempty if it contains the following:
However, there are three cases where SYMPUT creates the variable in the local symbol table, even if that symbol table is empty:
Problem Trying to Reference an SYMPUT-Assigned Value Before It Is Available
SYMPUT assigns the value of a macro variable during program execution whereas macro variable references resolve during the compilation of a step.
As a result of this, You cannot use a macro variable reference to retrieve the value of a macro variable in the same program (or step) which is used to create and assign that macro variable.
You must specify a step boundary such as a RUN statement or another DATA or proc statement to force the DATA step to execute before referencing a value in a global statement.
data test; var='Macro Variable'; call symput('mvar', var); proc print; title "Value of &var"; run;
Why there are trailing blanks when a macro variable is created using SYMPUT?
SYMPUT writes the value using $w. for the character variables. w is the length of the variable. Therefore, a value shorter than the length of the program variable is written with trailing blanks.
For example, in the following DATA step, the length of variables C1 and D1 is 8 by default. Therefore, SYMPUT uses $8. format and assigns the letter x followed by seven trailing blanks as the value of C1. For variable D1 the length of 8 is filled so there are no trailing blanks.
To remove the blanks, use the TRIM function as shown in the second SYMPUT statement.
data char1; input c $ d $; call symput('c1', c); call symput('c2', trim(c)); call symput('d1', d); datalines; x x1234567; run; %put c1 = ***&c1***; %put c2 = ***&c2***; %put d1 = ***&d1***;
When this program executes, these lines are written to the SAS log:
c1 = ***x ***c2 = ***x***d1 = ***x1234567***
For numeric variables, SYMPUT writes it using the BEST12. format. The resulting value is a 12-byte character with the value right-aligned within it.
For example, this DATA step assigns the value of the numeric variable n to the macro variables N1 and N2 and N3. The second CALL SYMPUT statement deletes leading blanks by using the LEFT function to left-align the value before the SYMPUT routine assigns the value to N2.
Because of this automatic variable conversion, SAS writes a note in the log. It is a best practice to explicitly convert the numeric variable to avoid the Note in the log. The third SYMPUT statement converts the variable to a character then removes the leading blanks.
data _null_; n=10; call symput('n1', n); call symput('n2', left(n)); call symput('n3', trim(left(put(n, 8.)))); run; %put n1 = ***&n1***; %put n2 = ***&n2***; %put n3 = ***&n3***;
When this program executes, these lines are written to the SAS log:
n1 = *** 10***n2 = ***10 ***n3 = ***10***
Also Read, Creating macro variables from SAS dataset
Assigns a value to a macro variable, and removes both leading and trailing blanks.
CALL SYMPUTX(macro-variable, value <, symbol-table>);
Main Benefits of using CALL SYMPUTX over CALL SYMPUT:
- No Notes in SAS LogCALL SYMPUTX does not write a note to the SAS log when the second argument is numeric.
- More widthCALL SYMPUTX uses a field width of up to 32 characters when it converts a numeric second argument to a character value. CALL SYMPUT uses a field width of up to 12 characters.
- No Leading BlanksCALL SYMPUTX left-justifies both arguments and trims trailing blanks. CALL SYMPUT does not left-justify the arguments and trims trailing blanks from the first argument only. Leading blanks in the value of the name cause an error.
- Control on Macro symbol table CALL SYMPUTX enables you to specify the symbol table in which to store the macro variable, whereas CALL SYMPUT does not.
If you use a CALL SYMPUT in a DATA step, where can you use the macro variable?
What is the maximum length of the macro variable?
Macro variables are 32 characters long.
What is the difference between SYMGET and SYMPUT?
SYMPUT puts the value from a dataset into a macro variable whereas SYMGET gets the value from the macro variable to the dataset.
What is the difference between %LOCAL and %GLOBAL?
% Local is a macro variable defined inside a macro.%Global is a macro variable defined in open code (outside the macro or can use anywhere)