DATA Step, Macro, Functions and more

Assigning Random numbers to a given set of IDs

Accepted Solution Solved
Reply
Contributor
Posts: 44
Accepted Solution

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
15768
15768
22749
22749
22749
59574
59574
59574

 

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,044

Re: Assigning Random numbers to a given set of IDs

Posted in reply to danwarags

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;

View solution in original post


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

Re: Assigning Random numbers to a given set of IDs

Posted in reply to danwarags

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:

 

ObsID
11456
23765
335632
489564
           .            .
           .            .
           .            .
2000580058

 

My output should look something like: 

 

ObsIDRandom
11456100000
23765101997
335632101456
489564100002
.. 
.. 
.. 
2000580058102000

 

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. 

 

Thanks for your help

Super User
Posts: 10,044

Re: Assigning Random numbers to a given set of IDs

Posted in reply to danwarags


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,044

Re: Assigning Random numbers to a given set of IDs

Posted in reply to danwarags
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.

Need further help from the community? Please ask a new question.

Discussion stats
  • 4 replies
  • 266 views
  • 1 like
  • 2 in conversation