DATA Step, Macro, Functions and more

add double quotes for words in string

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 111
Accepted Solution

add double quotes for words in string

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


Accepted Solutions
Solution
‎02-07-2018 06:44 AM
Super User
Super User
Posts: 9,227

Re: add double quotes for words in string

Posted in reply to Nasser_alfea

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


All Replies
Solution
‎02-07-2018 06:44 AM
Super User
Super User
Posts: 9,227

Re: add double quotes for words in string

Posted in reply to Nasser_alfea

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.

Contributor
Posts: 20

Re: add double quotes for words in string

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
Super User
Posts: 13,084

Re: add double quotes for words in string

Posted in reply to foobarbaz

@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;
Super Contributor
Posts: 320

Re: add double quotes for words in string

Posted in reply to Nasser_alfea

Hello,

 

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

%put &want.;
Super User
Posts: 10,621

Re: add double quotes for words in string

Posted in reply to Nasser_alfea
%let list_have = word1 word2 word3;
%let list="%sysfunc(prxchange(s/\s+/" "/,-1,&list_have))";

%put &list ;
PROC Star
Posts: 1,434

Re: add double quotes for words in string

Posted in reply to Nasser_alfea

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.

Super User
Posts: 10,621

Re: add double quotes for words in string

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         
☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 7 replies
  • 413 views
  • 3 likes
  • 7 in conversation