<?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: How do I sample from a dataset &amp;quot;while&amp;quot; total is less than &amp;quot;n&amp;quot; in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/How-do-I-sample-from-a-dataset-quot-while-quot-total-is-less/m-p/603382#M16939</link>
    <description>True, PROC OR may be a better option then.</description>
    <pubDate>Mon, 11 Nov 2019 22:41:13 GMT</pubDate>
    <dc:creator>Reeza</dc:creator>
    <dc:date>2019-11-11T22:41:13Z</dc:date>
    <item>
      <title>How do I sample from a dataset "while" total is less than "n"</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-do-I-sample-from-a-dataset-quot-while-quot-total-is-less/m-p/603251#M16915</link>
      <description>&lt;P&gt;Suppose you have an some empirical data in a SAS data set, say 3 numbers: {1, 3, 9}&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'd like to draw numbers from that data until the sum is greater than some number (say 15) and save those numbers.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Is there an easy (and efficient) way to do that in SAS? 9.4 Would I be better off switching to Rcpp?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 11 Nov 2019 15:48:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-do-I-sample-from-a-dataset-quot-while-quot-total-is-less/m-p/603251#M16915</guid>
      <dc:creator>dschmidt</dc:creator>
      <dc:date>2019-11-11T15:48:13Z</dc:date>
    </item>
    <item>
      <title>Re: How do I sample from a dataset "while" total is less than "n"</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-do-I-sample-from-a-dataset-quot-while-quot-total-is-less/m-p/603278#M16918</link>
      <description>&lt;P&gt;Hello&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/218034"&gt;@dschmidt&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Try this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
do x=1, 3, 9;
  output;
end;
run;

%let t=15; /* threshold */

data want;
call streaminit(27182818);
do _n_=1 to 999999 until(s&amp;gt;&amp;amp;t); /* "999999" to prevent infinite loop */
  p=rand('integer',n);
  set have point=p nobs=n;
  s+x;
  output;
end;
stop;
drop s; /* optional */
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If the 'integer' distribution is not yet available in your maintenance release, use&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;p=ceil(n*rand('uniform'));&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Edit: The subject line ("while total is less than") is not exactly equivalent to the requirement described as "&lt;SPAN&gt;until the sum is greater than," which I used in the UNTIL condition of the DO loop.&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 11 Nov 2019 16:49:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-do-I-sample-from-a-dataset-quot-while-quot-total-is-less/m-p/603278#M16918</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2019-11-11T16:49:08Z</dc:date>
    </item>
    <item>
      <title>Re: How do I sample from a dataset "while" total is less than "n"</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-do-I-sample-from-a-dataset-quot-while-quot-total-is-less/m-p/603337#M16926</link>
      <description>&lt;P&gt;A 'simple' approach is to randomly sort the data and take the first N records until the sum is over the N.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let N_Limit = 40;


data _random;
set sashelp.class;

*add random variable;
order = rand('integer', 1, 100);

run;

proc sort data=_random;
by order;
run;

data want;
set _random;

retain total;

if total&amp;lt;&amp;amp;N_Limit then output;
*increment total after to allow the total to go over;
total+age;

run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/218034"&gt;@dschmidt&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Suppose you have an some empirical data in a SAS data set, say 3 numbers: {1, 3, 9}&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I'd like to draw numbers from that data until the sum is greater than some number (say 15) and save those numbers.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Is there an easy (and efficient) way to do that in SAS? 9.4 Would I be better off switching to Rcpp?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 11 Nov 2019 20:16:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-do-I-sample-from-a-dataset-quot-while-quot-total-is-less/m-p/603337#M16926</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2019-11-11T20:16:32Z</dc:date>
    </item>
    <item>
      <title>Re: How do I sample from a dataset "while" total is less than "n"</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-do-I-sample-from-a-dataset-quot-while-quot-total-is-less/m-p/603354#M16933</link>
      <description>&lt;P&gt;This is a convenient approach for the situation of sampling &lt;EM&gt;without&lt;/EM&gt; replacement.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;My suggestion was rather aimed at sampling &lt;EM&gt;with&lt;/EM&gt; replacement -- in view of &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/218034"&gt;@dschmidt&lt;/a&gt;'s example of obtaining a sum &amp;gt;15 from the 3-element set {1, 3, 9}.&lt;/P&gt;</description>
      <pubDate>Mon, 11 Nov 2019 20:57:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-do-I-sample-from-a-dataset-quot-while-quot-total-is-less/m-p/603354#M16933</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2019-11-11T20:57:50Z</dc:date>
    </item>
    <item>
      <title>Re: How do I sample from a dataset "while" total is less than "n"</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-do-I-sample-from-a-dataset-quot-while-quot-total-is-less/m-p/603382#M16939</link>
      <description>True, PROC OR may be a better option then.</description>
      <pubDate>Mon, 11 Nov 2019 22:41:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-do-I-sample-from-a-dataset-quot-while-quot-total-is-less/m-p/603382#M16939</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2019-11-11T22:41:13Z</dc:date>
    </item>
    <item>
      <title>Re: How do I sample from a dataset "while" total is less than "n"</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-do-I-sample-from-a-dataset-quot-while-quot-total-is-less/m-p/603581#M16960</link>
      <description>&lt;P&gt;Thanks! What OR procedure would you refer to? I had that thought, but didn't see any obvious candidates.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;If it helps, this is something that I'd like to repeat many times for many lists (with replacement)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;E.g.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;For iteration 1:num_iterations&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; For lists 1:num_lists&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Draw a random value from the list if count &amp;lt; threshold&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Count = count + random draw from the list&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 12 Nov 2019 13:55:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-do-I-sample-from-a-dataset-quot-while-quot-total-is-less/m-p/603581#M16960</guid>
      <dc:creator>dschmidt</dc:creator>
      <dc:date>2019-11-12T13:55:27Z</dc:date>
    </item>
  </channel>
</rss>

