Chapter Contents

Previous

Next
SYMPUT

SYMPUT



Assigns a value produced in a DATA step to a macro variable

Type: SAS language routine
See also: SYMGET


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

value
is the value to be assigned, which can be


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

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:

Chapter 4, "Macro Processing" in SAS Macro Language: Reference provides details on 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));
   cards;
x
;
run;

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

Executing this program writes these lines 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***;

Executing this program writes these lines 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 @@;
   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.