<?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 Select data with some criterias in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Select-data-with-some-criterias/m-p/553937#M154074</link>
    <description>&lt;P&gt;Hello everyone !&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;(sorry for my english)&lt;/P&gt;&lt;P&gt;The tittle of the topic is not so explicit because i don't know how to tell that..&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm putting here an example&amp;nbsp; and then I explain you what i'm expecting for :&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data have ;
format id1 id2 $5. periode 2. cpt2 8. sum_cum 8.;
input id1 id2 periode cpt2 sum_cum ;
cards;
10000 00001 0 20 20
10000 00001 1 13 33
10000 00001 2 17 50
10000 00001 3 50 100
10000 00001 4 10 110
10000 00001 5 15 125
10000 00001 6 81 81
20000 00002 0 5 5
20000 00002 1 3 8
20000 00002 2 10 18
20000 00002 3 1 19
20000 00002 4 11 30
20000 00002 5 30 60
;
run ;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;CODE class=" language-sas"&gt;&amp;nbsp;&lt;/CODE&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This is the table :&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Capture.PNG" style="width: 296px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/29031iA846E2A49DA13596/image-size/large?v=v2&amp;amp;px=999" role="button" title="Capture.PNG" alt="Capture.PNG" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;- id1 is a head office&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;- id2 is an agency&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;- periode : 0 is the last month of the first trimester (01/03/2019 --&amp;gt; 31/03/2019)&lt;/P&gt;&lt;P&gt;&amp;nbsp; periode : 1 is the month before (01/02/2019 --&amp;gt; 30/02/2019)&lt;/P&gt;&lt;P&gt;&amp;nbsp; periode : 3 is the month before (01/01/2019 --&amp;gt; 31/01/2019)&lt;/P&gt;&lt;P&gt;...&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;- cpt2 is the number of survey respondents per periode&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;- sum_cum is the number cumulated of cpt2&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm trying to select data depending on 2 conditions :&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I want to know which period i have to take acording to 3 criteria :&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;- I need minimum 3 periods (so minimum periode 0, 1 and 2)&lt;/P&gt;&lt;P&gt;- I want minimum 20 respondents&lt;/P&gt;&lt;P&gt;- The maximum of periode is 36,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;If i don't have 20 respondents on the three periods, then i want to look for the next period and see if I have 20 respondents, if it is less than 20 again, see the next period, ....&lt;/P&gt;&lt;P&gt;if i don't have 20 respondents in 36 periodes then delete, good bye, we don't care haha&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;So this is what i would like to have on the output :&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data want ;
format id1 id2 $5. periode 2. cpt2 8. sum_cum 8.;
input id1 id2 periode cpt2 sum_cum ;
cards;
10000 00001 0 20 20 
10000 00001 1 13 33
10000 00001 2 17 50 
20000 00002 0 5 5
20000 00002 1 3 8
20000 00002 2 10 18
20000 00002 3 1 19
20000 00002 4 11 30
;
run ;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I don't know how write it in SAS, I don't think that it is very difficul but if you can help me, i will be very grateful !!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you !&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Onizuka&lt;/P&gt;</description>
    <pubDate>Thu, 25 Apr 2019 12:46:00 GMT</pubDate>
    <dc:creator>Onizuka</dc:creator>
    <dc:date>2019-04-25T12:46:00Z</dc:date>
    <item>
      <title>Select data with some criterias</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Select-data-with-some-criterias/m-p/553937#M154074</link>
      <description>&lt;P&gt;Hello everyone !&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;(sorry for my english)&lt;/P&gt;&lt;P&gt;The tittle of the topic is not so explicit because i don't know how to tell that..&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm putting here an example&amp;nbsp; and then I explain you what i'm expecting for :&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data have ;
