|
Max,
Mark and Dan have already directed you to regarding the algorithm. The code
produces the same result as if you had assigned random numbers to the
original file, sorted the file according to those assignments, and only
taken the first 10.
A simple test to show that it indeed does that might be:
data pool;
do pop=1 to 1000;
output;
end;
run;
data sample (drop=obsleft samplesize);
samplesize=10;
obsleft=totobs;
do while(samplesize>0);
pickit+1;
if ranunit(5)<samplesize/obsleft then do;
set pool point=pickit nobs=totobs;
output;
samplesize=samplesize-1;
end;
obsleft+(-1);
end;
stop;
run;
data test2;
do pop=1 to 1000;
x=ranuni(5);
output;
end;
run;
proc sort data=test2;
by x;
run;
It IS Saturday, isn't it?
Art
------
On Sat, 16 Jul 2011 15:14:38 -0700, Daniel Nordlund
<djnordlund@FRONTIER.COM> wrote:
>see comments below
>
>> -----Original Message-----
>> From: SAS(r) Discussion [mailto:SAS-L@LISTSERV.UGA.EDU] On Behalf Of Mark
>> Miller
>> Sent: Saturday, July 16, 2011 2:44 PM
>> To: SAS-L@LISTSERV.UGA.EDU
>> Subject: Re: the "standard" code generating a sample
>>
>> Max,
>>
>> Correction to my previous post
>> The sample procedure is for SRS w Replacement
>> due to use of POINT rather than sequential reads
>> Not sure if Knuth covers this variant since
>> I no longer have a copy to consult.
>>
>> ... Mark
>>
>
>
>
>No, the algorithm is for SRS without replacement. The pointer value pickit
runs consecutively from 1 to (potentially) totalobs. It never repeats.
>
>Hope this is helpful,
>
>Dan
>
>Daniel Nordlund
>Bothell, WA USA
>
>
>
>
>
>
>
>> On 7/16/2011 2:11 PM, bbser 2009 wrote:
>> > I just realized I should have asked more specific questions about the
>> > standard code.
>> > So here we go:
>> > 1. What's the purpose of the if expression?
>> > 2. why do we need to this statement obsleft+(-1);
>> > 3. How does this algorithm avoid picking up the same obs more than
once?
>> >
>> > Thank you.
>> >
>> > Max
>> > (Maaxx)
>> >
>> > -----Original Message-----
>> > From: bbser 2009 [mailto:bbser2009@gmail.com]
>> > Sent: July-16-11 4:47 PM
>> > To: 'SAS-L@LISTSERV.UGA.EDU'
>> > Subject: the "standard" code generating a sample
>> >
>> > Greetings!
>> >
>> > Basically speaking, the code below comes from the advanced
certification
>> > book.
>> > This code are suppose to get a sample of size 10 from the pool.
>> > But I do not understand the algorithm, especially the if expression.
>> > Could you please explain the algorithm to me? Thank you very much!
>> >
>> > Max
>> > (Maaxx)
>> >
>> > ======================
>> >
>> > data pool;
>> > do pop=1 to 1000;
>> > output;
>> > end;
>> > run;
>> >
>> > data sample(drop=obsleft samplesize);
>> > samplesize=10;
>> > obsleft=totobs;
>> > do while(samplesize>0);
>> > pickit+1;
>> > if ranuni(0)<samplesize/obsleft then
>> > do;
>> > set pool point=pickit nobs=totobs;
>> > output;
>> > samplesize=samplesize-1;
>> > end;
>> > obsleft+(-1);
>> > end;
>> > stop;
>> > run;
>> >
>> > proc print data=sample;
>> > run;
|