## Do loop

Solved
Regular Contributor
Posts: 222

# Do loop

Hi,

I have the following code sent to me by Reeza:

``````data random;
array s(60) s1-s60 (60*0);
array p(6) p1-p6;
do i=1 to 6;
do while(p(i)=.);
p(i)=floor(rand('uniform')*60+1);
if s(p(i))=0 then s(p(i))=1;
else p(i)=.;
end;
end;
keep p1-p6;
call sortn(of p(*));
run;
``````

This code create six random numbers from 1 to 60 without duplicating them.

Now i need a loop to create 60 lines with diferente possibilities, the sequence (can't,may not) duplicate

What i mean: if the sequence is: 1,2,3,4,5,6 the next line may not duplicate, so it can be any other sequence,

1,2,3,4,5,7 or 13,24, 25,30,57,58

Rodrigo

Accepted Solutions
Solution
‎11-26-2015 12:17 PM
Posts: 1,244

## Re: Do loop

Here is the code:

``````data temp(keep=r x1-x6);
array x[60];
seed=3141592653;
do i=1 to 60;
x[i]=i;
end;
do n=1 to 100;
call rancomb(seed, 6, of x[*]);
r=ranuni(577215);
output;
end;
drop i n;
run;

proc sort data=temp nodupkey;
by x1-x6;
run;

proc sort data=temp;
by r;
run;

data want;
set temp(obs=60 drop=r);
run;
``````

All Replies
Super User
Posts: 23,667

## Re: Do loop

Does order matter?
Regular Contributor
Posts: 222

## Re: Do loop

No, order doesn't metter but what metter is: if one sequence exists, it can not be duplicated.

That's all, thanks

Posts: 1,244

## Re: Do loop

I'm not sure that you understood Reeza's question the same way as I did: Would you regard the two sequences 1,2,3,4,5,6 and 1,2,3,4,6,5 as duplicates (because they contain the same numbers and "order doesn't matter")?

Regular Contributor
Posts: 222

## Re: Do loop

Oh yes, this can't happen.

Thanks

Super User
Posts: 23,667

## Re: Do loop

I'm not sure I see an easy clear way to do this. I'd generate a large sample, ie if I need 60, generate 100. Remove duplicates and then take the top 60.
Regular Contributor
Posts: 222

## Re: Do loop

Hum, it's ok but what i need is sixty different possibilities of sequences.

Thanks

Posts: 1,244

## Re: Do loop

No problem: As Reeza wrote, you create 100 sequences and then remove (possible) duplicates and then select 60 of these. I am just writing a code example for you.

Solution
‎11-26-2015 12:17 PM
Posts: 1,244

## Re: Do loop

Here is the code:

``````data temp(keep=r x1-x6);
array x[60];
seed=3141592653;
do i=1 to 60;
x[i]=i;
end;
do n=1 to 100;
call rancomb(seed, 6, of x[*]);
r=ranuni(577215);
output;
end;
drop i n;
run;

proc sort data=temp nodupkey;
by x1-x6;
run;

proc sort data=temp;
by r;
run;

data want;
set temp(obs=60 drop=r);
run;
``````
Super User
Posts: 23,667

## Re: Do loop

Nice!
One small suggestion, you can initialize the array in the array statement, and remove the first loop.

array x[60] (1:60);
Posts: 1,244

## Re: Do loop

Thanks, Reeza. I was looking at Example 1 in the online help for CALL RANCOMB, where they initialize their array the inelegant way: array x x1-x5 (1 2 3 4 5);

Regular Contributor
Posts: 222

## Re: Do loop

Amazing man, that't exactly what i need, but if i would need 50.063.860 ()

How much should i need to create to get the top 50.063.860 ?

Super User
Posts: 23,667

## Re: Do loop

[ Edited ]

Is that 50 million?

EDIT: That's 60 choose 6 - ie all possible combinations.

If you want that, there's other, faster ways to obtain it than this method.

Regular Contributor
Posts: 222

## Re: Do loop

Yes

Regular Contributor
Posts: 222

## Re: Do loop

The exactly number is 50.063.860

🔒 This topic is solved and locked.