<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: simple random sample using data step in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/simple-random-sample-using-data-step/m-p/667068#M199694</link>
    <description>&lt;P&gt;Second best thing to a mathematical proof, and sometimes more convincing: a simulation. Consider&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* Iterate the sample selection procedure, omitting the reads
    but recording which observations would be selected */
 data subset;
 call streaminit(768766);
 do rep = 1 to 100000;
   SampSize=3;
   totObs=10;
   ObsLeft=TotObs;
   pickIt = 0;
   do while(SampSize&amp;gt;0 and ObsLeft&amp;gt;0);
      PickIt+1;
      if rand("uniform") &amp;lt; SampSize/ObsLeft then do;
         ObsPicked=PickIt;
         output;
         SampSize=SampSize-1;
      end;
      ObsLeft=ObsLeft-1;
      end;
   end;
keep rep obsPicked;
run;

title "Get the size of the samples";
proc sql;
select sampleSize, count(*) as nbReps
from 
( select rep, count(*) as sampleSize from subset
  group by rep )
group by sampleSize;
quit; 

title "Check if every obs has an equal probability of selection";
title2 "Equal proportions test";
proc freq data=subset;
table obsPicked / chisq;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Sun, 05 Jul 2020 22:45:44 GMT</pubDate>
    <dc:creator>PGStats</dc:creator>
    <dc:date>2020-07-05T22:45:44Z</dc:date>
    <item>
      <title>simple random sample using data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/simple-random-sample-using-data-step/m-p/667037#M199684</link>
      <description>&lt;P&gt;I'm going through my notes from SAS Programming 3 and I'm having trouble making sense of the example code.&lt;/P&gt;&lt;P&gt;The goal here is to select random observations without duplicates.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data subset(drop=ObsLeft SampSize);
   SampSize=10;
   ObsLeft=TotObs;
   do while(SampSize&amp;gt;0 and ObsLeft&amp;gt;0);
      PickIt+1;
      if ranuni(0)&amp;lt;SampSize/ObsLeft then do;
         ObsPicked=PickIt;
         set orion.orderfact point=PickIt nobs=TotObs;
         output;
         SampSize=SampSize-1;
      end;
      ObsLeft=ObsLeft-1;
   end;
   stop;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I think I understand why they use&amp;nbsp;&lt;CODE class=" language-sas"&gt;ranuni(0)&amp;lt;SampSize/ObsLeft&lt;/CODE&gt;&amp;nbsp;to determine whether an observation is selected.&amp;nbsp; It compares a random number to the ratio of remaining samples to remaining observations.&amp;nbsp; In order to give each observation the same chance of being selected?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;But my question is whether this is actually giving each sample the same probability of being selected.&amp;nbsp; If the ratio changes with each iteration then doesn't each sample have a different probability of being selected?&amp;nbsp; Im not sure that it matters but the definition given for "simple random sampling" was "equal probability" of being selected.&amp;nbsp; Couldn't you just use a fixed ratio of initial sampsize/totobs?&lt;/P&gt;&lt;P&gt;My other question is what is preventing this from reaching the end of the dataset before outputting the required number of samples?&amp;nbsp; Theoretically, the random number could never match the condition for output before it gets to the end.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The next page in my notes is about PROC SURVEYSELECT so i'm assuming this example is just academic, but those things were bugging me.&lt;/P&gt;</description>
      <pubDate>Sun, 05 Jul 2020 19:26:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/simple-random-sample-using-data-step/m-p/667037#M199684</guid>
      <dc:creator>Hale</dc:creator>
      <dc:date>2020-07-05T19:26:19Z</dc:date>
    </item>
    <item>
      <title>Re: simple random sample using data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/simple-random-sample-using-data-step/m-p/667040#M199686</link>
      <description>&lt;P&gt;First I'm going to throw out an opinion. People like to write these complicated Do While loops, so that they can say they did it all in one step (one DATA step). To me that's a false goal, and the same process could be performed with much more readable code by assigning a random value to each observation, then sorting, then selecting the top N (whatever number of observations they want). What I just described is what I would have used before I learned about PROC SURVEYSELECT. Perhaps this Do While loop method will actually run faster than what I described, I don't know, but the possibility of coding error by creating the Do While loop is increased. In addition, you mention that you think the probabilities are not constant, so there is a possibility that there is also a math error being made when you create a Do While loop algorithm. (And to answer your specific question: I haven't tried to prove this, it may be that when you compute the probability of an observation being selected conditional on earlier events having happened, it does produce the correct probability of being selected, but like I said I haven't tried to prove this).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;And so I end with another opinion that I have expressed many times before in this forum. SAS has done the work to make PROC SURVEYSELECT provide the desired sample. Not only that, they have tested it, debugged it, documented it and this code has been proven in many many bazillions of real world applications. And also, you (or your company or university) is paying actual money to get the benefit of SAS's efforts, which includes code that does what you want, code that has been tested, debugged, documented and performed properly in real-world applications. There's no need to design your own algorithm in a complicated DATA step to do this, its inefficient to do so, and your own code may not give the right answer.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 05 Jul 2020 19:49:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/simple-random-sample-using-data-step/m-p/667040#M199686</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2020-07-05T19:49:05Z</dc:date>
    </item>
    <item>
      <title>Re: simple random sample using data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/simple-random-sample-using-data-step/m-p/667068#M199694</link>
      <description>&lt;P&gt;Second best thing to a mathematical proof, and sometimes more convincing: a simulation. Consider&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* Iterate the sample selection procedure, omitting the reads
    but recording which observations would be selected */
 data subset;
 call streaminit(768766);
 do rep = 1 to 100000;
   SampSize=3;
   totObs=10;
   ObsLeft=TotObs;
   pickIt = 0;
   do while(SampSize&amp;gt;0 and ObsLeft&amp;gt;0);
      PickIt+1;
      if rand("uniform") &amp;lt; SampSize/ObsLeft then do;
         ObsPicked=PickIt;
         output;
         SampSize=SampSize-1;
      end;
      ObsLeft=ObsLeft-1;
      end;
   end;