format id1 id2 $5. periode 2. cpt2 8. sum_cum 8.;
input id1 id2 periode cpt2 sum_cum ;
cards;
10000 00001 0 20 20
10000 00001 1 13 33
10000 00001 2 17 50
10000 00001 3 50 100
10000 00001 4 10 110
10000 00001 5 15 125
10000 00001 6 81 81
20000 00002 0 5 5
20000 00002 1 3 8
20000 00002 2 10 18
20000 00002 3 1 19
20000 00002 4 11 30
20000 00002 5 30 60
;
run ;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;CODE class=" language-sas"&gt;&amp;nbsp;&lt;/CODE&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This is the table :&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Capture.PNG" style="width: 296px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/29031iA846E2A49DA13596/image-size/large?v=v2&amp;amp;px=999" role="button" title="Capture.PNG" alt="Capture.PNG" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;- id1 is a head office&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;- id2 is an agency&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;- periode : 0 is the last month of the first trimester (01/03/2019 --&amp;gt; 31/03/2019)&lt;/P&gt;&lt;P&gt;&amp;nbsp; periode : 1 is the month before (01/02/2019 --&amp;gt; 30/02/2019)&lt;/P&gt;&lt;P&gt;&amp;nbsp; periode : 3 is the month before (01/01/2019 --&amp;gt; 31/01/2019)&lt;/P&gt;&lt;P&gt;...&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;- cpt2 is the number of survey respondents per periode&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;- sum_cum is the number cumulated of cpt2&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm trying to select data depending on 2 conditions :&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I want to know which period i have to take acording to 3 criteria :&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;- I need minimum 3 periods (so minimum periode 0, 1 and 2)&lt;/P&gt;&lt;P&gt;- I want minimum 20 respondents&lt;/P&gt;&lt;P&gt;- The maximum of periode is 36,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;If i don't have 20 respondents on the three periods, then i want to look for the next period and see if I have 20 respondents, if it is less than 20 again, see the next period, ....&lt;/P&gt;&lt;P&gt;if i don't have 20 respondents in 36 periodes then delete, good bye, we don't care haha&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;So this is what i would like to have on the output :&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data want ;
format id1 id2 $5. periode 2. cpt2 8. sum_cum 8.;
input id1 id2 periode cpt2 sum_cum ;
cards;
10000 00001 0 20 20 
10000 00001 1 13 33
10000 00001 2 17 50 
20000 00002 0 5 5
20000 00002 1 3 8
20000 00002 2 10 18
20000 00002 3 1 19
20000 00002 4 11 30
;
run ;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I don't know how write it in SAS, I don't think that it is very difficul but if you can help me, i will be very grateful !!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you !&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Onizuka&lt;/P&gt;</description>
      <pubDate>Thu, 25 Apr 2019 12:46:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Select-data-with-some-criterias/m-p/553937#M154074</guid>
      <dc:creator>Onizuka</dc:creator>
      <dc:date>2019-04-25T12:46:00Z</dc:date>
    </item>
    <item>
      <title>Re: Select data with some criterias</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Select-data-with-some-criterias/m-p/553953#M154080</link>
      <description>&lt;P&gt;First idea: one step to find the last "periode" required to get at least 20 respondents for each id1/id2 combination, then a second step to merge the result of the first step with "have" to create "want", but then i thought why two steps, when all can be done in one - still two passes required, but i don't think this can be avoided, at least not while having in mind that there could be less than 20 respondents.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data want;
   keeped = 0;
   lastPeriode = 36;
   summed = 0;

   do until(last.id2);
      set work.have;
      by id1 id2;
      
      if keeped &amp;lt; 3 or summed &amp;lt; 20 then do;
         keeped = keeped + 1;
         lastPeriode = periode;
         summed = sum_cum;
      end;
   end;

   if summed &amp;gt;= 20 then do;
      do until(last.id2);
         set work.have;
         by id1 id2;

         if periode &amp;lt;= lastPeriode then output; 
      end;
   end;

   drop keeped lastPeriode summed;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 25 Apr 2019 13:36:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Select-data-with-some-criterias/m-p/553953#M154080</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2019-04-25T13:36:15Z</dc:date>
    </item>
    <item>
      <title>Re: Select data with some criterias</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Select-data-with-some-criterias/m-p/553959#M154085</link>
      <description>Here's how I'd get the 3 min periodes:&lt;BR /&gt;proc sql; select min(periode) into :min1 from want;&lt;BR /&gt;select min(periode) into :min2 from want where periode &amp;gt; &amp;amp;min1;&lt;BR /&gt;select min(periode) into :min3 from want where periode &amp;gt; &amp;amp;min2;&lt;BR /&gt;</description>
      <pubDate>Thu, 25 Apr 2019 13:45:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Select-data-with-some-criterias/m-p/553959#M154085</guid>
      <dc:creator>tomrvincent</dc:creator>
      <dc:date>2019-04-25T13:45:34Z</dc:date>
    </item>
    <item>
      <title>Re: Select data with some criterias</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Select-data-with-some-criterias/m-p/553964#M154089</link>
      <description>&lt;P&gt;Hello Andreas,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you for your response as fast, I have an additionnal constraint which I forgot to put on my topic because I just thought about it and which can complicate a little bit the problem.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I want the same thing but i want to keep these periods only if i have respondents in period 0 or period 1 or period 2 (during the first quarter : 01/01/2019 --&amp;gt; 31/03/2019).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Indeed,&amp;nbsp; on my real datas, I have some ids which the first period is period 9 for example (9 month before so on jun 2018).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I don't know if my reply is clear .. =/&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you again for your time , really appreciate it ! &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 25 Apr 2019 14:02:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Select-data-with-some-criterias/m-p/553964#M154089</guid>
      <dc:creator>Onizuka</dc:creator>
      <dc:date>2019-04-25T14:02:48Z</dc:date>
    </item>
    <item>
      <title>Re: Select data with some criterias</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Select-data-with-some-criterias/m-p/554172#M154156</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/269468"&gt;@Onizuka&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hello Andreas,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you for your response as fast, I have an additionnal constraint which I forgot to put on my topic because I just thought about it and which can complicate a little bit the problem.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I want the same thing but i want to keep these periods only if i have respondents in period 0 or period 1 or period 2 (during the first quarter : 01/01/2019 --&amp;gt; 31/03/2019).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Indeed,&amp;nbsp; on my real datas, I have some ids which the first period is period 9 for example (9 month before so on jun 2018).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I don't know if my reply is clear .. =/&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you again for your time , really appreciate it ! &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&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;This adds an interesting aspect to the problem &lt;span class="lia-unicode-emoji" title=":winking_face:"&gt;😉&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;The code i suggest has at least one major bug: i won't work as expected in some cases i made up. So remenbering KISS (=keep it simple stupid), i returned to the initially idea of two steps and a merge. I modified "have" to contain one case that should no be in the output, because there is no data for periode &amp;lt;= 3.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data have;
   format id1 id2 $5. periode 2. cpt2 8. sum_cum 8.;
   input id1 id2 periode cpt2 sum_cum;
   cards;
