Previous Page | Next Page

DATA Step Call Routines for Macros

CALL SYMPUT Routine



Assigns a value produced in a DATA step to a macro variable.
Type: DATA step call routine
See also: SYMGET Function and CALL SYMPUTX

Syntax
Details
Concepts
Scope of Variables Created with SYMPUT
Problem Trying to Reference a SYMPUT-Assigned Value Before It Is Available
Formatting Rules For Assigning Character Values
Formatting Rules For Assigning Numeric Values
Comparisons
Example
Creating Macro Variables and Assigning Them Values from a Data Set

Syntax

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:

However, there are three cases where SYMPUT creates the variable in the local symbol table, even if that symbol table is empty:

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:

Macro Processing provides details about compilation and execution.


Formatting Rules For Assigning Character Values

If value is a character variable, SYMPUT writes it using the $w. format, where 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 C is 8 by default. Therefore, SYMPUT uses the $8. format and assigns the letter x followed by seven trailing blanks as the value of CHAR1. To eliminate the blanks, use the TRIM function as shown in the second SYMPUT statement.

data char1;
   input c $;
   call symput('char1',c);
   call symput('char2',trim(c));
   datalines;
x
;
run;

%put char1 = ***&char1***;
%put char2 = ***&char2***;

When this program executes, these lines are written to the SAS log:

char1 = ***x       ***
char2 = ***x***


Formatting Rules For Assigning Numeric Values

If value is a numeric variable, SYMPUT writes it using the BEST12. format. The resulting value is a 12-byte string with the value right-aligned within it. For example, this DATA step assigns the value of numeric variable X to the macro variables NUM1 and NUM2. The last CALL SYMPUT statement deletes undesired leading blanks by using the LEFT function to left-align the value before the SYMPUT routine assigns the value to NUM2.

data _null_;
   x=1;
   call symput('num1',x);
   call symput('num2',left(x));
   call symput('num3',trim(left(put(x,8.)))); /*preferred technique*/
run;

%put num1 = ***&num1***;
%put num2 = ***&num2***;
%put num3 = ***&num3***;

When this program executes, these lines are written to the SAS log:

num1 = ***           1***
num2 = ***1           ***
num3 = ***1***


Comparisons


Example


Example 1: Creating Macro Variables and Assigning Them Values from a Data Set

data dusty;
   input dept $ name $ salary @@;
   datalines;
bedding Watlee 18000    bedding Ives 16000
bedding Parker 9000     bedding George 8000
bedding Joiner 8000     carpet Keller 20000
carpet Ray 12000        carpet Jones 9000
gifts Johnston 8000     gifts Matthew 19000
kitchen White 8000      kitchen Banks 14000
kitchen Marks 9000      kitchen Cannon 15000
tv Jones 9000           tv Smith 8000
tv Rogers 15000         tv Morse 16000
;

proc means noprint;
   class dept;
   var salary;
   output out=stats sum=s_sal;
run;

data _null_;
   set stats;
   if _n_=1 then call symput('s_tot',trim(left(s_sal)));
   else call symput('s'||dept,trim(left(s_sal)));
run;

%put _user_;

When this program executes, this list of variables is written to the SAS log:

GLOBAL SCARPET 41000
GLOBAL SKITCHEN 46000
GLOBAL STV 48000
GLOBAL SGIFTS 27000
GLOBAL SBEDDING 59000
GLOBAL S_TOT 221000

Previous Page | Next Page | Top of Page