Using SAS Macro Functions to Mask Special Characters

Macro Quoting Functions

The SAS programming language uses matched pairs of either double or single quotation marks to distinguish character constants from names. The quotation marks are not stored as part of the token that they define. For example, in the following program, Var is stored as a 4-byte variable that has the value text. If text were not enclosed in quotation marks, it would be treated as a variable name. Var2 is stored as a 7-byte variable that has the value example.
data one;
   var='text';
   text='example';
   var2=text;
run;
Similarly, the title text in the following example is Joan's Report. Although the TITLE statement contains a matched pair of double quotation marks, the title itself does not include these outer quotation marks. However, the outer quotation marks cause the unmatched single quotation mark within the text to be interpreted as an apostrophe that is part of the title text.
proc print;
   title "Joan's Report";
run;

The %STR Function

A Brief Overview

The %STR function is used to mask tokens during compilation so that the macro processor does not interpret them as macro-level syntax. That is, the %STR function hides the usual meaning of a semicolon (and other special tokens and mnemonic equivalents of comparison or logical operators) so that they appear as constant text. Special tokens and mnemonic equivalents include the following:
; + - * / , < > = blank ^ ~ # | 
LT EQ GT AND OR NOT LE GE NE IN
The %STR function also has these capabilities:
  • enables macro triggers to work normally
  • preserves leading and trailing blanks in its argument

%STR Syntax

Syntax, %STR function:
%STR (argument)
argument
is any combination of text and macro triggers.
The %STR function can also be used to mask tokens that typically occur in pairs:
' " ) (

Example: Using %STR Function

