DATA Step, Macro, Functions and more

Do loop

Accepted Solution Solved
Reply
Regular Contributor
Posts: 212
Accepted Solution

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

 

Thanks in advance,

 

Rodrigo


Accepted Solutions
Solution
‎11-26-2015 12:17 PM
Trusted Advisor
Posts: 1,117

Re: Do loop

Posted in reply to DartibaliRodrigo

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;

View solution in original post


All Replies
Super User
Posts: 19,822

Re: Do loop

Posted in reply to DartibaliRodrigo
Does order matter?
Regular Contributor
Posts: 212

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

Trusted Advisor
Posts: 1,117

Re: Do loop

Posted in reply to DartibaliRodrigo

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: 212

Re: Do loop

Posted in reply to FreelanceReinhard

Oh yes, this can't happen.

 

Thanks

Super User
Posts: 19,822

Re: Do loop

Posted in reply to DartibaliRodrigo
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: 212

Re: Do loop

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

 

Thanks

Trusted Advisor
Posts: 1,117

Re: Do loop

Posted in reply to DartibaliRodrigo

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
Trusted Advisor
Posts: 1,117

Re: Do loop

Posted in reply to DartibaliRodrigo

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: 19,822

Re: Do loop

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

array x[60] (1:60);
Trusted Advisor
Posts: 1,117

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: 212

Re: Do loop

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

 

just asking !

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

Super User
Posts: 19,822

Re: Do loop

[ Edited ]
Posted in reply to DartibaliRodrigo

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: 212

Re: Do loop

Yes

Regular Contributor
Posts: 212

Re: Do loop

The exactly number is 50.063.860

🔒 This topic is solved and locked.

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

Discussion stats
  • 20 replies
  • 378 views
  • 10 likes
  • 3 in conversation