SAS: What’s CALL SYMPUT?

CALL SYMPUT(macro-variable, value);

macro-variable
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 testing to macro variable NEW, submit the following statement:
    call symput('new','testing');
    
  • the name of a character variable whose values are SAS names. For example, this DATA step creates the three macro variables SHORTSTP, PITCHER, and FRSTBASE and respectively assign them the values ANN, TOM, and BILL.
    data team1;
       input position : $8. player : $12.;
       call symput(position,player);
    datalines;
    shortstp Ann
    pitcher Tom
    frstbase Bill
    ;
    
  • a character expression that produces a macro variable name. This form is useful for creating a series of macro variables. For example, the CALL SYMPUT statement builds a series of macro variable names by combining the character string POS and the left-aligned value of _N_ and assigns values to the macro variables POS1, POS2, and POS3.
    data team2;
       input position : $12. player $12.;
       call symput('POS'||left(_n_), position);
       datalines;
    shortstp Ann
    pitcher Tom
    frstbase Bill
    ;
    
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:
    call symput('new','testing');
    
  • the name of a numeric or character variable. The current value of the variable is assigned as the value of the macro variable. If the variable is numeric, SAS performs an automatic numeric-to-character conversion and writes a message in the log. Later sections on formatting rules describe the rules that SYMPUT follows in assigning character and numeric values of DATA step variables to macro variables.Note:   This form is most useful when macro-variable is also the name of a SAS variable or a character expression that contains a SAS variable because a unique macro variable name and value can be created from each observation, as shown in the previous example for creating the data set TEAM1.  [cautionend]

    If macro-variable is a character string, SYMPUT creates only one macro variable, and its value changes in each iteration of the program. Only the value assigned in the last iteration remains after program execution is finished.

  • 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 the expression is numeric, SAS performs an automatic numeric-to-character conversion and writes a message in the log. Later sections on formatting rules describe the rules that SYMPUT follows in assigning character and numeric values of expressions to macro variables.


Details

If macro-variable does not exist, SYMPUT creates it. SYMPUT makes a macro variable assignment when the program executes.

SYMPUT can be used in all SAS language programs, including SCL programs. Because it resolves variables at program execution instead of macro execution, SYMPUT should be used to assign macro values from DATA step views, SQL views, and SCL programs.


Concepts

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
  • a 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:

  • Beginning with Version 8, 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.

For more information about creating a variable with SYMPUT, see Scopes of Macro Variables.

Problem Trying to Reference a SYMPUT-Assigned Value Before It Is Available

One of the most common problems in using SYMPUT is trying to reference a macro variable value assigned by SYMPUT before that variable is created. The failure generally occurs because the statement referencing the macro variable compiles before execution of the CALL SYMPUT statement that assigns the variable’s value. The most important fact to remember in using SYMPUT is that it assigns the value of the macro variable during program execution, but macro variable references resolve during the compilation of a step, a global statement used outside a step, or an SCL program. As a result:

  • You cannot use a macro variable reference to retrieve the value of a macro variable in the same program (or step) in which SYMPUT creates that macro variable and assigns it a value.
  • You must specify a step boundary statement to force the DATA step to execute before referencing a value in a global statement following the program (for example, a TITLE statement). The boundary could be a RUN statement or another DATA or PROC statement. For example:
    data x;
       x='December';
       call symput('var',x);
    
    proc print;
    title "Report for &var";
    run;