Solved
Contributor
Posts: 44

# Assigning Random numbers to a given set of IDs

Hello all,

I am trying to assign random numbers to a given set of IDs. The reason for assinging random numbers to the IDs is to de-identify the information from the IDs. There should not be any relationship between the IDs and the Random numbers generated from them. For example:

if I have a set of IDs as:

 IDs Random Numbers 1 5768 1 5768 2 2749 2 2749 2 2749 5 9574 5 9574 5 9574

The random numbers in the above tables are just the examples. These numbers can be any given number until and unless there is no relationship between the IDs and Random numbers. The only intention for decoding the IDs is to secure the information. Again, these random numbers cannot be used to lookback the IDs.

Thank you

Accepted Solutions
Solution
‎08-26-2016 01:43 PM
Super User
Posts: 10,766

## Re: Assigning Random numbers to a given set of IDs

```
data have;
input id;
cards;
1
1
2
2
2
3
4
5
5
;
run;
data want;
set have;
by id;
retain ran;
if first.id then ran=ceil(1000000000000*ranuni(0));
run;
```

All Replies
Solution
‎08-26-2016 01:43 PM
Super User
Posts: 10,766

## Re: Assigning Random numbers to a given set of IDs

```
data have;
input id;
cards;
1
1
2
2
2
3
4
5
5
;
run;
data want;
set have;
by id;
retain ran;
if first.id then ran=ceil(1000000000000*ranuni(0));
run;
```
Contributor
Posts: 44

## Re: Assigning Random numbers to a given set of IDs

[ Edited ]

Thanks Ksharp. This code works. If I wanted to assign random numbers in particular range how should I modify the code. For example for a given set of 2000 IDs I want to assign random numbers in the range 100000- 102000. How should I do this. For example:

 Obs ID 1 1456 2 3765 3 35632 4 89564 . . . . . . 2000 580058

My output should look something like:

 Obs ID Random 1 1456 100000 2 3765 101997 3 35632 101456 4 89564 100002 . . . . . . 2000 580058 102000

In the above example, the number of observations in the dataset are 2000. The Random numbers are assigned such that they are in particular range i.e 100000-102000.

Super User
Posts: 10,766

## Re: Assigning Random numbers to a given set of IDs

```

data have;
do obs=1 to 2000;
output;
end;
run;
data temp;
set have;
ran=ranuni(0);
run;
proc rank data=temp out=want;
var ran;
ranks r;
run;
data want;
set want;
random=100001+r;
drop r;
run;

```
Super User
Posts: 10,766

## Re: Assigning Random numbers to a given set of IDs

```Last code is not right. Try this one:

data have;
do obs=1 to 2000;
output;
end;
run;
data temp;
set have;
ran=ranuni(0);
run;
proc rank data=temp out=want;
var ran;
ranks r;
run;
data want;
set want;
random=100000+r;
drop r;
run;

proc sql;
select count(distinct random),max(random),min(random) from want;quit;

```
☑ This topic is solved.