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
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;
data _null_;
do i=1 to 6;
x=round(60*ranuni(123434));
put x;
end;
run;
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;
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;
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;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.