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.
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 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.
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. 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;
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***