SAS Macro Quoting

SAS Macro Quoting functions: Masking Special Characters

Every Macro is a character in SAS. Even Macro values that are declared numeric are processed as character variables unless during expression or evaluation of the variables using macro functions such as %eval.

You can generate all sorts of special characters as text. But the macro language also consists of the same special characters, and this ambiguity arises.

The macro processor must know whether to interpret a particular special character such as a % sign or mnemonic like GE as text or as a symbol in the macro language.

What is SAS Macro Quoting functions?

SAS Macro quoting functions resolve these ambiguities by masking the significance of special characters so that the macro processor does not misinterpret them.

The following special characters and mnemonics might require masking when they appear in text strings:

= LT
; ( | GE
¬ + AND IN
^ OR %
~ * NOT &
, (comma) / EQ #
< NE
> LE
Special Characters and Mnemonics

Why Macro Quoting Necessary?

Macro quoting functions tell the macro processor to interpret special characters and mnemonics as text rather than as part of the macro language.

If you do not use a macro quoting function to mask the special characters, the macro processor or the rest of SAS might give the character a meaning you did not intend.

Here are some examples of the kinds of ambiguities that can arise when text strings contain special characters and mnemonics:

  • Is % sign a macro keyword to call a macro a “percent sign”?
  • Is OR the mnemonic Boolean operator or the abbreviation for Oregon?
  • Is the quote in They’re an unbalanced single quotation mark or the contraction of They are?
  • Is H&M a reference to the macro variable &M or the clothing brand?
  • Is GE the mnemonic for “greater than or equal”, or is it short for General Electric?
  • Which statement does a semicolon end?
  • Does a comma separate parameters, or is it part of the value of one of the parameters?

Macro quoting functions enable you to indicate to the macro processor how it is to interpret special characters and mnemonics.

Macro Quoting Functions

The following macro quoting functions are most commonly used in SAS:

  • %STR and %NRSTR

You can read the on  Macro Quoting Functions, and the Characters That They Mask

The function beginning with NR – No Rescan affects the same category of special characters masked by the plain macro quoting function and ampersands and percent signs.

Additionally, the NR functions prevent macro and macro variable resolution.

To help you remember which does which, try associating the NR in the macro quoting function names with the words “not resolved.”

— that is, macros and macro variables are not resolved when you use these functions.

The macro quoting functions with B in their names are useful for macro quoting unmatched quotation marks and parentheses.

When to use Compilation Functions and when to use Execution Functions?

Use Compilation functions at compilation time when you have to mask special characters or text constants and use the Execution function when you have to mask macro or macro variable references containing & or %.

Compilation Functions

Compilation functions cause the macro processor to interpret special characters as text in a macro program statement in open code or while compiling (constructing) a macro.

%STR and %NRSTR Functions

  1. %STR removes the meaning of most special characters except(% and &), removes commas and semicolons, and preserves blanks and null spaces at compilation. 
  2. Macro triggers like % and & are not masked.
  3. The %STR function can also handle characters in pairs, like parenthesis and mismatched quotes but unmatched characters need to precede the % symbol.

The below code will not work.

%let string=%str(They're);
%put &string;
%let string1=%str(50%);
%put &string1;

The updated code with a preceding % sign will work.

%let string=%str(They%'re);
%put &string;
%let string1=%str(50%%);
%put &string1;


It is used to prevent the resolution of macro variables at compilation.

  1. %NSTR removes the meaning of commas and semicolons and preserves blanks and null spaces at compilation. 
  2. Macro triggers like % and & are masked.
  3. The %STR function can also handle characters in pairs, like parenthesis and mismatched quotes but unmatched characters need to precede the % symbol.

This does not work, and Warning messages are generated due to unknown variable references and macro invocations. 

%let string2=&str(H&M);
%put &string2;
WARNING: Apparent symbolic reference STR not resolved. 
WARNING: Apparent symbolic reference M not resolved. 

You can also use %NRSTR to print the macro variable’s name, not the value.

%let string=%str(50%%);
%put %nrstr(&string);

Execution Functions

Execution functions cause the macro processor to treat special characters that result from resolving a macro expression as text (such as a macro variable reference, a macro invocation, or the argument of an implicit %EVAL function).

They are called execution functions because resolution occurs during macro execution or executing a macro program statement in open code.

The macro processor resolves the expression as far as possible, issues any warning messages for macro variable references or macro invocations it cannot resolve and quotes the result.


These functions mask special characters and mnemonic operators in a resolved value at macro execution.%QUOTE

  1. This function masks the following special characters and mnemonic operators:
+ - * / < > = ¬ ^ ~ ; , blank AND OR NOT EQ NE LE LT GE GT


%BQUOTE and %NRBQUOTE are used to mask values or macro variable references during the execution of a macro statement.

These functions mask all the characters that %STR and %NRSTR mask with the addition of unmatched quotation or parenthesis.

The %BQUOTE does not mask parenthesis or quotation marks in the argument at compile time.

In the below example, the conditional statement uses %BQUOTE to prevent an error if the macro variable state resolves to OR (for Oregon).

ERROR: A character operand was found in the %EVAL function or %IF condition where a numeric operand is required. The condition was: &state = OR ERROR: The macro TEST will stop executing.
%let state=OR;
%macro test;
%if %bquote(&state) = %str(OR) %then %put Oregon;

The %NRBQUOTE function is useful when you want a value to be resolved when it is first encountered but do not want any ampersands or per cent signs in the result.

If the argument of the %NRBQUOTE function contains an unresolved macro variable reference, SAS issues a warning message before it masks the ampersand or percent sign. You can use the %SUPERQ function to suppress the message.

%let str= 'H&M';
%let brand = %NRBQUOTE(&str);
%put print = &brand;

The %BQUOTE and %NRBQUOTE functions do not require preceding % for quotations and parenthesis, which the %QUOTE and %NRQUOTE require.


The %SUPERQ function takes as its argument a macro variable name (or a macro expression that yields a macro variable name). The argument must not be a reference to the macro variable whose value you are masking. That is, do not include the & before the name.

%SUPERQ does not attempt any resolution of its argument. Therefore, the macro processor does not issue any warning, unlike %NRBQUOTE.

The argument to a %SUPERQ function is either a macro variable name without an ampersand or a text expression that yields a macro variable name.

%let str= 'H&M';
%let brand = %superq(&str);
%put print = &brand;
print = 'H&M'

You may also want to look at the following cheat sheet: Deciding When to Use a Macro Quoting Function and Which Function to Use.

Every week we'll send you SAS tips and in-depth tutorials



Subhro provides valuable and informative content on SAS, offering a comprehensive understanding of SAS concepts. We have been creating SAS tutorials since 2019, and 9to5sas has become one of the leading free SAS resources available on the internet.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.