Assign Values
Blanks and
special
characters
Macro quoting function %STR or %NRSTR around the value. This action
masks the blanks or special characters so that the macro processor interprets
them as text. For more information, see “Macro Quoting Functions” on page
163. For example,
%let state=%str( North Carolina);
%let town=%str(Taylor%'s Pond);
%let store=%nrstr(Smith&Jones);
%let plotit=%str(
proc plot;
plot income*age;
run;);
The definition of macro variable TOWN demonstrates using %STR to mask
a value containing an unmatched quotation mark. “Macro Quoting
Functions” on page 163 discuss macro quoting functions that require
unmatched quotation marks and other symbols to be marked.
The definition of macro variable PLOTIT demonstrates using %STR to
mask blanks and special characters (semicolons) in macro variable values.
When a macro variable contains complete SAS statements, the statements
are easier to read if you enter them on separate lines with indentions for
statements within a DATA or PROC step. Using a macro quoting function
retains the significant blanks in the macro variable value.
Value from a
DATA step
The SYMPUT routine. This example puts the number of observations in a
data set into a FOOTNOTE statement where AGE is greater than 20:
data _null_;
set in.permdata end=final;
if age>20 then n+1;
if final then call symput('number',trim(left(n)));
run;
footnote "&number Observations have AGE>20";
During the last iteration of the DATA step, the SYMPUT routine creates a
macro variable named NUMBER whose value is the value of N. (SAS also
issues a numeric-to-character conversion message.) The TRIM and the
LEFT functions remove the extra space characters from the DATA step
variable N before its value is assigned to the macro variable NUMBER.
For a discussion of SYMPUT, including information about preventing the
numeric-character message, see “CALL SYMPUT Routine” on page 240.
Using Macro Variables
Macro Variable Reference
After a macro variable is created, you typically use the variable by referencing it with an
ampersand preceding its name (&variable-name), which is called a macro variable
reference. These references perform symbolic substitutions when they resolve to their
value. You can use these references anywhere in a SAS program. To resolve a macro
variable reference that occurs within a literal string, enclose the string in double
quotation marks. Macro variable references that are enclosed in single quotation marks
32 Chapter 3 • Macro Variables