BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
DartRodrigo
Lapis Lazuli | Level 10

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

1 ACCEPTED SOLUTION

Accepted Solutions
FreelanceReinh
Jade | Level 19

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

20 REPLIES 20
Reeza
Super User
Does order matter?
DartRodrigo
Lapis Lazuli | Level 10

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

That's all, thanks

FreelanceReinh
Jade | Level 19

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")?

DartRodrigo
Lapis Lazuli | Level 10

Oh yes, this can't happen.

 

Thanks

Reeza
Super User
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.
DartRodrigo
Lapis Lazuli | Level 10

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

 

Thanks

FreelanceReinh
Jade | Level 19

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.

FreelanceReinh
Jade | Level 19

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;
Reeza
Super User
Nice!
One small suggestion, you can initialize the array in the array statement, and remove the first loop.

array x[60] (1:60);
FreelanceReinh
Jade | Level 19

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);

DartRodrigo
Lapis Lazuli | Level 10

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 ?

Reeza
Super User

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. 

 

 

DartRodrigo
Lapis Lazuli | Level 10

Yes

DartRodrigo
Lapis Lazuli | Level 10

The exactly number is 50.063.860

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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