<?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: Random Sampling with conditions in All Things Community</title>
    <link>https://communities.sas.com/t5/All-Things-Community/Random-Sampling-with-conditions/m-p/302310#M1916</link>
    <description>&lt;PRE&gt;
Can you post an example to explain this question ?



data have;
 set sashelp.cars(keep=make model invoice);
run;

proc sql noprint;
 select int(count(distinct make)*0.1) as n,
        0.09*sum(invoice) as low,
        0.11*sum(invoice)
  into :n,: low,: high
   from have;
quit;


proc iml;
use have;
read all var{make model invoice};
close;

found=0;
key=unique(make);
do until(found);
 temp=sample(key,&amp;amp;n,'wor');
 idx=loc(element(make,temp));
 sum=sum(invoice[idx]);
 if &amp;amp;low &amp;lt; sum &amp;amp; sum &amp;lt; &amp;amp;high then do;
  found=1;print "Found:" "&amp;amp;low" sum[l=''] "&amp;amp;high";
 end;
end;

  _make=make[idx];_model=model[idx];_invoice=invoice[idx];
  create want var{_make _model _invoice};
  append;
  close;
quit;




&lt;/PRE&gt;</description>
    <pubDate>Tue, 04 Oct 2016 11:13:22 GMT</pubDate>
    <dc:creator>Ksharp</dc:creator>
    <dc:date>2016-10-04T11:13:22Z</dc:date>
    <item>
      <title>Random Sampling with conditions</title>
      <link>https://communities.sas.com/t5/All-Things-Community/Random-Sampling-with-conditions/m-p/302285#M1912</link>
      <description>&lt;P&gt;Hi there,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I would be really thankfull if you can help me with following query.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have customer list with their numbers of transactions(No of Invoices) and their spend. And I want select 10% random customers from this sample which have almost 10% spend and transactions (taking 9 to 11% to get a solution). Basically I want to keep on creating the random samples untill my conditions are met.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Following is the code.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%let Trans_Ratio=1;&lt;BR /&gt;%let Spend_Ratio=1;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;%do %until(0.09&amp;lt;=Trans_Ratio&amp;lt;=0.11 %and 0.09&amp;lt;=Spend_Ratio&amp;lt;=0.11);&lt;/P&gt;&lt;P&gt;proc surveyselect data=All_data&amp;nbsp;samprate=0.1 method=srs out=Sample_data;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;proc sql;&lt;BR /&gt;select sum(NoOfInvoices) into : Sample_Invoice from &lt;SPAN&gt;Sample_data&lt;/SPAN&gt;;&lt;BR /&gt;select sum(Revenue) into : Sample_Spend from &lt;SPAN&gt;Sample_data&lt;/SPAN&gt;;&lt;BR /&gt;select sum(NoOfInvoices) into : Tot_Invoice from All&lt;SPAN&gt;_data&lt;/SPAN&gt;;&lt;BR /&gt;select sum(Revenue) into : Tot_Spend from All&lt;SPAN&gt;_data&lt;/SPAN&gt;;&lt;BR /&gt;quit;&lt;/P&gt;&lt;P&gt;%let Trans_Ratio=%sysevalf(&amp;amp;Sample_Invoice./&amp;amp;Tot_Invoice.);&lt;BR /&gt;%let Spend_Ratio=%sysevalf(&amp;amp;Sample_Spend./&amp;amp;Tot_Spend.);&lt;/P&gt;&lt;P&gt;%end;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;But the problem is that the loop is running even if the condtitions(&lt;SPAN&gt;0.09&amp;lt;=Trans_Ratio&amp;lt;=0.11 %and 0.09&amp;lt;=Spend_Ratio&amp;lt;=0.11) are met.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;SYMBOLGEN: Macro variable TRANS_RATIO resolves to 0.09950702848001&lt;BR /&gt;SYMBOLGEN: Macro variable SPEND_RATIO resolves to 0.10113175882141&lt;BR /&gt;MLOGIC(TG_CREATION): %IF condition ((0.09&amp;lt;=&amp;amp;Trans_Ratio.&amp;lt;=0.11) %and (0.09&amp;lt;=&amp;amp;Spend_Ratio.&amp;lt;=0.11)) is FALSE&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Not sure why the if statment is FALSE.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks in advance,&lt;/P&gt;&lt;P&gt;Shailendra&lt;/P&gt;</description>
      <pubDate>Tue, 04 Oct 2016 09:06:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/All-Things-Community/Random-Sampling-with-conditions/m-p/302285#M1912</guid>
      <dc:creator>Shailendram</dc:creator>
      <dc:date>2016-10-04T09:06:15Z</dc:date>
    </item>
    <item>
      <title>Re: Random Sampling with conditions</title>
      <link>https://communities.sas.com/t5/All-Things-Community/Random-Sampling-with-conditions/m-p/302286#M1913</link>
      <description>&lt;P&gt;Your %if condition is false because the macro processor processes TEXT.&lt;/P&gt;
