%BQUOTE and %NRBQUOTE Functions

Mask special characters and mnemonic operators in a resolved value at macro execution.
Type: Macro quoting function
See: %QUOTE and %NRQUOTE Functions and %SUPERQ Function

Syntax

%BQUOTE (character string | text expression)
%NRBQUOTE (character string | text expression)

Details

The %BQUOTE and %NRBQUOTE functions mask a character string or resolved value of a text expression during execution of a macro or macro language statement. They mask the following special characters and mnemonic operators:
' " ( ) + − * / < > = ¬ ^ ~ ; , # blank
AND OR NOT EQ NE LE LT GE GT IN
In addition, %NRBQUOTE masks:
& %
%NRBQUOTE is most useful when the resolved value of an argument might contain
  • strings that look like macro variable references but are not, so the macro processor should not attempt to resolve them when it next encounters them.
  • macro invocations that you do not want the macro processor to attempt to resolve when it next encounters them.
Note: The maximum level of nesting for the macro quoting functions is 10.
Tip: You can use %BQUOTE and %NRBQUOTE for all execution-time macro quoting because they mask all characters and mnemonic operators that can be interpreted as elements of macro language.
Quotation marks ('") do not have to be marked.
For a description of quoting in SAS macro language, see Macro Quoting.

Comparisons

%NRBQUOTE and the %SUPERQ function mask the same items. However, %SUPERQ does not attempt to resolve a macro variable reference or a macro invocation that occurs in the value of the specified macro variable. %NRBQUOTE does attempt to resolve such references.%BQUOTE and %NRBQUOTE do not require that you mark quotation marks.

Example: Quoting a Variable

This example tests whether a filename passed to the macro FILEIT starts with a quotation mark. Based on that evaluation, the macro creates the correct FILE command.
%macro fileit(infile);
  %if %bquote(&infile) NE %then
     %do;
         %let char1 = %bquote(%substr(&infile,1,1));
         %if %bquote(&char1) = %str(%')
             or %bquote(&char1) = %str(%")
         %then %let command=FILE &infile;
         %else %let command=FILE "&infile";
     %end;
  %put &command;
%mend fileit;
%fileit(myfile)
%fileit('myfile')
When this program executes, the following is written to the log:
FILE "myfile"
FILE 'myfile'