DATA Step, Macro, Functions and more

How to convert a list into a prefixed list?

Accepted Solution Solved
Reply
Contributor
Posts: 50
Accepted Solution

How to convert a list into a prefixed list?

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 ?


Accepted Solutions
Solution
‎11-06-2014 05:51 PM
Super User
Super User
Posts: 7,076

Re: How to convert a list into a prefixed list?

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.

View solution in original post


All Replies
Contributor
Posts: 50

Re: How to convert a list into a prefixed list?

The above varlist can also be varlist=var1 var2 var3, but the newvarlist must be a.var1, a.var2, a.var3 separated by comma.

Super Contributor
Posts: 297

Re: How to convert a list into a prefixed list?

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.;

PROC Star
Posts: 7,492

Re: How to convert a list into a prefixed list?

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;

Contributor
Posts: 50

Re: How to convert a list into a prefixed list?

very cute catx() function

Solution
‎11-06-2014 05:51 PM
Super User
Super User
Posts: 7,076

Re: How to convert a list into a prefixed list?

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.

Super Contributor
Posts: 387

Re: How to convert a list into a prefixed list?

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 Smiley Happy

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...

Contributor
Posts: 50

Re: How to convert a list into a prefixed list?

Posted in reply to ScottBass

Very nice stuff !!! Thanks.

Contributor
Posts: 50

Re: How to convert a list into a prefixed list?

Posted in reply to ScottBass

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?

Super Contributor
Posts: 387

Re: How to convert a list into a prefixed list?

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.

Super User
Posts: 10,044

Re: How to convert a list into a prefixed list?

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

🔒 This topic is solved and locked.

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

Discussion stats
  • 10 replies
  • 382 views
  • 6 likes
  • 6 in conversation