Date: Fri, 16 May 2003 17:09:07 +0000
Sender: "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From: Paul Dorfman <paul_dorfman@HOTMAIL.COM>
Subject: Re: Generate Random Number???
Content-Type: text/plain; format=flowed
>From: Tim Corn <tim19972003@YAHOO.COM>
>I want to generate an unique random number for each observation of a sas
>dataset. And the >range of the random number should be 1~10,000. How can I
>do this? I tried using ranuni >function, but the numbers are less than 1.
>Any suggetion? Thank in advance for your valuable >advice!
Since you file cannot have more than 10,000 observations (else the random
numbers in the [1:10000] range could not be unique), it is simplest to
permute natural numbers from 1 to 10000 using a sort by another random
variable, as Andy and others offered to do.
Now there have been array solutions by Harry and Daniel. However, as Howard
pointed out, they require a massive number of retrials, especially as the
process moves ot the end of file. A usual technique of avoiding this when
sampling without replacement is to select from the population of decreasing
size, where the element just selected is swapped with the top unused
114 data a ;
115 do var = 1 to 10000 ; output ; end ;
116 run ;
NOTE: The data set WORK.A has 10000 observations and 1 variables.
NOTE: DATA statement used (Total process time):
117 data r ;
118 array nat ( 1 : 10000 ) _temporary_ ;
119 if _n_ = 1 then do _n_ = 1 to n ;
120 nat (_n_) = _n_ ;
121 end ;
122 set a nobs = n ;
123 _n_ = ceil ( ranuni(1) * n ) ;
124 random = nat (_n_) ;
125 nat (_n_) = nat (n) ;
126 n +- 1 ;
127 run ;
NOTE: There were 10000 observations read from the data set WORK.A.
NOTE: The data set WORK.R has 10000 observations and 2 variables.
Paul M. Dorfman
Help STOP SPAM with the new MSN 8 and get 2 months FREE*