BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Nasser_DRMCP
Lapis Lazuli | Level 10
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

1 ACCEPTED SOLUTION

Accepted Solutions
ChanceTGardener
SAS Employee

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;

View solution in original post

8 REPLIES 8
Ksharp
Super User
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;
Nasser_DRMCP
Lapis Lazuli | Level 10
thanks Ksharp for your quick respons by I encounter ERROR: No BY statement was specified for a MERGE statement.
Ksharp
Super User
Add one more option:


options mergenoby=nowarn;
data want;
merge have id(drop=rand);
run;
Nasser_DRMCP
Lapis Lazuli | Level 10

thanks ksharp for your quick respons but I encounter

ERROR: No BY statement was specified for a MERGE statement.

ChanceTGardener
SAS Employee

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;
Nasser_DRMCP
Lapis Lazuli | Level 10

Thanks a lot !

could you please explain randseed(27513); ?

regards

Nass

 

ChanceTGardener
SAS Employee

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). 

FreelanceReinh
Jade | Level 19

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;

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 8 replies
  • 3688 views
  • 4 likes
  • 4 in conversation