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 DATA step to a macro variable.
In this article, I 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:
• 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.
Example 1: Using CALL SYMPUT to create macro variables of DATA step variables.
This DATA step creates the three macro variables countries, and respectively assign them the values in the code variable.
data currency_codes; input country $ 6. code $4.; call symput(country,code); datalines; USA USD UK GBP Japan JPY Europe EUR India INR ; run; %put &India;
• A character expression to create a macro variable name. You can create a series of macro variables using this form.
Example 2: Creating a series of macro variable
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 $ 6. 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 be the name of a numeric or character variable. The macro variable is assigned with the value of the variable,
For numeric variable, 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;
• 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 c; input holiday mmddyy.; call symput('holdate',trim(left(put(holiday,worddate.)))); datalines; 070497 ; run;
If macro-variable does not exist, SYMPUT creates it. SYMPUT makes a macro variable assignment when the program executes.
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:
- a value
- computed %GOTO (A computed %GOTO contains % or & and resolves to a label.)
- The macro variable
&SYSPBUFF, created at macro invocation time.
However, there are three cases where SYMPUT creates the variable in the local symbol table, even if that symbol table is empty:
- if SYMPUT is used after a PROC SQL, the variable will be created in a local symbol table.
- If an executing macro contains a computed %GOTO statement and uses SYMPUT to create a macro variable, the variable is created in the local symbol table.
- If an executing macro uses &SYSPBUFF and SYMPUT to create a macro variable, the macro variable is created in the local symbol table.
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 variable 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 variable, 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>);
value fo symbol table can be
- G – It specifies that the macro variable is stored in the global symbol table, even if the local symbol table exists.
- L – specifies that the macro variable is stored in the most local symbol table that exists, which will be the global symbol table if used outside a macro.
- F specifies that if the macro variable exists in any symbol table, CALL SYMPUTX uses the version in the most local symbol table in which it exists. If the macro variable does not exist, CALL SYMPUTX stores the variable in the most local symbol table.
Main Benefits of using CALL SYMPUTX over CALL SYMPUT:
If you use a SYMPUT in a DATA step, where can you use the macro variable?
Macro variables created using the CALL SYMPUT routine cannot be used in the same datastep in which it got created. Other than that we can use the macro variable at anywhere.
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 where as
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