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 .

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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
  • 1199 views
  • 1 like
  • 5 in conversation