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: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 3 replies
  • 1991 views
  • 0 likes
  • 4 in conversation