BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
gleebglorb
Calcite | Level 5

I am writing a macro that takes in a list and selects records from a dataset with those names.  

 

It will look something like this (it has to be done in a macro with a proc sql so I can't do this in a data step):

%macro test (name = John Jake Mary);

proc sql; 

select * from names where name in (%upcase(%sysfunc(tranwrd("&name",%str( ),%str(,))))); 

quit;

%mend;

 

Right now, all it is doing is just outputting this "John, Jake, Mary" but I want there to be quotations around each element in the list.  so the output looks like this "John", "Jake", "Mary"

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

Because you didn't ask it to insert the quotes into the middle.  Don't just change the spaces to commas. Change them to commas with quotes around them.

where name in (%sysfunc(tranwrd(%upcase("&name"),%str( ),",")))

You might also want to use single quotes instead, especially is you might be pushing this code into some remote database where only single quotes are used for string literals.

Also watch out for extra spaces, you might want to use COMPBL() to collapse them to single spaces.

%macro test(name);
%let name=%sysfunc(compbl(&name));
proc sql; 
select * from names 
  where name in (%sysfunc(tranwrd(%upcase(%str(%')&name%str(%')),%str( ),',')))
;
quit;
%mend;

%test(name = John Jake Mary);

View solution in original post

3 REPLIES 3
sbxkoenk
SAS Super FREQ

Change your select statement to:

select * from names where name in (%upcase(%sysfunc(tranwrd("&name",%str( ),%str(",")))));

Koen

Tom
Super User Tom
Super User

Because you didn't ask it to insert the quotes into the middle.  Don't just change the spaces to commas. Change them to commas with quotes around them.

where name in (%sysfunc(tranwrd(%upcase("&name"),%str( ),",")))

You might also want to use single quotes instead, especially is you might be pushing this code into some remote database where only single quotes are used for string literals.

Also watch out for extra spaces, you might want to use COMPBL() to collapse them to single spaces.

%macro test(name);
%let name=%sysfunc(compbl(&name));
proc sql; 
select * from names 
  where name in (%sysfunc(tranwrd(%upcase(%str(%')&name%str(%')),%str( ),',')))
;
quit;
%mend;

%test(name = John Jake Mary);
Ksharp
Super User
%macro test(name=);

proc sql; 
select * from sashelp.class where findw(symget('name'),strip(name),' ','i'); 
quit;

%mend;

%test(name = Janet Mary John)

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 3 replies
  • 1941 views
  • 0 likes
  • 4 in conversation