Using 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 four-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 seven-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;

Example

Earlier you learned that macro variable values are character strings, and you saw examples of macro variables whose values included special characters. Now, suppose you want to store one or more SAS statements in a macro variable. For example, suppose you want to create a macro variable named prog with data new; x=1; run; stored as its value.
options symbolgen;
%let prog=data new; x=1; run;;
&prog
proc print;
run;
Here is part of the SAS log that results from the above program.
Table 9.5 SAS Log
25 options symbolgen;
26
27 %let prog=data new; x=1; run;
27 %let prog=data new; x=1; run;
                       -
                       180
ERROR 180-322: Statement is not valid or it is used out of proper order.
SYMBOLGEN: Macro variable PROG resolves to data new
28      &prog
29      proc print;
30      run;
NOTE: The data set WORK.NEW has 1 observations and 0 variables.
NOTE: The data set WORK.PROC has 1 observations and 0 variables.
NOTE: The data set WORK.PRINT has 1 observations and 0 variables.
NOTE: DATA statement used (Total process time):
      real time           0.25 seconds
      cpu time            0.07 seconds
Notice that according to the SYMBOLGEN statement in the log, the macro variable prog has been assigned a value of data new. SAS interpreted the first semicolon as the end of the macro assignment statement. In this case, we want the semicolon to be part of the macro variable value, but SAS has no way of knowing that. In this situation, you need to mask text that you want to assign to a macro variable. That is, you need to hide the normal meaning of the semicolon from the macro processor. You can use a macro quoting function to do this.

The %STR Function

The %STR function is used to mask (or write quotation marks around) tokens during compilation so that the macro processor does not interpret them as macro-level syntax. That is, the %STR function hides the normal 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
; + - * / , < > = blank ^ ~ # | 
LT EQ GT AND OR NOT LE GE NE IN
The %STR function also
  • enables macro triggers to work normally
  • preserves leading and trailing blanks in its argument.
General form, %STR function:
%STR (argument)
Here is an explanation of the syntax:
argument
is any combination of text and macro triggers.
Applying this to our previous example, there are a number of ways that text can be quoted. Remember that we wanted to create a macro variable named prog that has data new; x=1; run; as its value.
Method One
You could quote all text. %let prog=%str(data new; x=1; run;);
Method Two
You could quote only the semicolons. %let prog=data new%str(;) x=1%str(;)run%str(;);
Method Three
You could create an additional macro variable, assign a quoted value to it, and reference it in the assignment statement for the prog macro variable. %let s=%str(;); %let prog=data new&s x=1&s run&s;
Each of these methods accomplishes the same thing: they all assign the value data=new; x=1; run; to the macro variable prog.
The %STR function can also be used to quote tokens that typically occur in pairs:
' " ) (

Example

Suppose you want to assign text that contains an apostrophe ( ' )  to a macro variable. Without any quoting, this leads to errors.
options symbolgen;
%let text=Joan's Report;
proc print data=sasuser.courses;
   where days > 3;
title "&text";
run;
Table 9.6 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. In the last section you saw several methods of using the %STR function to mask the normal meaning of a semicolon. However, none of the methods shown correctly mask the apostrophe in our current example.
When you quote tokens that typically appear in pairs, such as quotation marks or parentheses, you must take one additional step. To perform this quoting, you precede the token that you want to quote 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

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

Example

Suppose you want to create a macro variable named period and to assign a value of May&Jun to it. If you attempt to use the %STR function in the assignment statement, SAS interprets the ampersand as a macro trigger and generate a warning message. You need to 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 function and the %NRSTR function for this example.
Table 9.7 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 %BQUOTE Function

Like the %STR function, the %BQUOTE function is used to mask (or write quotation marks around) special characters and mnemonic operators. The %STR function performs during compilation, and the %BQUOTE function performs during execution. That is, the %BQUOTE function masks a character string or resolved value of a text expression during execution of a macro or macro language statement so that special characters and mnemonic operators are not interpreted as anything other than plain text. Special tokens and mnemonic equivalents include
' " ( ) + - * / < > = ¬ ^ ~ ; , # blank 
AND  OR  NOT  EQ  NE  LE  LT  GE  GT  IN
The %BQUOTE function also
  • does not require that quotation marks be marked
  • enables macro triggers to work normally
  • preserves leading and trailing blanks in its argument.
General form, %BQUOTE function:
%BQUOTE (argument)
Here is an explanation of the syntax:
argument
is any combination of text and macro triggers.

Example

Remember the example where you want to assign text that contains an apostrophe (') to a macro variable. You used the %STR function to mask the apostrophe.
%let text=%str(Joan%'s Report);
%let text=Joan%str(%')s Report;
You can accomplish this task using the %BQUOTE function. The %BQUOTE function does not require that unmatched quotation marks be marked, so the title that contains an apostrophe requires no special syntax.
%let text=%bquote(Joan's Report);
..................Content has been hidden....................

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