BookmarkSubscribeRSS Feed
kalbo
Obsidian | Level 7

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?

 

 

5 REPLIES 5
PaigeMiller
Diamond | Level 26

%let searchterm=%nrstr(%%)UNS%nrstr(%%);

--
Paige Miller
Ksharp
Super User

%let searchterm=%nrstr(  %F%  );



data out;
set sashelp.heart (where=(sex like "&searchterm")) ;
run;

maguiremq
SAS Super FREQ

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% 

https://v8doc.sas.com/sashtml/macro/z4bquote.htm  

Tom
Super User Tom
Super User

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;

 

Ksharp
Super User
Tom,
Good idea. But That is not readable for some user .

sas-innovate-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!

Register now

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 5 replies
  • 1303 views
  • 1 like
  • 5 in conversation