As the first step in the decommissioning of sasCommunity.org the site has been converted to read-only mode.
Here are some tips for How to share your SAS knowledge with your professional network.
Resolving a macro variable within single quotes
The tip Resolving a macro variable within single quotes makes reference to using
to get a quoted string that includes a resolved macro variable.
Quite often you need to use
%unquote(%str(%')&YADAYADA.%str(%')) to get SAS to glue things back together as a single token. One notable exception is when the text is used in PROC SQL explicit pass-thru. In that case it is not SAS that sees the resulting quoted string, it is the data base. And by the time it gets there, the issue of it's being three+ tokens, instead of the literal string token is a non-issue.
The following log snippet illustrates why %unquote is needed when SAS is processing the string. Note the error that is caused by the resolved text string 'M' being passed as the clause for the WHERE statement as three tokens instead of one (the quoted string).
48 %let gender = M; 49 proc print data=sashelp.class; 50 where sex = %str(%')&gender%str(%'); 22: LINE and COLUMN cannot be determined. NOTE 242-205: NOSPOOL is on. Rerunning with OPTION SPOOL might allow recovery of the LINE and COLUMN where the error has occurred. ERROR 22-322: Syntax error, expecting one of the following: a name, a quoted string, a numeric constant, a datetime constant, a missing value, (, *, +, -, :, INPUT, NOT, PUT, ^, ~. 76: LINE and COLUMN cannot be determined. NOTE: NOSPOOL is on. Rerunning with OPTION SPOOL might allow recovery of the LINE and COLUMN where the error has occurred. ERROR 76-322: Syntax error, statement will be ignored. ERROR: Syntax error while parsing WHERE clause. 51 run;
The %unquote function causes another pass thru the wordscanner to glue the three tokens back together as a single token. Thus, the code works.
52 %let gender = M; 53 proc print data=sashelp.class; 54 where sex = %unquote(%str(%')&gender%str(%')); 55 run; NOTE: There were 10 observations read from the data set SASHELP.CLASS. WHERE sex='M';
However, when the code where this is done is passed to an external data base (e.g., PROC SQL explicit pass-thru), it is not SAS that receives the tokens and so this nuance of what the tokens are is moot. Given that many databases required single quotes for text strings, this is an important thing to point out.
In general, it should be considered a Best Practice to use %unquote to glue the tokens back together so it is interpreted as a single literal string token.