From:http://blog.chinaunix.net/uid-675476-id-2076827.html
In SAS macros, strings are enclosed in double quotation marks, but if a string contains special characters such as double quotes or a percent semicolon, a single quotation mark is used to escape the special characters in the string. Like what:
%let test = ' A ' is the first character! ';
However, if the string contains single quotes, the above method is invalidated. So more generally, SAS provides%STR and%nrstr to escape special characters. Where%str can escape commas, single and double quotes, left and right parenthesis, tilde, semicolon, space, sharp angle, etc., while%nrstr can escape the special symbols%str can escape, but also can be escaped with the (&) symbol and the percent sign. Specific use methods such as:
%let test=%str ("A" is the first character!);
It is important to note that when using%STR, if the referenced string contains mismatched single double quotes, the parentheses must be escaped with a percent sign, such as:
%let test=%str (i% ' m consultant.);
Of course, the author in order to fear errors,%str in general encountered special characters are escaped with a percent, such as:
%macro doit;
X "%str (copy%" a% "%" b% ")";
%mend doit
The most special symbol is the percent sign itself, if you want to include a percent in the%str, but also do not let the SAS and its subsequent% of the characters as a macro name or macro call, use percent to escape the percent semicolon, that is, with two percent sign to represent a percent semicolon, such as:
%let test =%str (% "%%%" is a special character!);
Test it:
Data _null_;
Put "&test";
Run
The log window is not printed out "%" is a special character! words.
%str and%nrstr are valid for constants, and%STR and%nrstr do not escape the contents of variables if they are variables. In this case, we need%bquote and%nrbquote, these two statement are specifically after the macro expansion, the macro execution of the variable corresponding to the contents of the escape, such as:
%let test =%STR (% "percent");
%macro DD (h=);
Data _null_;
%let a =%nrbquote (%substr (&h.,1,1));
Put "&a.";
Run
%mend DD;
%DD (h=&test.);