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

Hello,

 

I have got a list of words and I would like get these words with double quotes.

%let list_have = word1 word2 word3 ...; the max number of word is 10

I would like to get the macro variable list_want that would contain  "word1" "word2" "word3" ...

 

thanks in advance for your help

 

regards

 

Nasser

1 ACCEPTED SOLUTION

Accepted Solutions
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Really not the best idea to be using a text find and replace system for data.  You can do it simply (shows the power of using the real programming language rather than find/replace) in a data _null_;

%let list_have = word1 word2 word3;
data _null_; length temp $2000; temp=cat('"',tranwrd("&list_have."," ",'" "'),'"'); call symput('list_want',temp); run;
%put &list_want.;

You can likely do the same if you create some ghastly complex macro code which is indecipherable and falls over every other run.

View solution in original post

7 REPLIES 7
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Really not the best idea to be using a text find and replace system for data.  You can do it simply (shows the power of using the real programming language rather than find/replace) in a data _null_;

%let list_have = word1 word2 word3;
data _null_; length temp $2000; temp=cat('"',tranwrd("&list_have."," ",'" "'),'"'); call symput('list_want',temp); run;
%put &list_want.;

You can likely do the same if you create some ghastly complex macro code which is indecipherable and falls over every other run.

foobarbaz
Obsidian | Level 7

Within a macro you can also use the following which allows direct assignment in a let statement.    This is adapted from http://documentation.sas.com/?docsetId=mcrolref&docsetTarget=p1n2i0ewaj1zian1ria5579z1zjh.htm&docset...

 

%let list_have = word1 word2 word3;

%macro quotelist(varlist);
   %let word_cnt=%sysfunc(countw(&varlist)); 
   %let list=;
   %do i = 1 %to &word_cnt; 
     %let temp=%qscan(%bquote(&varlist),&i);
     %let list = &list %bquote("&temp.");
   %end;
   &list;
%mend;

%let new = %quotelist(&list_have);

%put &=new;
Regards,
Cameron | Selerity
ballardw
Super User

@foobarbaz I reached a similar result. However note the difference in behavior with:

 

%macro addquote (wordlist);
/* word list is expected to be space delimited and no word containing
quotes or special symbols*/
%let result=;
%do i=1 %to %sysfunc(countw(&wordlist));
   %let result = &result %sysfunc(quote(%sysfunc(scan(&wordlist,&i))));
%end;
&result
%mend;

%put %addquote(var1 var2 var3);

data junk;
   set sashelp.class;
   where name in (%addquote(Alice Carol John Philip));
run;

and

 

data junk;
   set sashelp.class;
   where name in (%quotelist(Alice Carol John Philip));
run;
gamotte
Rhodochrosite | Level 12

Hello,

 

%let list_have=word1 word2 word3;
%let want="%sysfunc(tranwrd(&list_have.,%str( ),%str(%" %")))";

%put &want.;
Ksharp
Super User
%let list_have = word1 word2 word3;
%let list="%sysfunc(prxchange(s/\s+/" "/,-1,&list_have))";

%put &list ;
Quentin
Super User

I'm a big fan of Richard DeVenezia's utility macro %SepList.  It does a lot of list management, including adding quotes to items, changing delimiters, adding prefixes...

 

https://www.devenezia.com/downloads/sas/macros/index.php?m=seplist

 

83   %let list_have = word1 word2 word3 ;
84
85   %put %seplist(&list_have,indlm=%str( ),dlm=%str( ),nest=QQ) ;
"word1" "word2" "word3"
86
87   %put %seplist(&list_have,indlm=%str( ),dlm=%str( ),nest=Q) ;
'word1' 'word2' 'word3'
88
89   %put %seplist(&list_have,indlm=%str( ),dlm=|) ;
word1|word2|word3
90
91   %put %seplist(&list_have,indlm=%str( ),dlm=%str(, ),prefix=_) ;
_word1, _word2, _word3

I changed my personal version slightly, so that it %unquotes the value returned, and the default value for DLM is set to INDLM.

BASUG is hosting free webinars Next up: Mike Sale presenting Data Warehousing with SAS April 10 at noon ET. Register now at the Boston Area SAS Users Group event page: https://www.basug.org/events.
Ksharp
Super User

I am big fan of PRXCHANGE for this kind of problem which is origined from @Patrick

 


 1          OPTIONS NONOTES NOSTIMER NOSOURCE NOSYNTAXCHECK;
 68         
 69         %let list_have = word1 word2 word3;
 70         %let list="%sysfunc(prxchange(s/\s+/" "/,-1,&list_have))";
 71         %put &list ;
 "word1" "word2" "word3"
 72         
 73         
 74         %let list_have = word1 word2 word3;
 75         %let list=%bquote('%sysfunc(prxchange(s/\s+/' '/,-1,&list_have))');
 76         %put &list ;
 'word1' 'word2' 'word3'
 77         
 78         
 79         %let list_have = word1 word2 word3;
 80         %let list=%sysfunc(prxchange(s/\s+/|/,-1,&list_have));
 81         %put &list ;
 word1|word2|word3
 82         
 83         %let list_have = word1 word2 word3;
 84         %let list=%sysfunc(prxchange(s/(\w+)/_$1%str(,)/,-1,&list_have));
 85         %put &list ;
 _word1, _word2, _word3,
 86         
 87         
 88         OPTIONS NONOTES NOSTIMER NOSOURCE NOSYNTAXCHECK;
 99         

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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.

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
  • 7 replies
  • 20089 views
  • 6 likes
  • 7 in conversation