DATA Step, Macro, Functions and more

create a list of macro variable to loop thru

Accepted Solution Solved
Reply
Contributor
Posts: 46
Accepted Solution

create a list of macro variable to loop thru

[ Edited ]

Hello all,

 

I have a data sets that I'd like to to a proc with and I want to loop through the columns

looks like this

  apple  orange seeds red
obs1 12 12 12 12
obs2 2 2 2 2
obs3 -8 -8 -8 -8

 

what I wanted to do is this 

 

%do i=1 to end;

proc glm;

model y=&varlist&i;

quit;

%end;

 

so I can run the proc against each of the variable in my variable list.

 

I can easily create the &varlist. using proc sql, but I wanted to run the proc glm on each of the vaeriable list (not all of them)

can anyone show me how to create this &varlist&i?

 

use symput? 

 

Thank you!

 

 

More explicitly,

%let names=Susan Mary Alice Zeta Queen......Evin;

I wanted to have sometime embeded with it, say &i.

so I can have names get recognized as &&list.&i, e.g. &names1.=susan &names2.=Mary, &names3.=zeta, &names4.=Queen and so on.

 

a extended quesiton will be, is there any doc that I can find such tutorial or logic to build such mechanism for broder &&&type of marco use?

 

Thank you!!


Accepted Solutions
Solution
‎12-07-2016 02:09 PM
Super User
Posts: 5,082

Re: create a list of macro variable to loop thru

Let's suppose this is your starting point:

 

%let names=Susan Mary Alice Zeta Queen......Evin;

 

Inside a macro, you can add code like this to split &NAMES into separate variables:

 

%do i=1 %to %sysfunc(countw(&names));

   %let name&i = %scan(&names, &i, %str( ));

%end;

 

While this can be done, it also can add unnecessary complexity to the subsequent processing.  So it's a choice you need to make.

View solution in original post


All Replies
Super User
Posts: 5,082

Re: create a list of macro variable to loop thru

Super User
Posts: 17,824

Re: create a list of macro variable to loop thru

See the example on this page, under:

A macro program for repeating a procedure multiple times

http://www.ats.ucla.edu/stat/sas/seminars/sas_macros_introduction/

Contributor
Posts: 46

Re: create a list of macro variable to loop thru

Thank you!

Contributor
Posts: 46

Re: create a list of macro variable to loop thru

Thank you, I actually saw this solution before I posted my question.

This works but I was looking more for that how to create indexes for my macro variable contains multiple variables names in there.

 

for example,

 

%let names=Susan Mary Alice Zeta Queen......Evin;

I wanted to have sometime embeded with it, say &i.

so I can have names get recognized as &&list.&i, e.g. &names1.=susan &names2.=Mary, &names3.=zeta, &names4.=Queen and so on.

 

a extended quesiton will be, is there any doc that I can find such tutorial or logic to build such mechanism for broder &&&type of marco use?

 

Thank you!!

 

 

 

Super User
Posts: 10,500

Re: create a list of macro variable to loop thru

Can you show exactly what code the macro is supposed to generate for a given macro variable containing a list?

How you are actually using things helps provide better responses.

 

Contributor
Posts: 46

Re: create a list of macro variable to loop thru

Thank you for getting back to me.  I just had it answered below.

I appreciate your help and hope to learn again from you next time!

Solution
‎12-07-2016 02:09 PM
Super User
Posts: 5,082

Re: create a list of macro variable to loop thru

Let's suppose this is your starting point:

 

%let names=Susan Mary Alice Zeta Queen......Evin;

 

Inside a macro, you can add code like this to split &NAMES into separate variables:

 

%do i=1 %to %sysfunc(countw(&names));

   %let name&i = %scan(&names, &i, %str( ));

%end;

 

While this can be done, it also can add unnecessary complexity to the subsequent processing.  So it's a choice you need to make.

☑ This topic is SOLVED.

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

Discussion stats
  • 7 replies
  • 282 views
  • 0 likes
  • 4 in conversation