Note: This example works if you use %STR, but it is not robust or good programming
practice. Because you cannot guarantee what &STATE is going to resolve to, you
need to use %BQUOTE to mask the resolution of the macro variable at execution
time, not the name of the variable itself at compile time.
In the following example, a DATA step creates a character value containing a single
quotation mark and assigns that value to a macro variable. The macro READIT then uses
the %BQUOTE function to enable a %IF condition to accept the unmatched single
quotation mark:
data test;
store="Susan's Office Supplies";
call symput('s',store);
run;
%macro readit;
%if %bquote(&s) ne %then %put *** valid ***;
%else %put *** null value ***;
%mend readit;
%readit
When you assign the value Susan's Office Supplies to STORE in the DATA
step, enclosing the character string in double quotation marks enables you to use an
unmatched single quotation mark in the string. SAS stores the value of STORE:
Susan's Office Supplies
The CALL SYMPUT routine assigns that value (containing an unmatched single
quotation mark) as the value of the macro variable S. If you do not use the %BQUOTE
function when you reference S in the macro READIT, the macro processor issues an
error message for an invalid operand in the %IF condition.
When you submit the code, the following is written to the SAS log:
*** valid ***
Referring to Already Quoted Variables
Items that have been masked by a macro quoting function, such as the value of WHOSE
in the following program, remain masked as long as the item is being used by the macro
processor. When you use the value of WHOSE later in a macro program statement, you
do not need to mask the reference again.
/* Use %STR to mask the constant, and use a % sign to mark */
/* the unmatched single quotation mark. */
%let whose=%str(John%'s);
/* You don't need to mask the macro reference, because it was */
/* masked in the %LET statement, and remains masked. */
%put *** This coat is &whose ***;
Here is the output from the %PUT statement that is written to the SAS log:
*** This coat is John's ***
92 Chapter 7 Macro Quoting
..................Content has been hidden....................

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