Solved
Contributor
Posts: 71

# Assigning a random numeric ID within a series of numbers

I am trying to figure out if there is a way to automate the process of assigning random IDs to a set of 50 records. I want to randomly assign a number between 1 and 100 to the 50 records - is there a SAS function that do this for me? I have found some random ID creation code, but what I have found creates decimals and alphanumeric IDs and I cannot specify a specific range. Thank you for any help you can provide.

Accepted Solutions
Solution
‎08-08-2012 04:57 PM
Regular Contributor
Posts: 241

## Re: Assigning a random numeric ID within a series of numbers

I like patrick's. Here is another using an array in order to keep track of which new id has already been assigned. hth

/* a test data set with 50 obs */
data one;
do oldId = 1 to 50;
output;
end;
run;

/* attach a randomly selected id from [1,100] */
%let seed=1234567;
data two;
array isAssigned[1:100] _temporary_ (100*0);
set one;
do while (1);
newId = ceil(100*ranuni(&seed));
if isAssigned[newId] then continue;
isAssigned[newId] = 1;
leave;
end;
run;

/* check */
proc print data=two;
run;
/*
old    new
Obs     Id     Id
1      1     69
2      2     52
3      3     90
4      4      1
...
50     50     45
*/

All Replies
Posts: 5,535

## Re: Assigning a random numeric ID within a series of numbers

Here is a simple way :

/* Create the set of IDs that you want, */
data tmp;
do id = 1 to 100;
order=ranuni(-1);
output;
end;
run;

/* sort them in random order, */
proc sort data=tmp out=ids(keep=id); by order; run;

/* associate the IDs to your observations. */

data want;
set yourData;
set ids;
run;

PG

PG
Super User
Posts: 6,781

## Re: Assigning a random numeric ID within a series of numbers

Your assumptions and constraints will affect the solution.  Here, I assumed that you want integers only from 1 to 100, and it is OK for two observations to have the same random ID assigned.  If that doesn't reflect what you need, we can revisit:

data want;

set have;

id = ceil(100*ranuni(0));

run;

Note that you will not be able to replicate your results this way.  Using 0 as the seed to a random number generator creates a stream of numbers based on the time clock as the program begins.  Again, if that is an issue, we can revisit what you need.

Good luck.

Posts: 4,736

## Re: Assigning a random numeric ID within a series of numbers

If you have SAS/Stat licensed then you could also use PROC PLAN (thanks Data Null; for the time when you've advertised this Proc to us).

I believe below usage of PROC PLAN generates a random row of the numbers 1 to 100 without repetition.

proc plan seed=0;
factors id=100 /noprint;
output out=IDs;
run;

data have;
do MyVar=1 to 50;
output;
end;
run;

data want;
set IDs;
set have;
run;

Solution
‎08-08-2012 04:57 PM
Regular Contributor
Posts: 241

## Re: Assigning a random numeric ID within a series of numbers

I like patrick's. Here is another using an array in order to keep track of which new id has already been assigned. hth

/* a test data set with 50 obs */
data one;
do oldId = 1 to 50;
output;
end;
run;

/* attach a randomly selected id from [1,100] */
%let seed=1234567;
data two;
array isAssigned[1:100] _temporary_ (100*0);
set one;
do while (1);
newId = ceil(100*ranuni(&seed));
if isAssigned[newId] then continue;
isAssigned[newId] = 1;
leave;
end;
run;

/* check */
proc print data=two;
run;
/*
old    new
Obs     Id     Id
1      1     69
2      2     52
3      3     90
4      4      1
...
50     50     45
*/

Contributor
Posts: 71

## Re: Assigning a random numeric ID within a series of numbers

Thank you Patrick and Chang! The answers you provided were extremely helpful.

Chang -

I am not very familiar with the seed, ceil and ranuni functions in SAS, could you explain a little of what they are performing? the resources I found cleared it up some, but I thought perhaps you may be able to help explain it and help me understand better. Thank you for all of your help.

🔒 This topic is solved and locked.