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