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"
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);
Change your select statement to:
select * from names where name in (%upcase(%sysfunc(tranwrd("&name",%str( ),%str(",")))));
Koen
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);
%macro test(name=);
proc sql;
select * from sashelp.class where findw(symget('name'),strip(name),' ','i');
quit;
%mend;
%test(name = Janet Mary John)
April 27 – 30 | Gaylord Texan | Grapevine, Texas
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!
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.