You must use %NRSTR to mask the % sign at compilation, so the macro processor does
not try to invoke %NRSTR a second time. If you did not use %NRSTR to mask the
string %nrstr, the macro processor would complain about a missing open parenthesis
for the function.
%BQUOTE and %NRBQUOTE Functions
Using %BQUOTE and %NRBQUOTE Functions
%BQUOTE and %NRBQUOTE mask values during execution of a macro or a macro
language statement in open code. These functions instruct the macro processor to resolve
a macro expression as far as possible and mask the result, issuing any warning messages
for macro variable references or macro invocations that it cannot resolve. These
functions mask all the characters that %STR and %NRSTR mask with the addition of
unmarked percent signs; unmatched, unmarked single and double quotation marks; and
unmatched, unmarked opening and closing parentheses. That means that you do not have
to precede an unmatched quotation mark with a % sign, as you must when using %STR
and %NRSTR.
The %BQUOTE function treats all parentheses and quotation marks produced by
resolving macro variable references or macro calls as special characters to be masked at
execution time. (It does not mask parentheses or quotation marks that are in the
argument at compile time.) Therefore, it does not matter whether quotation marks and
parentheses in the resolved value are matched; each one is masked individually.
The %NRBQUOTE function is useful when you want a value to be resolved when first
encountered, if possible, but you do not want any ampersands or percent signs in the
result to be interpreted as operators by an %EVAL function.
If the argument of the %NRBQUOTE function contains an unresolvable macro variable
reference or macro invocation, the macro processor issues a warning message before it
masks the ampersand or percent sign (assuming the SERROR or MERROR system
option, described in “System Options for Macros” on page 343 is in effect). To suppress
the message for unresolved macro variables, use the %SUPERQ function (discussed
later in this section) instead.
The %BQUOTE and %NRBQUOTE functions operate during execution and are more
flexible than %STR and %NRSTR. Therefore, %BQUOTE and %NRBQUOTE are
good choices for masking strings that contain macro variable references.
Examples Using %BQUOTE
In the following statement, the %IF-%THEN statement uses %BQUOTE to prevent an
error if the macro variable STATE resolves to OR (for Oregon), which the macro
processor would interpret as the logical operator OR otherwise:
%if %bquote(&state)=%str(OR) %then %put Oregon Dept. of
Revenue;
Note: This example works if you use %STR, but it is not robust or good programming
practice. Because you cannot guarantee what &STATE is going to resolve to, you
need to use %BQUOTE to mask the resolution of the macro variable at execution
time, not the name of the variable itself at compile time.
In the following example, a DATA step creates a character value containing a single
quotation mark and assigns that value to a macro variable. The macro READIT then uses
%BQUOTE and %NRBQUOTE Functions 89

Get SAS 9.4 Macro Language: Reference, Fourth Edition, 4th 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.