Lapis Lazuli | Level 10

## generate a random integer without duplicate

```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)?

1 ACCEPTED SOLUTION

Accepted Solutions
SAS Employee

## Re: generate a random integer without duplicate

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;``````
8 REPLIES 8
Super User

## Re: generate a random integer without duplicate

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

## Re: generate a random integer without duplicate

thanks Ksharp for your quick respons by I encounter ERROR: No BY statement was specified for a MERGE statement.
Super User

## Re: generate a random integer without duplicate

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

## Re: generate a random integer without duplicate

thanks ksharp for your quick respons but I encounter

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

SAS Employee

## Re: generate a random integer without duplicate

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

## Re: generate a random integer without duplicate

Thanks a lot !

could you please explain randseed(27513); ?

regards

Nass

SAS Employee

## Re: generate a random integer without duplicate

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

## Re: generate a random integer without duplicate

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;``````
Discussion stats
• 8 replies
• 3702 views
• 4 likes
• 4 in conversation