Chapter Contents


SAS Macro Language: Reference

Using the %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 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 by a macro quoting function. (It does not mask parentheses or quotation marks that are not produced by resolution.) 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 explicit or implicit %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 Chapter 13, is in effect). To suppress the message for unresolved macro variables, use the %SUPERQ function (discussed later in this chapter) instead.

Because the %BQUOTE and %NRBQUOTE functions operate during execution and are more flexible than %STR and %NRSTR, %BQUOTE and %NRBQOUTE are good choices for masking strings which 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)=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.  [cautionend]

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 the %BQUOTE function to allow a %IF condition to accept the unmatched single quotation mark:

data test;
   store="Susan's Office Supplies";
   call symput('s',store);

%macro readit;
   %if %bquote(&s) ne %then %put *** valid ***;
   %else %put *** null value ***;
%mend readit;


When you assign the value Susan's Office Supplies to STORE in the DATA step, enclosing the character string in double quotation marks allows you to use an unmatched single quotation mark in the string. The SAS System stores the value of STORE as

Susan's Office Supplies

The CALL SYMPUT routine assigns that value (containing an unmatched single quotation mark) as the value of the macro variable S. If you do not use the %BQUOTE function when you reference S in the macro READIT, the macro processor issues an error message for an invalid operand in the %IF condition.

When you submit the code, the following is written to the SAS log:

*** valid ***

Chapter Contents



Top of Page

Copyright 1999 by SAS Institute Inc., Cary, NC, USA. All rights reserved.