Assign Values
Blanks and
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;
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)));
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
are not resolved. Compare the following statements that assign a value to macro variable
DSN and use it in a TITLE statement:
%let dsn=Newdata;
title1 "Contents of Data Set &dsn";
title2 'Contents of Data Set &dsn';
In the first TITLE statement, the macro processor resolves the reference by replacing
&DSN with the value of macro variable DSN. In the second TITLE statement, the value
for DSN does not replace &DSN. SAS sees the following statements:
TITLE1 "Contents of Data Set Newdata";
TITLE2 'Contents of Data Set &dsn';
You can refer to a macro variable as many times as you need to in a SAS program. The
value remains constant until you change it. For example, this program refers to macro
variable DSN twice:
%let dsn=Newdata;
data temp;
set &dsn;
if age>=20;
proc print;
title "Subset of Data Set &dsn";
Each time the reference &DSN appears, the macro processor replaces it with Newdata.
SAS sees the following statements:
IF AGE>=20;
TITLE "Subset of Data Set NewData";
Note: If you reference a macro variable that does not exist, a warning message is printed
in the SAS log. For example, if macro variable JERRY is misspelled as JERY, the
following produces an unexpected result:
%let jerry=student;
data temp;
x="produced by &jery";
This code produces the following message:
WARNING: Apparent symbolic reference JERY not resolved.
Combining Macro Variable References with Text
It is often useful to place a macro variable reference next to leading or trailing text (for
example, DATA=PERSNL&YR.EMPLOYES, where &YR contains two characters for a
year), or to reference adjacent variables (for example, &MONTH&YR). You can reuse
the same text in several places or to reuse a program because you can change values for
each use.
Using Macro Variables 33

Get SAS 9.4 Macro Language, 5th Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.