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
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;
run;
proc print;
title "Subset of Data Set &dsn";
run;
Each time the reference &DSN appears, the macro processor replaces it with Newdata.
SAS sees the following statements:
DATA TEMP;
SET NEWDATA;
IF AGE>=20;
RUN;
PROC PRINT;
TITLE "Subset of Data Set NewData";
RUN;
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";
run;
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
To reuse the same text in several places, you can write a program with macro variable
references representing the common elements. You can change all the locations with a
single %LET statement, as shown:
%let name=sales;
data new&name;
set save.&name;
more SAS statements
if units>100;
run;
After macro variable resolution, SAS sees these statements:
DATA NEWSALES;
SET SAVE.SALES;
more SAS statements
IF UNITS>100;
RUN;
Notice that macro variable references do not require the concatenation operator as the
DATA step does. SAS forms the resulting words automatically.
Delimiting Macro Variable Names within Text
Sometimes when you use a macro variable reference as a prefix, the reference does not
resolve as you expect if you simply concatenate it. Instead, you might need to delimit the
reference by adding a period to the end of it.
A period immediately following a macro variable reference acts as a delimiter. That is, a
period at the end of a reference forces the macro processor to recognize the end of the
reference. The period does not appear in the resulting text.
Continuing with the example above, suppose that you need another DATA step that uses
the names Sales1, Sales2, and Insales.Temp. You might add the following step to the
program:
/* first attempt to add suffixes--incorrect */
data &name1 &name2;
set in&name.temp;
run;
After macro variable resolution, SAS sees these statements:
DATA &NAME1 &NAME2;
SET INSALESTEMP;
RUN;
None of the macro variable references have resolved as you intended. The macro
processor issues warning messages, and SAS issues syntax error messages. Why?
Because NAME1 and NAME2 are valid SAS names, the macro processor searches for
those macro variables rather than for NAME, and the references pass into the DATA
statement without resolution.
In a macro variable reference, the word scanner recognizes that a macro variable name
has ended when it encounters a character that is not used in a SAS name. However, you
can use a period ( . ) as a delimiter for a macro variable reference. For example, to cause
the macro processor to recognize the end of the word NAME in this example, use a
period as a delimiter between &NAME and the suffix:
/* correct version */
data &name.1 &name.2;
34 Chapter 3 Macro Variables
..................Content has been hidden....................

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