BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Lulus
Obsidian | Level 7

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

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

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

7 REPLIES 7
Reeza
Super User

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/

Lulus
Obsidian | Level 7

Thank you!

Lulus
Obsidian | Level 7

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

 

 

 

ballardw
Super User

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.

 

Lulus
Obsidian | Level 7

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!

Astounding
PROC Star

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.

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 7 replies
  • 21400 views
  • 1 like
  • 4 in conversation