Chapter Contents |
Previous |
Next |
SYMPUT |
Type: | SAS language routine |
See also: | SYMGET |
Syntax |
CALL SYMPUT(macro-variable,value); |
testing
to macro variable NEW
call symput('new','testing');
data team1; input position : $8. player : $12.; call symput(position,player); cards; shortstp Ann pitcher Tom frstbase Bill ;
data team2; input position : $12. player $12.; call symput('POS'||left(_n_), position); cards; shortstp Ann pitcher Tom frstbase Bill ;
testing
to the macro variable NEW:
call symput('new','testing');
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.
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.
July 4,1997
:
data c; input holiday mmddyy.; call symput('holdate',trim(left(put(holiday,worddate.)))); cards; 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 |
SYMPUT puts the macro variable in the most
local nonempty symbol
table. In addition to a symbol table that contains a value, a symbol table
is also considered nonempty if a computed %GOTO is present or the macro variable
&SYSPBUFF is created at macro invocation time. (A computed %GOTO contains
%
or
&
and resolves to a label.)
If the local symbol table is empty and an executing macro contains a computed %GOTO and uses SYMPUT to create a macro variable, the variable is created in the local, empty symbol table and not in the nearest nonempty symbol table.
If the local symbol table is empty and an executing macro uses &SYSPBUFF and SYMPUT to create a macro variable, the macro is created in the local, empty symbol table and not in the nearest nonempty symbol table.
For more information on creating a variable with SYMPUT, see Chapter
5 in SAS Macro Language: Reference.
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:
data x; x='December'; call symput('var',x); proc print; title "Report for &var"; run;
Chapter 4, "Macro Processing" in SAS Macro Language: Reference provides details
on compilation and execution.
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)); cards; x ; run; %put char1 = ***&char1***; %put char2 = ***&char2***;
Executing this program writes these lines to the SAS log:
char1 = ***x *** char2 = ***x***
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***;
Executing this program writes these lines to the SAS log:
num1 = *** 1*** num2 = ***1 *** num3 = ***1***
Comparisons |
Example |
data dusty; input dept $ name $ salary @@; cards; 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_;
Executing this program writes these lines this list of variables to the SAS log:
GLOBAL SCARPET 41000 GLOBAL SKITCHEN 46000 GLOBAL STV 48000 GLOBAL SGIFTS 27000 GLOBAL SBEDDING 59000 GLOBAL S_TOT 221000
Chapter Contents |
Previous |
Next |
Top of Page |
Copyright 1999 by SAS Institute Inc., Cary, NC, USA. All rights reserved.