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

sas-innovate-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 2030 views
  • 2 likes
  • 5 in conversation