Discussion:
An easy way to generate random numbers without replacement
(too old to reply)
Susie Li
2005-07-21 13:12:33 UTC
Permalink
Hi all,

I have the need to assign a 7-digit-long ID number randomly to 500,000
subjects.

I tried to use ranuni() to do the job, and ended up with many duplicated
IDs. Is there an easier way to guarantee non-repeatable random IDs?

Susie Li
TV Guide
1211 Avenue of the Americas
New York, NY 10036
Tel 212.852.7453
Email ***@tvguide.com
Talbot Michael Katz
2005-07-21 14:09:55 UTC
Permalink
Hi, Susie.

Here is a three-step method for what you're trying to do. I'm sure
someone will come up with something more elegant:


* 1. assign uniform random numbers to all seven-digit numeric strings;
%let ranseed = 1234567; %* grow your own;
%let myds = ; %* name of your data set ;
data ran7int;
length numstr7 $7.;
keep numstr7 urn;
do i = 0 to 9999999;
numstr = put(i,z7.);
urn = ranuni(&ranseed.);
output;
end;
run;

* 2. sort step 1 data set by random number;
proc sort data = ran7int;
by urn;
run;

* 3. match step 2 output to your data set;
data &myds.;
drop urn;
merge &myds. (in = inm) ran7int (in = inr);
if inm;
run;


Hope this helps!


-- TMK --
212-460-5430
Post by Susie Li
Hi all,
I have the need to assign a 7-digit-long ID number randomly to 500,000
subjects.
I tried to use ranuni() to do the job, and ended up with many duplicated
IDs. Is there an easier way to guarantee non-repeatable random IDs?
Susie Li
TV Guide
1211 Avenue of the Americas
New York, NY 10036
Tel 212.852.7453
Dennis Diskin
2005-07-21 14:52:31 UTC
Permalink
Susie,
A simple one step solution:
=20
data a;
do i =3D 1 to 500000;
output;
end;
run;

data b;
array _s(0:9999999) $1 _temporary_;
set a;
do until(_s (c) eq ' ');
c=3Dint(ranuni(1234) * 9999999);
end;
_s(c) =3D '*';
run;

/* just to prove there are no duplicates */

proc sort data=3Db nodupkey;
by c;
run;

HTH,
Dennis Diskin
Post by Susie Li
=20
Hi all,
=20
I have the need to assign a 7-digit-long ID number randomly to 500,000
subjects.
=20
I tried to use ranuni() to do the job, and ended up with many duplicated
IDs. Is there an easier way to guarantee non-repeatable random IDs?
=20
Susie Li
TV Guide
1211 Avenue of the Americas
New York, NY 10036
Tel 212.852.7453
Ian Wakeling
2005-07-21 14:02:38 UTC
Permalink
Hi Susi,

I think the following should do the trick.

data ids(keep=id);
first=1E6;
last=9999999;
k=5E5;
do id=last to first by -1;
c=ranuni(1234);
if c<=k/(id+1-first) then do;
k=k-1;
output;
end;
end;
run;

You should of course use a different seed each
time you want a set of random ids.

Ian.


----- Original Message -----
From: "Susie Li" <***@TVGUIDE.COM>
To: <SAS-***@LISTSERV.UGA.EDU>
Sent: Thursday, July 21, 2005 2:12 PM
Subject: An easy way to generate random numbers without replacement
Post by Susie Li
Hi all,
I have the need to assign a 7-digit-long ID number randomly to 500,000
subjects.
I tried to use ranuni() to do the job, and ended up with many duplicated
IDs. Is there an easier way to guarantee non-repeatable random IDs?
Susie Li
TV Guide
1211 Avenue of the Americas
New York, NY 10036
Tel 212.852.7453
Susie Li
2005-07-21 15:48:18 UTC
Permalink
Thank you all. The problem is solved.

Susie Li
TV Guide
1211 Avenue of the Americas
New York, NY 10036
Tel 212.852.7453
Email ***@tvguide.com

-----Original Message-----
From: SAS(r) Discussion [mailto:SAS-***@LISTSERV.UGA.EDU] On Behalf Of Dennis
Diskin
Sent: Thursday, July 21, 2005 10:53 AM
To: SAS-***@LISTSERV.UGA.EDU
Subject: Re: An easy way to generate random numbers without replacement

Susie,
A simple one step solution:

data a;
do i = 1 to 500000;
output;
end;
run;

data b;
array _s(0:9999999) $1 _temporary_;
set a;
do until(_s (c) eq ' ');
c=int(ranuni(1234) * 9999999);
end;
_s(c) = '*';
run;

/* just to prove there are no duplicates */

proc sort data=b nodupkey;
by c;
run;

HTH,
Dennis Diskin
Post by Susie Li
Hi all,
I have the need to assign a 7-digit-long ID number randomly to 500,000
subjects.
I tried to use ranuni() to do the job, and ended up with many duplicated
IDs. Is there an easier way to guarantee non-repeatable random IDs?
Susie Li
TV Guide
1211 Avenue of the Americas
New York, NY 10036
Tel 212.852.7453
Loading...