10000 00001 0 20 20
10000 00001 1 13 33
10000 00001 2 17 50
10000 00001 3 50 100
10000 00001 4 10 110
10000 00001 5 15 125
10000 00001 6 81 81
12000 00002 5 5 5
12000 00002 6 3 8
12000 00002 7 10 18
12000 00002 8 1 19
20000 00003 0 5 5
20000 00003 1 3 8
20000 00003 2 10 18
20000 00003 3 1 19
20000 00003 4 11 30
20000 00003 5 30 60
;
run;

data work.step1;
   keeped = 0;
   lastPeriode = 36;
   summed = 0;

   do until(last.id2);
      set work.have;
      by id1 id2;
      
      if first.id2 and periode &amp;gt; 3 then do;
         summed = .;
      end;      

      if not missing(summed) and (keeped &amp;lt; 3 or summed &amp;lt; 20) then do;
         keeped = keeped + 1;
         lastPeriode = periode;
         summed = sum_cum;
      end;

   end;

   if summed &amp;gt;= 20;

   keep id1 id2 lastPeriode;
run;

data work.want;
   merge work.have work.step1(in= needed);
   by id1 id2;

   if needed and Periode &amp;lt;= lastPeriode;

   drop lastPeriode;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 26 Apr 2019 06:39:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Select-data-with-some-criterias/m-p/554172#M154156</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2019-04-26T06:39:27Z</dc:date>
    </item>
    <item>
      <title>Re: Select data with some criterias</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Select-data-with-some-criterias/m-p/554201#M154167</link>
      <description>&lt;P&gt;Hello &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/15475"&gt;@andreas_lds&lt;/a&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you for you answer &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Your solution is working very well, I have to say that i'm not understanding everything on your code ..&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;More precisely, I do not understand this part :&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;      if not missing(summed) and (keeped &amp;lt; 3 or summed &amp;lt; 20) then do;
         keeped = keeped + 1;
         lastPeriode = periode;
         summed = sum_cum;
      end;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I don't understand the process for keeped and summed, because to me, keeped is always &lt;span class="lia-unicode-emoji" title=":red_heart:"&gt;❤️&lt;/span&gt; and summed &amp;lt;20 because it equals 0 ? (on the beggining of the code)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Another question, why if i'm putting the&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;do until(last.cniv1_cniv4);&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;after the set, it don't work ?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you very much and sorry for bad skills on SAS ... I though I was better in SAS that I really am haha&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Onizuka&lt;/P&gt;</description>
      <pubDate>Fri, 26 Apr 2019 09:26:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Select-data-with-some-criterias/m-p/554201#M154167</guid>
      <dc:creator>Onizuka</dc:creator>
      <dc:date>2019-04-26T09:26:00Z</dc:date>
    </item>
  </channel>
</rss>

