%QUOTE and %NRQUOTE Functions

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

Syntax

%QUOTE (character string | text expression)
%NRQUOTE (character string | text expression)

Details

The %QUOTE and %NRQUOTE 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
They also mask the following characters when they occur in pairs and when they are not matched and are marked by a preceding %
' "
In addition, %NRQUOTE masks
& %
%NRQUOTE is most useful when an argument might contain a macro variable reference or macro invocation that you do not want resolved.
For a description of quoting in SAS macro language, see Macro Quoting.
Note that the maximum level of nesting for the macro quoting functions is 10.

Comparisons

  • %QUOTE and %NRQUOTE mask the same items as %STR and %NRSTR, respectively. However, %STR and %NRSTR mask constant text instead of a resolved value. And, %STR and %NRSTR work when a macro compiles, while %QUOTE and %NRQUOTE work when a macro executes.
  • The %BQUOTE and %NRBQUOTE functions do not require that quotation marks without a match be marked with a preceding %, while %QUOTE and %NRQUOTE do.
  • %QUOTE and %NRQUOTE mask resolved values, while the %SUPERQ function prevents resolution of any macro invocations or macro variable references that might occur in a value.

Example: Quoting a Value that Might Contain a Mnemonic Operator

The macro DEPT1 receives abbreviations for states and therefore might receive the value OR for Oregon.
%macro dept1(state);
      /* without %quote -- problems might occur */
   %if &state=nc %then
       %put North Carolina Department of Revenue;
   %else %put Department of Revenue;
%mend dept1;
%dept1(or)
When the macro DEPT1 executes, the %IF condition executes a %EVAL function, which evaluates or as a logical operator in this expression. Then the macro processor produces an error message for an invalid operand in the expression or=nc.
The macro DEPT2 uses the %QUOTE function to treat characters that result from resolving &STATE as text:
%macro dept2(state);
      /* with %quote function--problems are prevented */
   %if %quote(&state)=nc %then
       %put North Carolina Department of Revenue;
   %else %put Department of Revenue;
%mend dept2;
%dept2(or)
The %IF condition now compares the strings or and nc and writes to the SAS log:
Department of Revenue