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

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 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

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

 

View solution in original post

2 REPLIES 2
Tom
Super User Tom
Super User

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

 

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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-wordmark-2025-midnight.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


Register now!

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
  • 2 replies
  • 4114 views
  • 1 like
  • 3 in conversation