Deciding When to Use a Macro Quoting Function
and Which Function to Use
Use a macro quoting function anytime you want to assign to a macro variable a special
character that could be interpreted as part of the macro language. The following table
describes the special characters to mask when used as part of a text string and which
macro quoting functions are useful in each situation.
Table 7.2 Special Characters and Macro Quoting Guidelines
Special
Character Must Be Masked
Quoted by
All Macro
Quoting
Functions? Remarks
+-*/<>=^|¬ ~
# LE LT EQ
NE GE GT
AND OR
NOT IN
To prevent it from being
treated as an operator in
the argument of an
%EVAL function
Yes AND, OR, IN, and NOT need to
be masked because they are
interpreted as mnemonic operators
by an %EVAL and by
%SYSEVALF.
blank To maintain, rather than
ignore, a leading,
trailing, or isolated
blank
Yes
; To prevent a macro
program statement from
ending prematurely
Yes
, (comma) To prevent it from
indicating a new
function argument,
parameter, or parameter
value
Yes
Deciding When to Use a Macro Quoting Function and Which Function to Use 85
Special
Character Must Be Masked
Quoted by
All Macro
Quoting
Functions? Remarks
' " ( ) If it might be unmatched No Arguments that might contain
quotation marks and parentheses
should be masked with a macro
quoting function so that the macro
facility interprets the single and
double quotation marks and
parentheses as text rather than
macro language symbols or
possibly unmatched quotation
marks or parentheses for the SAS
language. With %STR, %NRSTR,
%QUOTE, and %NRQUOTE,
unmatched quotation marks and
parentheses must be marked with
a % sign. You do not have to mark
unmatched symbols in the
arguments of %BQUOTE,
%NRBQUOTE, and %SUPERQ.
%name
&name
(Depends on what the
expression might
resolve to)
No %NRSTR, %NRBQUOTE, and
%NRQUOTE mask these patterns.
To use %SUPERQ with a macro
variable, omit the ampersand from
name.
The macro facility allows you as much flexibility as possible in designing your macros.
You need to mask a special character with a macro quoting function only when the
macro processor would otherwise interpret the special character as part of the macro
language rather than as text. For example, in this statement that you must use a macro
quoting function to mask the first two semicolons to make them part of the text:
%let p=%str(proc print; run;);
However, in the macro PR, shown here, you do not need to use a macro quoting function
to mask the semicolons after PRINT and RUN:
%macro pr(start);
%if &start=yes %then
%do;
%put proc print requested;
proc print;
run;
%end;
%mend pr;
Because the macro processor does not expect a semicolon within the %DO group, the
semicolons after PRINT and RUN are not ambiguous, and they are interpreted as text.
Although it is not possible to give a series of rules that cover every situation, the
following sections describe how to use each macro quoting function. Table 7.6 on page
96 provides a summary of the various characters that might need masking and of which
macro quoting function is useful in each situation.
86 Chapter 7 Macro Quoting

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

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.