In the following statement, the %IF-%THEN statement
uses %BQUOTE to prevent an error if the macro variable STATE resolves
to OR (for Oregon), which the macro processor would interpret as the
logical operator OR otherwise:
%if %bquote(&state)=%str(OR) %then %put Oregon Dept. of
Revenue;
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:
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 ***