I have a varlist given by
%let varlist=%str(var1, var2, var3);
In practice, there may be 100 such variables in the list instead of just three.
How to convert the above macro variable varlist into something like
newvarlist =a.var1, a.var2, a.var3
efficiently ?
You can do it with a loop, but if the input is well formed then TRANWRD will work also.
%let list=x y z ;
%let newlist=A.%sysfunc(tranwrd(&list,%str( ),%str( A.))) ;
If it is not well behaved you can clean it up with %SYSFUNC(COMPBL()) for space delimited lists.
The above varlist can also be varlist=var1 var2 var3, but the newvarlist must be a.var1, a.var2, a.var3 separated by comma.
If this is a true representation of the variable names you are using you could use:
%LET VARLIST=%SYSFUNC(TRANWRD(%STR(VAR1, VAR2, VAR3),VAR,A.VAR));
%PUT *************************** &VARLIST.;
I'm sure there are numerous ways to accomplish what you want. Here is one way:
%let varlist=%str(var1, var2, var3);
data _null_;
length newvarlist $32767.;
i=1;
do while (scan("&varlist,",i," ,") ne "");
newvarlist=catx(', ',newvarlist,'a.'||scan("&varlist,",i," ,"));
i+1;
end;
call symput('newvarlist',newvarlist);
run;
%put &newvarlist;
very cute catx() function
You can do it with a loop, but if the input is well formed then TRANWRD will work also.
%let list=x y z ;
%let newlist=A.%sysfunc(tranwrd(&list,%str( ),%str( A.))) ;
If it is not well behaved you can clean it up with %SYSFUNC(COMPBL()) for space delimited lists.
Download Richard Devenezia's seplist macro: http://www.devenezia.com/downloads/sas/macros/index.php?m=seplist
Or, download my slightly modified version: SAS/seplist.sas at master · scottbass/SAS · GitHub
Also download my loop macro from my GitHub site
Lots of good macros on both sites
Then, you can do:
%let varlist=%str(var1, var2, var3);
%put %seplist(%superq(varlist),indlm=%str(,),prefix=a.);
* I hate commas in a macro variable list. That's why I have to use %superq above.;
* Don't use them, let %seplist add them later at "run time" ;
%let varlist=
var1
var2
var3
;
%let newlist = %seplist(&varlist,prefix=a.);
%put &=newlist;
* Or, use %loop, which is more complex, but can convert both ways ;
%macro newlist;
%* convert oldlist to newlist ;
%if (&__iter__) gt 1 %then %str(,);
a.&word
%mend;
%macro oldlist;
%* convert newlist to oldlist ;
%scan(&word,2,.)
%mend;
%let varlist=varA varB varC;
%let newlist = %loop(&varlist,mname=newlist);
%put &=newlist;
%let oldlist = %loop(%superq(newlist),dlm=%str(,),mname=oldlist);
%put &=oldlist;
Here are the results:
3 %let varlist=%str(var1, var2, var3);
4 %put %seplist(%superq(varlist),indlm=%str(,),prefix=a.);
a.var1,a.var2,a.var3
5
6 * I hate commas in a macro variable list. ;
7 * Don't use them, let %seplist add them later at "run time" ;
8
9 %let varlist=
10 var1
11 var2
12 var3
13 ;
14 %let newlist = %seplist(&varlist,prefix=a.);
15 %put &=newlist;
NEWLIST=a.var1,a.var2,a.var3
16
17 * Or, use %loop, which is more complex, but can convert both ways ;
18 %macro newlist;
19 %* convert oldlist to newlist ;
20 %if (&__iter__) gt 1 %then ,;
21 a.&word
22 %mend;
23 %macro oldlist;
24 %* convert newlist to oldlist ;
25 %scan(&word,2,.)
26 %mend;
27
28 %let varlist=varA varB varC;
29 %let newlist = %loop(&varlist,mname=newlist);
30 %put &=newlist;
NEWLIST=a.varA , a.varB , a.varC
31
32 %let oldlist = %loop(%superq(newlist),dlm=%str(,),mname=oldlist);
33 %put &=oldlist;
OLDLIST=varA varB varC
I would just use %seplist unless I needed to convert back to the original list and for some reason didn't preserve the original list.
%seplist is REALLY handy for converting between data step and SQL syntax. I used it ALL the time.
HTH...
Very nice stuff !!! Thanks.
I don't quite understand what the following is doing:
%macro newlist;
%* convert oldlist to newlist ;
%if (&__iter__) gt 1 %then %str(,);
a.&word
%mend;
%macro oldlist;
%* convert newlist to oldlist ;
%scan(&word,2,.)
%mend;
What is the purpose of the above for using %loop()?
What does "&" mean in %put &=newlist?
Read the macro headers, including the numerous use cases.
Read the SAS 9.3 (or 9.4) documentation on the %put macro statement.
Post back then if you still have questions.
Or you don't need %SYSFUNC(COMPBL()) for multiple blanks.
%let list=x y z ;
%let newlist=a.%sysfunc(prxchange(s/\s+/%str(,a.)/,-1,&list));
%put &newlist ;
Xia Keshan
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.
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.