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-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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