Suppose you have text that contains an apostrophe ( ' )  and you want to assign that text to a macro variable. Without any quoting, this produces errors.
options symbolgen;
%let text=Joan's Report;
proc print data=certadvadv.courses;
   where days > 3;
title "&text";
run;
The following is written to the SAS log.
Log 8.7 SAS Log
75 %let text=Joan's Report;
                ---------
                32
WARNING 32-169: The quoted string currently being processed has
                become more than 262 characters long. You may
                have unbalanced quotation marks.
The word scanner interprets the apostrophe as the beginning of a literal that is defined by a pair of single quotation marks. You can use the %STR function to avoid this error. The previous section covered several methods of using the %STR function to mask the usual meaning of a semicolon. None of the methods shown correctly masks the apostrophe in the current example.
When you mask tokens that typically appear in pairs, such as quotation marks or parentheses, you must take one additional step. To perform this masking, you precede the token that you want to mask with a percent sign (%) within the %STR function argument.
%let text=%str(Joan%'s Report);
%let text=Joan%str(%')s Report;
The value of Text is Joan's Report in both cases.

The %NRSTR Function

A Brief Overview

To hide the normal meaning of an ampersand or a percent sign, use the %NRSTR function. The %NRSTR performs in the same way as %STR, except that it also masks macro triggers (& and %). The NR in the name %NRSTR stands for No Resolution. %NRSTR has the same syntax as %STR.

%NRSTR Syntax

Syntax, %NRSTR function:
%NRSTR (character-string)
Note: The maximum level of nesting for the macro quoting functions is 10.

Example: Using %NRSTR Function

Suppose you want to create a macro variable named Period and assign a value of May&Jun to it. If you use the %STR function in the assignment statement, SAS interprets the ampersand as a macro trigger and generates a warning message. You must use the %NRSTR function instead.
%let Period=%str(May&Jun);
%put Period resolves to: &period;
%let Period=%nrstr(May&Jun);
%put Period resolves to: &period;
The following portion of a SAS log shows the results of both the %STR and the %NRSTR functions for this example.
Log 8.8 SAS Log
1    %let Period=%str(May&Jun);
WARNING: Apparent symbolic reference JUN not resolved.
2    %put Period resolves to &period:
WARNING: Apparent symbolic reference JUN not resolved.
Period resolves to: May&Jun
3
4    %let Period=%nrstr(May&Jun);
5    %put Period resolves to &period;
Period resolves to: May&Jun

The %SUPERQ Function

A Brief Overview

The %SUPERQ macro quoting function locates the macro variable named in its argument and retrieves the masked value of that macro variable without permitting any resolution to occur. It masks all items that might require macro quoting at macro execution. This ensures that the macro processor will never attempt to resolve macro triggers in the text resolved with %SUPERQ.
%SUPERQ is the only quoting function that prevents the resolution of macro variables and macro references in the value of the specified macro variable.
%SUPERQ accepts only the name of a macro variable as its argument, without an ampersand, but the other quoting functions accept any text expression, including constant text, as an argument.
The %SUPERQ function returns the value of a macro variable without attempting to resolve any macros or macro variable references in the value. %SUPERQ masks the following special characters and mnemonic operators:
&, %, ', ", ( ), + ,− ,* ,/ ,< >, =, ¬, ^, ~, ; , #, blank,
AND, OR, NOT, EQ, NE, LE, LT, GE, GT, IN 
Note:
  • The argument for %SUPERQ is just the macro variable name. There is no ampersand.
  • The maximum level of nesting for the macro quoting functions is 10.

%SUPERQ Syntax

Syntax, %SUPERQ function:
%SUPERQ (argument)
argument
is the name of either a macro variable with no leading ampersand or an expression that produces the name of a macro variable with no leading ampersand.

Example: Using the %SUPERQ Function

In this example, %SUPERQ prevents the macro processor from attempting to resolve macro references in the values of MV1 and MV2 before assigning them to macro variables TESTMV1 and TESTMV2.
data _null_;
   call symputx('mv1','Smith&Jones');
   call symputx('mv2','%macro abc;');
run;
%let testmv1=%superq(mv1);
%let testmv2=%superq(mv2);
%put Macro variable TESTMV1 is &testmv1;
%put Macro variable TESTMV2 is &testmv2;
The following is written to the SAS log.
Log 8.9 SAS Log
102  %let testmv1=%superq(mv1);
103  %let testmv2=%superq(mv2);
104  %put Macro variable TESTMV1 is &testmv1;
Macro variable TESTMV1 is Smith&Jones
105  %put Macro variable TESTMV2 is &testmv2;
Macro variable TESTMV2 is %macro abc;
You might think of the values of TESTMV1 and TESTMV2 as “pictures” of the original values of MV1 and MV2. The %PUT statement then writes the pictures in its text. The macro processor does not attempt resolution. It does not issue a warning message for the unresolved reference &JONES or an error message for beginning a macro definition inside a %LET statement.

The %BQUOTE Function

A Brief Overview

The %BQUOTE macro quoting function allows the macro processor to resolve all macro expressions before quoting the resulting text. It masks the characters that %STR masks without the requirement to mark unmatched quotation marks or parentheses with a % sign. The %BQUOTE function treats all parentheses and quotation marks produced by resolving macro variable references or macro calls as special characters to be masked at execution time. (It does not mask parentheses or quotation marks that are in the argument at compile time.) Therefore, it does not matter whether quotation marks and parentheses in the resolved value are matched. Each one is masked individually.
The %BQUOTE function masks a character string or resolved value of a text expression during execution of a macro or macro language statement. It masks the following special characters and mnemonic operators:
&, %, ', ", ( ), + ,− ,* ,/ ,< >, =, ¬, ^, ~, ; , #, blank,
AND, OR, NOT, EQ, NE, LE, LT, GE, GT, IN 

%BQUOTE Syntax

Syntax, %BQUOTE function:
%BQUOTE (character string | text expression)
character string | text expression
accepts any text including macro triggers.

Example: Using the %BQUOTE Function

data _null_;
   call symputx('text',"Sally's Seashell Store at Old Towne's Beach");
run;
data _null_;
   put "%bquote(&text)";
run;
Log 8.10 SAS Log
Sally's Seashell Store at Old Towne's Beach

Macro Q Functions

A Brief Overview

The macro Q functions process text in the macro facility just like the non-Q versions, but the Q functions return their results as quoted text. This means that the text produced by a Q function will never be mistaken for macro code. Unless you intend the function output to execute as macro code, it is a best practice to always use the Q function instead of the non-Q version.
Table 8.1 Macro Q Functions
Function Name
Description
%QUPCASE
converts values to uppercase
%QSUBSTR
extracts a substring from a character string
%QSCAN
extracts a word from a character string
%QSYSFUNC
executes a DATA step function and returns formatted results
The functions listed above work just like the non-Q version, but return the quoted text.

Example: Using the %QUPCASE Function

This example illustrates using the %PUT statement and %QUPCASE function to convert the lowercase values to uppercase.
%let a=%nrstr(Address&name); 
%put QUPCASE produces: %qupcase(&a); 
The following is written to the SAS log.
Log 8.11 SAS Log
QUPCASE produces: ADDRESS&NAME

Example: Using the %QSUBSTR Function

These statements show the results produced by %SUBSTR and %QSUBSTR:
%let a=one;
%let b=two;
%let c=%nrstr(&a &b);

%put C: &c;
%put With SUBSTR: %substr(&c,1,2);
%put With QSUBSTR: %qsubstr(&c,1,2);
Executing these statements produces the following messages in the SAS log. As you can see, the first %PUT statement shows that &c resolves to the value &a &b. In the second %PUT statement, the %SUBSTR function extracts the value &a from the resolved value of the macro variable reference &c, and resolves &a to 1. The third %PUT statement shows that the %QSUBSTR function prevents the value &a from being resolved further.
Log 8.12 SAS Log
11   %let a=one;
12   %let b=two;
13   %let c=%nrstr(&a &b);
14
15   %put C: &c;
C: &a &b
16   %put With SUBSTR: %substr(&c,1,2);
With SUBSTR: one
17   %put With QSUBSTR: %qsubstr(&c,1,2);
With QSUBSTR: &a

Example: Using the %QSCAN Function

These statements show the results produced by %SUBSTR and %QSUBSTR.
%macro a;
   aaaaaa
%mend a;
%macro b;
   bbbbbb
%mend b;
%macro c;
   cccccc
%mend c;

%let x=%nrstr(%a*%b*%c);
%put X: &x
%put The third word in X, with SCAN: %scan(&x,3,*);
%put The third word in X, with QSCAN: %qscan(&x,3,*);
Log 8.13 SAS Log
The third word in X, with SCAN: cccccc
The third word in X, with QSCAN: %c

Example: Using the %QSYSFUNC Function

Use the %QSYSFUNC function to mask the comma and execute the LEFT function. The LEFT function expects only one argument, but this example passes “June 4, 2019” to it. It interprets the comma as the delimiter between two arguments. You can mask the comma by using the %QSYSFUNC function.
title "Report Produced on %sysfunc(left(%qsysfunc(today(),worddate.)))";
The modified statement generates the following title:
Report Produced on June 4, 2019
Last updated: October 16, 2019
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset