SAS Macro Quoting functions: Masking Special Characters6 min read

Every Macro is a character in SAS. Even Macro values that are declared as 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 character as text. But the macro language also consists of the same special characters and this an ambiguity arises.

The macro processor must know whether to interpret a particular special character such as % 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 are used to 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:

SAS Macro Quoting functions: Masking Special Characters 2

Why Macro Quoting Is 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 clearly 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
  • %QUOTE and %NRQUOTE
  • %BQUOTE and %NRBQUOTE
  • %SUPERQ

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 that are masked by the plain macro quoting function as well as 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 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

%STR removes meaning of most special characters except(% and &), remove commas and semicolons, preserves blanks and null spaces at compilation.

The %STR function can also handle characters that are in pair, like parenthesis and mismatched quotes.

Note: If an unmatched single or double quotes or parenthesis is used with %STR or %NRSTR, these characters must be preceded by a percent sign. (%)

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;
OUTPUT:
They're
50%

%NRSTR is used to prevent the resolution of macro variables at compilation. For example, if you want to print the name of the macro variable, and not the value you can use %NRSTR.

%let string=%str(50%%);
%put %nrstr(&string);
OUTPUT:
&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 during the execution of 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.

%QUOTE and %NRQUOTE

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

%BQUOTE and %NRBQUOTE

%BQUOTE and %NRBQUOTE are used to mask values or macro variable references during the execution of a macro statement. These functions masks 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 that are 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;
%mend;
%test; 
OUTPUT:
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 percent signs in the result.

If the argument of %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;
WARNING: Apparent symbolic reference M not resolved.
OUTPUT:
print = 'H&M'

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

SUPERQ

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;
OUTPUT:
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.

by Subhro Kar
Been in the realm with the professionals of the IT industry. I am passionate about Coding, Blogging, Web Designing and deliver creative and useful content for a wide array of audience.

Leave a Comment

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

Share via
Copy link
Powered by Social Snap