Details
If macro-variable exists in any enclosing scope, macro-variable is updated. If macro-
variable does not exist, SYMPUT creates it. (See below to determine in which scope
SYMPUT creates macro-variable.) 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 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” on page 49.
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 on page 39 provides details about compilation and execution.
242 Chapter 15 DATA Step Call Routines for Macros
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
SYMPUT assigns values produced in a DATA step to macro variables during
program execution, but the SYMGET function returns values of macro variables to
the program during program execution.
SYMPUT is available in DATA step and SCL programs, but SYMPUTN is available
only in SCL programs.
SYMPUT assigns character values, but SYMPUTN assigns numeric values.
CALL SYMPUT Routine 243
Example: 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
CALL SYMPUTN Routine
In SCL programs, assigns a numeric value to a global macro variable.
Type: SCL call routine
Restriction: The SYMPUT CALL routine is not supported by the CAS engine.
See: “SYMGET Function” on page 252, “SYMGETN Function” on page 255, and “CALL
SYMPUT Routine” on page 240
Syntax
CALL SYMPUTN('macro-variable', value);
244 Chapter 15 DATA Step Call Routines for Macros
Required Arguments
macro-variable
is the name of a global macro variable with no ampersand – note the single quotation
marks. Or, it is the name of an SCL variable that contains the name of a global macro
variable.
value
is the numeric value to assign, which can be a number or the name of a numeric SCL
variable.
Details
The SYMPUTN routine assigns a numeric value to a global SAS macro variable.
SYMPUTN assigns the value when the SCL program executes. You can also use
SYMPUTN to assign the value of a macro variable whose name is stored in an SCL
variable. For example, to assign the value of SCL variable UNITNUM to SCL variable
UNITVAR, which contains 'UNIT', submit the following:
call symputn(unitvar,unitnum)
You must use SYMPUTN with a CALL statement.
Note: It is inefficient to use an ampersand (&) to reference a macro variable that was
created with CALL SYMPUTN. Instead, use SYMGETN. It is also inefficient to use
CALL SYMPUTN to store a variable that does not contain a numeric value.
Comparisons
SYMPUTN assigns numeric values, but SYMPUT assigns character values.
SYMPUTN is available only in SCL programs, but SYMPUT is available in DATA
step programs and SCL programs.
SYMPUTN assigns numeric values, but SYMGETN retrieves numeric values.
Example: Storing the Value 1000 in the Macro Variable
UNIT When the SCL Program Executes
This statement stores the value 1000 in the macro variable UNIT when the SCL program
executes:
call symputn('unit',1000);
CALL SYMPUTX Routine
Assigns a value to a macro variable, and removes both leading and trailing blanks.
Category: Macro
Restriction: The SYMPUT CALL routine is not supported by the CAS engine.
See: “CALL SYMPUTX Routine” in SAS Functions and CALL Routines: Reference
Syntax
CALL SYMPUTX(macro-variable, value <, symbol-table>);
CALL SYMPUTX Routine 245
Required Arguments
macro-variable
can be one of the following items:
a character string that is a SAS name, enclosed in quotation marks.
the name of a character variable whose values are SAS names.
a character expression that produces a macro variable name. This form is useful
for creating a series of macro variables.
a character constant, variable, or expression. Leading and trailing blanks are
removed from the value of name, and the result is then used as the name of the
macro variable.
value
is the value to be assigned, which can be
a string enclosed in quotation marks.
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.
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. A unique
macro variable name and value can be created from each observation.
a DATA step expression. The value returned by the expression in the current
observation is assigned as the value of macro-variable.
If the expression is numeric, SAS performs an automatic numeric-to-character
conversion and writes a message in the log.
Optional Argument
symbol-table
specifies a character constant, variable, or expression. The value of symbol-table is
not case sensitive. The first non-blank character in symbol-table specifies the symbol
table in which to store the macro variable. The following values are valid as the first
non-blank character in symbol-table:
G
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.
Note: If you omit symbol-table, or if symbol-table is blank, CALL SYMPUTX
stores the macro variable in the same symbol table as does the CALL
SYMPUT routine.
246 Chapter 15 DATA Step Call Routines for Macros
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset