## Help with Do loop through a list of variables

Solved
Super Contributor
Posts: 506

# Help with Do loop through a list of variables

Hi Everyone,

I have a dataset with few groups of variables (say a,b,c  and x,y,z).

I want to run a specific code body for each combination of the group( say a and x ; a and y; a and z   ;;; b and x..... c and z)

the code body is long, so I cannot wrap it up to use array for (a b c) and array for (x y z).

So I think about a big marco :

Do variable1 = a,b,c;

Do varibale 2= x, y, z;

Code body;

end;

end;

I don't know how to set up the Do with list of variables as above.

Below is my sample data and the sample code to calculate the number of time value fall in the range of a_x.

HHC

data have;

input value a b c x y z;

datalines;

4 1 2 3 11 12 13

6 5 6 7 88 89 90

8 2 3 4 66 67 68

10 9 8 7 77 78 79

;

run;

data want;

set have nobs=totalobs;

drop vl;

N_between_a_x=0;

i+1;

do j=i to totalobs;

set have (keep=value rename=(value=vl))point=j;

if a<=vl<=x then N_between_a_x=N_between_a_x+1;

end;

run;

Accepted Solutions
Solution
‎11-29-2014 10:01 PM
Super User
Posts: 8,127

## Re: Help with Do loop through a list of variables

No. Or at least not without using some modern functions DOSUB.   But even if you could I am pretty sure that you wouldn't want to.

You could learn macro coding to allow you to generate code in a loop.

Or possibly you can just re-format your data so that you can do all of the analyses at once using BY group processing.

What is it that you actually want to do?  You description is not really clear.

If the question is can I pass in two sets of variables and have it re-run the same analysis using different combinations of the variables then perhaps macro is what you want .

%macro myloop(list1,list2);

%local i j var1 var2 ;

%do i=1 %to %sysfunc(countw(&list1,,s));

%do j=1 %to %sysfunc(countw(&list2,,s));

%let var1=%scan(&list1,&j,,s);

%let var2=%scan(&list2,&j,,s);

proc reg .... ;

model &var1 = &var2 ....

run;

....

%end;

%end;

%mend myloop;

%myloop(a b c,x y z);

All Replies
Super Contributor
Posts: 3,176

## Re: Help with Do loop through a list of variables

When you define two ARRAYs,  one each for the two variable groups, then you can have the nested DO/END to iterate through the second-ARRAY (variable) set for each of the first-ARRAY variables; the key consideration here is that you must have two different control variables, different for each of the DO/END code controls.

Scott Barry

SBBWorks, Inc.

Super User
Posts: 10,787

## Re: Help with Do loop through a list of variables

You need to clarify it more . What output do you need ?  Does also contain combination  ( a  x  y ) ?

What are you counting for those combinations ?

Xia Keshan

Posts: 4,743

## Re: Help with Do loop through a list of variables

It's not in your narrative but your sample code indicates that you also want to compare all observations with each other, eg. A (obs 1) with X (obs 5). Is this the case?

Super Contributor
Posts: 506

## Re: Help with Do loop through a list of variables

My problem is for 2 factor combination say (a,x). not (a,x,y) or (a,b,x,y).

Also, it analyzes the combination of variable within the same record/obs. So it will not work on A(obs1) with X(obs 5)

HHC

Posts: 4,743

## Re: Help with Do loop through a list of variables

Still not sure what you're actually asking for. Below my interpretation of what you're after.

data have;

input value a b c x y z;

datalines;

4 1 2 3 11 12 13

6 5 6 7 88 89 90

8 2 3 4 66 67 68

10 9 8 7 77 78 79

;

run;

data want;

set have;

array arA {*} a b c;

array arX {*} x y z;

do _iA=1 to dim(arA);

do _iX=1 to dim(arX);

if arA[_iA]<=value<=arX[_iX] then

N_between_a_x=sum(N_between_a_x,1);

end;

end;

run;

Super Contributor
Posts: 506

## Re: Help with Do loop through a list of variables

Thank you, Tom and Patrick for your help.

I wonder if the below is a correct approach to run many codes for each combination lower (a b c) and upper ( x y z)?

HHC

data want ;

array lower a b c ;

array upper x y z ;

array counts (25,25) _temporary_;

set have end=eof ;

do i=1 to dim(lower) ;

do j=1 to dim(upper);

*---------------------------------------

Can I put a lot of code in this section say:

data a1; set b1;

....

run;

data abc1; set zyz1;

....

run;

....

*----------------------------------------;

end;

end;

Solution
‎11-29-2014 10:01 PM
Super User
Posts: 8,127

## Re: Help with Do loop through a list of variables

No. Or at least not without using some modern functions DOSUB.   But even if you could I am pretty sure that you wouldn't want to.

You could learn macro coding to allow you to generate code in a loop.

Or possibly you can just re-format your data so that you can do all of the analyses at once using BY group processing.

What is it that you actually want to do?  You description is not really clear.

If the question is can I pass in two sets of variables and have it re-run the same analysis using different combinations of the variables then perhaps macro is what you want .

%macro myloop(list1,list2);

%local i j var1 var2 ;

%do i=1 %to %sysfunc(countw(&list1,,s));

%do j=1 %to %sysfunc(countw(&list2,,s));

%let var1=%scan(&list1,&j,,s);

%let var2=%scan(&list2,&j,,s);

proc reg .... ;

model &var1 = &var2 ....

run;

....

%end;

%end;

%mend myloop;

%myloop(a b c,x y z);

Super Contributor
Posts: 506

## Re: Help with Do loop through a list of variables

It is complicate for sure.

I am thinking about make the main code a separate file with some macro variable.

Then create another file to do the DO i and DO j to pass the value into the main code.

Thank you.

HHC

Super User
Posts: 8,127

## Re: Help with Do loop through a list of variables

Not clear what you want to count. It sounds like you want to count how many times the variable VALUE is between the value of A and X.

So with 4 input observations that count should be between 0 and 4.

data have;

input value a b c x y z;

datalines;

4 1 2 3 11 12 13

6 5 6 7 88 89 90

8 2 3 4 66 67 68

10 9 8 7 77 78 79

;

run;

data want ;

array lower a b c ;

array upper x y z ;

array counts (25,25) _temporary_;

set have end=eof ;

do i=1 to dim(lower) ;

do j=1 to dim(upper) ;

counts(i,j) = sum(counts(i,j),(lower(i) <= value <= upper(j)) );

end;

end;

if eof then do ;

do i=1 to dim(lower);

do j=1 to dim(upper);

count =counts(i,j);

var1=vname(lower(i));

var2=vname(upper(j));

output;

keep count var1 var2 ;

end;

end;

end;

run;

data _null_;

set want ;

put (_all_) (=);

run;

count=4 var1=a var2=x

count=4 var1=a var2=y

count=4 var1=a var2=z

count=4 var1=b var2=x

count=4 var1=b var2=y

count=4 var1=b var2=z

count=3 var1=c var2=x

count=3 var1=c var2=y

count=3 var1=c var2=z

🔒 This topic is solved and locked.