All,
I have a fairly simple question. When I have a string in open code, how can I add single / double quotes around it ?
%Let sep = %str(-);
%Let first = %str(uday) ;
%Let last = %str(guntupalli);
%Let name = %sysfunc(catx(&sep.,&first.,&last.));
%Put &name.;
I looked at discussions in the community but they use a data step. Can anybody kindly show, how I can achieve similar results in open code ?
Best
Uday
You could use add the double quotes. Single quotes are a little harder since macro references inside of them will not resolve.
Or you could use the QUOTE() function. That will also take care to double up any embedded quotes for you.
122 %put &first.&sep.&last ; uday-guntupalli 123 %put "&first.&sep.&last"; "uday-guntupalli" 124 %put %str(%')&first.&sep.&last%str(%'); 'uday-guntupalli' 125 %put %sysfunc(quote(&first.&sep.&last)); "uday-guntupalli" 126 %put %sysfunc(quote(&first.&sep.&last,%str(%'))); 'uday-guntupalli'
If you do need to add single quotes a lot (for example for pass thru SQL or strings with embedded & or % characters) then you might want to use a utility macro like: https://github.com/sasutils/macros/blob/master/squote.sas
You could use add the double quotes. Single quotes are a little harder since macro references inside of them will not resolve.
Or you could use the QUOTE() function. That will also take care to double up any embedded quotes for you.
122 %put &first.&sep.&last ; uday-guntupalli 123 %put "&first.&sep.&last"; "uday-guntupalli" 124 %put %str(%')&first.&sep.&last%str(%'); 'uday-guntupalli' 125 %put %sysfunc(quote(&first.&sep.&last)); "uday-guntupalli" 126 %put %sysfunc(quote(&first.&sep.&last,%str(%'))); 'uday-guntupalli'
If you do need to add single quotes a lot (for example for pass thru SQL or strings with embedded & or % characters) then you might want to use a utility macro like: https://github.com/sasutils/macros/blob/master/squote.sas
Its very rarely, if ever, a good idea to put quotes in macro variables. Just look at the trouble to do something simple like put a double quote in, its really more effort than its worth. Macro should note replace datastep, base SAS is the programming language, use that and you will not have all this messy code.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.