hi, im trying to create a macro variable containing a search term such as %UNS%.
I will need to use this search term many times throughout the program.
For example:
%let searchterm=%UNS%;
data out;
set in (where=(visit like "&searchterm")) ;
run;
The problem is that the % is messing things up. How can I use such a macro variable?
%let searchterm=%nrstr(%%)UNS%nrstr(%%);
%let searchterm=%nrstr( %F% );
data out;
set sashelp.heart (where=(sex like "&searchterm")) ;
run;
Use %NRBQUOTE.
data have;
input visit $;
datalines;
%UNS%
%NO%
%HI%
;
run;
%let searchterm = %nrbquote(%UNS%);
%put &searchterm.;
data want;
set have (where = (visit like "&searchterm"));
run;
Obs visit 1 %UNS%
Why not just include the quotes in the macro variable? The macro processor ignores strings bounded on the outside by single quotes.
%let searchterm='%UNS%';
data out;
set in (where=(visit like &searchterm )) ;
run;
Or use a different search method.
%let searchterm=UNS;
data out;
set in (where=(index(visit,"&searchterm")) ;
run;
Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.