Date: Fri, 25 May 2001 09:16:07 -0700 Dale McLerran "SAS(r) Discussion" Dale McLerran Re: Sampling numbers randomly To: fredrik.glockner@bio.uio.no text/plain

Fredrik,

Here is one solution:

data sample; seed=0; N=1000; k=250; do val=1 to 1000; x = ranuni(seed); if x<(k/n) then do; k = k-1; output; end; n = n-1; end; run;

Note that the variable VAL ranges from 1 to 1000. Only a subset of size 250 is selected. The algorithm presented above was the subject of a lively exchange during the past 2-3 weeks on SAS-L. If you are interested in learning more about this algorithm, you can look in the archives at http://www.listserv.uga.edu/archives/sas-l.html. This algorithm is computationally quite efficient, and subject to the limitations of any random number generator, will yield a simple random sample of specified size (250) from the original list. You may want to change the seed to something other than zero. When you specify zero for a seed, the system clock is employed to start the seed stream. This renders your results nonreproducible. If you need to have reproducible results, then you need to specify a nonzero seed for the random number generator.

Dale

>Date: Fri, 25 May 2001 14:53:38 +0200 >Reply-To: Fredrik Glöckner <fredrik.glockner@BIO.UIO.NO> >From: Fredrik Glöckner <fredrik.glockner@BIO.UIO.NO> >SUBJECTTo: SAS-L@LISTSERV.UGA.EDU > >Hi, > >I would like to produce a table with 250 observations. Each >observation should be a random number between 1 and 1000, and the >numbers need to be unique over the table. So, what I want to do, is >to draw 250 numbers out of 1-1000 without replacements. > >Do anybody know of an easy way to achieve this? > >Thanks for any help! > >Fredrik

--------------------------------------- Dale McLerran Fred Hutchinson Cancer Research Center mailto: dmclerra@fhcrc.org Ph: (206) 667-2926 Fax: (206) 667-5977 ---------------------------------------

------------------------------------------------------------ --== Sent via Deja.com ==-- http://www.deja.com/

Back to: Top of message | Previous page | Main SAS-L page