Notation Description Example
Quoted Value
Stored
%%%% two % signs in a row
%let myvar=
%str(%%%%);
%%
Note: If a macro variable ends with a % and at resolution is followed immediately by a
non-blank character followed by another macro variable, the second macro variable
might print twice. This can be avoided by using the %NRSTR function along with
two percent signs. For example, change the original syntax
%let val=abc%;
to this syntax
%let val=%nrstr(abc%%);
Examples Using %STR
The %STR function in the following %LET statement prevents the semicolon after
PROC PRINT from being interpreted as the ending semicolon for the %LET statement:
%let printit=%str(proc print; run;);
As a more complex example, the macro KEEPIT1 shows how the %STR function works
in a macro definition:
%macro keepit1(size);
%if &size=big %then %put %str(keep city _numeric_;);
%else %put %str(keep city;);
%mend keepit1;
Call the macro as follows:
%keepit1(big)
This code produces the following statement:
keep city _numeric_;
When you use the %STR function in the %IF-%THEN statement, the macro processor
interprets the first semicolon after the word %THEN as text. The second semicolon ends
the %THEN statement, and the %ELSE statement immediately follows the %THEN
statement. Thus, the macro processor compiles the statements as you intended. However,
if you omit the %STR function, the macro processor interprets the first semicolon after
the word %THEN as the end of the %THEN clause. The next semicolon as constant text.
Because only a %THEN clause can precede a %ELSE clause, the semicolon as constant
text causes the macro processor to issue an error message and not compile the macro.
In the %ELSE statement, the %STR function causes the macro processor to treat the first
semicolon in the statement as text and the second one as the end of the %ELSE clause.
Therefore, the semicolon that ends the KEEP statement is part of the conditional
execution. If you omit the %STR function, the first semicolon ends the %ELSE clause
and the second semicolon is outside the conditional execution. It is generated as text
each time the macro executes. (In this example, the placement of the semicolon does not
affect the SAS code.) Again, using %STR causes the macro KEEPIT1 to compile as you
intended.
Here is an example that uses %STR to mask a string that contains an unmatched single
quotation mark. Note the use of the % sign before the quotation mark:
%STR and %NRSTR Functions 89