- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
options mergenoby=nowarn;
data want;
merge have id(drop=rand);
run;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
thanks ksharp for your quick respons but I encounter
ERROR: No BY statement was specified for a MERGE statement.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Thanks a lot !
could you please explain randseed(27513); ?
regards
Nass
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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).
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;