Date: Tue, 3 Feb 1998 08:26:52 -0500
Reply-To: Alan Rimm-Kaufman <rimmkaufman@CRUTCHFIELD.COM>
Sender: "SAS(r) Discussion" <SAS-L@UGA.CC.UGA.EDU>
From: Alan Rimm-Kaufman <rimmkaufman@CRUTCHFIELD.COM>
Subject: Re: Drawing ramdom samples without replacement
Content-Type: text/plain; charset="us-ascii"
Rajiv --
To draw a random sample:
Use ranuni(seed) to assign a random number to each observation, then sort
the data by that random key, then take the first "k" in the sorted list.
This will give you a random sample, and by noting the see you used, you
can regenerate it as needed.
eg.
DIN input dataset
DOUT output dataset
K number of obs in random sample
SEED rng seed, use -1 for time of day
%macro randsamp(din, dout, k, seed);
data _temp;
set &din; _key = ranuni(&seed);
run;
proc sort data=_temp; by _key; run;
data &dout;
set _temp;
drop _key;
if _n_ <= &k;
run;
%mend;
If you need 5 random samples:
%randsamp(mydata, samp1, 10000, 1234);
%randsamp(mydata, samp2, 10000, 2468);
%randsamp(mydata, samp3, 10000, 1357);
%randsamp(mydata, samp4, 10000, 5678);
%randsamp(mydata, samp5, 10000, 5789);
If you need lots and lots of samples, you could
embed this in a call execute type of loop.
-----Original Message-----
From: Rajiv Kashyap [SMTP:rkk@HRTA.UMASS.EDU]
Sent: Sunday, February 01, 1998 2:15 AM
Subject: Drawing ramdom samples without replacement
I would appreciate any help with this problem. I am trying to draw 500
random samples from a database of 10000 obs for a monte carlo
experiment. I am having trouble with the logic to set up different
seeds every time I draw a sample. At present, every sample I draw has
the same observations. I should also add that I need to be able to
replicate my samples in the future if need be. Here is a sample of my
code (which is part of a larger macro):
%DO Z=1 %TO &SETS;
data temp&Z;
set database;
seed=123456;
%DO I=1 %TO &SMPSIZE;
iobs=ceil(ranuni(seed)*10000);
idobs=iobs;
set database point=iobs nobs=n;
output;
%END;
stop;
%END;
Thanks
Rajiv Kashyap
University of Massachusetts Amherst
|