Data have ; infile datalines ; input name $20. ; datalines ; Henry Robert Martin Salah Carla Jamal David Jeff Fred Samuel Nasser Kylian Lionel Barbara run ;
hello,
I have a team with 14 persons. I would like to give a number to each person by random.
I tried this: person_number = ceil(ranuni(1) * 14) ; but the number 8 appaers 3 times.
how to do that without duplicates (each person should have a uniq number between 1 and 14)?
thanks in adance
If you have SAS/IML licensed, it's very straightforward with the sample() function.
proc iml;
call randseed(27513);
random_digit = t(sample(1:14, 14, "WOR"));
create rand var {random_digit};
append;
close rand;
quit;
data want;
merge have rand;
run;
Data have ; infile datalines ; input name $20. ; datalines ; Henry Robert Martin Salah Carla Jamal David Jeff Fred Samuel Nasser Kylian Lionel Barbara ; run ; data id; set have; n+1; rand=rand('uniform'); keep n rand; run; proc sort data=id;by rand;run; data want; merge have id(drop=rand); run;
thanks ksharp for your quick respons but I encounter
ERROR: No BY statement was specified for a MERGE statement.
If you have SAS/IML licensed, it's very straightforward with the sample() function.
proc iml;
call randseed(27513);
random_digit = t(sample(1:14, 14, "WOR"));
create rand var {random_digit};
append;
close rand;
quit;
data want;
merge have rand;
run;
Thanks a lot !
could you please explain randseed(27513); ?
regards
Nass
A seed is a number that initializes the random number generator. By keeping it fixed (to 27513 in this case), it ensures the sampling results stay the same each time the code is ran.
Any seed >0 ensures repeatable results when the code is re-ran. I chose 27513 arbitrarily (it's the zip code for the SAS campus in Cary) so that we'd both get the same results.
If you want the results to change every time the code is ran, set call randseed(0).
Hello @Nasser_DRMCP and later readers of this thread,
Here is another option using just one DATA step:
data want;
array _r[14] _temporary_ (1:14);
if _n_=1 then do;
_iorc_=2718; /* random seed */
call ranperm(_iorc_, of _r[*]);
end;
set have;
person_number=_r[_n_];
run;
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
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.
Ready to level-up your skills? Choose your own adventure.