&lt;P&gt;0.09&amp;lt;=Trans_Ratio&amp;lt;=0.11&lt;/P&gt;
&lt;P&gt;translates to&lt;/P&gt;
&lt;P&gt;"0.09"&amp;lt;="Trans_Ratio"&amp;lt;= "0.11"&lt;/P&gt;
&lt;P&gt;Since the letter "T" can never be "smaller" than the letter(!) "0", the condition will never be true.&lt;/P&gt;
&lt;P&gt;To evaluate a numeric expression in macro, you have to use the %eval function:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro test;
%let testvar=0.1;
%if 0.09 &amp;lt; &amp;amp;testvar &amp;lt;0.11 %then %put yes1;
%if %eval(0.09 &amp;lt; &amp;amp;testvar) and %eval(&amp;amp;testvar &amp;lt;= 0.11) %then %put yes2;
%mend;
%test;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You will find that the first line does not return yes1, but the second returns yes2.&lt;/P&gt;
&lt;P&gt;You also missed the fact that to address a macro variable, you have to place the ampersand before its name.&lt;/P&gt;</description>
      <pubDate>Tue, 04 Oct 2016 09:15:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/All-Things-Community/Random-Sampling-with-conditions/m-p/302286#M1913</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2016-10-04T09:15:10Z</dc:date>
    </item>
    <item>
      <title>Re: Random Sampling with conditions</title>
      <link>https://communities.sas.com/t5/All-Things-Community/Random-Sampling-with-conditions/m-p/302288#M1914</link>
      <description>&lt;P&gt;Thanks a lot for the solution.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This works perfectly fine but this is a brute force method and might not be effective everytime. Is there any way I can create random sample which satisfy cetain conditions like in this case.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks,&lt;/P&gt;&lt;P&gt;Shailendra&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 04 Oct 2016 09:37:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/All-Things-Community/Random-Sampling-with-conditions/m-p/302288#M1914</guid>
      <dc:creator>Shailendram</dc:creator>
      <dc:date>2016-10-04T09:37:24Z</dc:date>
    </item>
    <item>
      <title>Re: Random Sampling with conditions</title>
      <link>https://communities.sas.com/t5/All-Things-Community/Random-Sampling-with-conditions/m-p/302308#M1915</link>
      <description>&lt;P&gt;I'd try something like&lt;/P&gt;
&lt;P&gt;- sort by descending spend&lt;/P&gt;
&lt;P&gt;- use 1+int(random()*10) (insert a suitable random number function) to create a number "n" between 1 and 10&lt;/P&gt;
&lt;P&gt;- take the nth record from the first 10&lt;/P&gt;
&lt;P&gt;- repeat, starting 10 records later&lt;/P&gt;
&lt;P&gt;- until you reach eof&lt;/P&gt;
&lt;P&gt;that should give you 10% of the dataset with about 10% of spend, and require only one sort + one pass&lt;/P&gt;</description>
      <pubDate>Tue, 04 Oct 2016 11:02:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/All-Things-Community/Random-Sampling-with-conditions/m-p/302308#M1915</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2016-10-04T11:02:01Z</dc:date>
    </item>
    <item>
      <title>Re: Random Sampling with conditions</title>
      <link>https://communities.sas.com/t5/All-Things-Community/Random-Sampling-with-conditions/m-p/302310#M1916</link>
      <description>&lt;PRE&gt;
Can you post an example to explain this question ?



data have;
 set sashelp.cars(keep=make model invoice);
run;

proc sql noprint;
 select int(count(distinct make)*0.1) as n,
        0.09*sum(invoice) as low,
        0.11*sum(invoice)
  into :n,: low,: high
   from have;
quit;


proc iml;
use have;
read all var{make model invoice};
close;

found=0;
key=unique(make);
do until(found);
 temp=sample(key,&amp;amp;n,'wor');
 idx=loc(element(make,temp));
 sum=sum(invoice[idx]);
 if &amp;amp;low &amp;lt; sum &amp;amp; sum &amp;lt; &amp;amp;high then do;
  found=1;print "Found:" "&amp;amp;low" sum[l=''] "&amp;amp;high";
 end;
end;

  _make=make[idx];_model=model[idx];_invoice=invoice[idx];
  create want var{_make _model _invoice};
  append;
  close;
quit;




&lt;/PRE&gt;</description>
      <pubDate>Tue, 04 Oct 2016 11:13:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/All-Things-Community/Random-Sampling-with-conditions/m-p/302310#M1916</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2016-10-04T11:13:22Z</dc:date>
    </item>
  </channel>
</rss>

