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 need to create 6 number between 01 to 60;

Just like: 45,04,10,54,11,48;

And change each time i run the code.

 

Any numbers, but without repeating them.

 

Tks

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

Theoretically any random number generator has the possibility of duplicates, albeit a small possibility. 

 

Here's a method that would check and ensure it was not a duplicate. There are probably more efficient methods and looking forward to see what others pick.

 

 

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;
 

 

View solution in original post

4 REPLIES 4
slchen
Lapis Lazuli | Level 10
data _null_;
   do i=1 to 6;
      x=round(60*ranuni(123434));
      put x;
   end;
run; 
Reeza
Super User

Theoretically any random number generator has the possibility of duplicates, albeit a small possibility. 

 

Here's a method that would check and ensure it was not a duplicate. There are probably more efficient methods and looking forward to see what others pick.

 

 

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;
 

 

billfish
Quartz | Level 8

A solution amongst others.

 

condition 1 -> And change each time i run the code.
condition 2 -> Any numbers, but without repeating them.

 


data t_a(keep=zRandom);
   array xRand(6);
   x = 0;
   do until (x=6);
      y = ceil(60*ranuni(-3));      *** covers condition 1 negative argument ***;
      if not(y in xRand) then do; *** covers condition 2 ***;
         x+1;
         xRand(x)=y;
      end;
   end;

   do  i = 1 to 6;
     zRandom = xRand(i);
     output;
   end;

run;

PGStats
Opal | Level 21

To select randomly k distinct numbers out of n, use rancomb

 

data test;
array x{60};
retain x (1:60);
retain seed (-1);
do i = 1 to 10;
    call rancomb(seed, 6, of x{*});
    put x1-x6;
    output;
    end;
keep x1-x6;
run;

 

PG
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
  • 4 replies
  • 2014 views
  • 2 likes
  • 5 in conversation