keep rep obsPicked;
run;

title "Get the size of the samples";
proc sql;
select sampleSize, count(*) as nbReps
from 
( select rep, count(*) as sampleSize from subset
  group by rep )
group by sampleSize;
quit; 

title "Check if every obs has an equal probability of selection";
title2 "Equal proportions test";
proc freq data=subset;
table obsPicked / chisq;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 05 Jul 2020 22:45:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/simple-random-sample-using-data-step/m-p/667068#M199694</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2020-07-05T22:45:44Z</dc:date>
    </item>
    <item>
      <title>Re: simple random sample using data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/simple-random-sample-using-data-step/m-p/667076#M199698</link>
      <description>&lt;P&gt;This code looks like a variation of Method 3 from SAS Sample&amp;nbsp;&lt;A href="https://support.sas.com/kb/24/722.html" target="_self"&gt;Sample 24722: Simple random sample without replacement&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;There is some code inline comment which explains why each row gets selected with the same probability.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I've actually asked the exactly same question many years ago for the referenced Method 3 code and got great answers. ...Tried to find the discussion but had no luck with my searches.&lt;/P&gt;</description>
      <pubDate>Mon, 06 Jul 2020 02:19:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/simple-random-sample-using-data-step/m-p/667076#M199698</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2020-07-06T02:19:52Z</dc:date>
    </item>
    <item>
      <title>Re: simple random sample using data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/simple-random-sample-using-data-step/m-p/667086#M199701</link>
      <description>&lt;P&gt;thats cool.&amp;nbsp; it definitely works, but im still not quite sure why.&lt;/P&gt;&lt;P&gt;if the first iteration is 3/10=.3 and it doesnt find a match&lt;/P&gt;&lt;P&gt;the next iteration is 3/9=.33 and it does find a match&lt;/P&gt;&lt;P&gt;the next iteration is 2/8=.25 ...and so on.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;So the probability doesn't stay the same, but i guess over 10000 repetitions it evens out?&lt;/P&gt;&lt;P&gt;I suppose it doesn't matter, at the end of the day its still a random sample.&lt;/P&gt;</description>
      <pubDate>Mon, 06 Jul 2020 13:05:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/simple-random-sample-using-data-step/m-p/667086#M199701</guid>
      <dc:creator>Hale</dc:creator>
      <dc:date>2020-07-06T13:05:13Z</dc:date>
    </item>
    <item>
      <title>Re: simple random sample using data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/simple-random-sample-using-data-step/m-p/667087#M199702</link>
      <description>I agree completely, and i like your alternate solution.</description>
      <pubDate>Mon, 06 Jul 2020 01:54:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/simple-random-sample-using-data-step/m-p/667087#M199702</guid>
      <dc:creator>Hale</dc:creator>
      <dc:date>2020-07-06T01:54:13Z</dc:date>
    </item>
    <item>
      <title>Re: simple random sample using data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/simple-random-sample-using-data-step/m-p/667088#M199703</link>
      <description>&lt;P&gt;We did have a mathematical proof on SAS-L many years ago, but I have lost track of the author and the post.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here's why your reasoning is not quite right.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Selecting 3 out of 10, first observation has a 30% chance of being selected.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Second observation needs a more complex formula based on whether or not first observation was selected:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;30% * 2/9 + 70% * 3/9 = 30%&lt;/P&gt;</description>
      <pubDate>Mon, 06 Jul 2020 02:16:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/simple-random-sample-using-data-step/m-p/667088#M199703</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2020-07-06T02:16:22Z</dc:date>
    </item>
    <item>
      <title>Re: simple random sample using data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/simple-random-sample-using-data-step/m-p/667194#M199732</link>
      <description>&lt;P&gt;I think this selection method is usually credited to Fan, Muller, and Rezucha (1962). Their proof is on page 392.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Fan, C.T, Muller, M. E., and Rezucha, I. (1962).&amp;nbsp;"Development of Sampling Plans by Using Sequential (Item by Item) Selection Techniques and Digital Computers." &lt;EM&gt;Journal of the American Statistical Association &lt;/EM&gt;57: 387-402.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 06 Jul 2020 14:26:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/simple-random-sample-using-data-step/m-p/667194#M199732</guid>
      <dc:creator>Watts</dc:creator>
      <dc:date>2020-07-06T14:26:18Z</dc:date>
    </item>
  </channel>
</rss>

