<?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: Counting with conditions in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Counting-with-conditions/m-p/724001#M224749</link>
    <description>Very nice.&lt;BR /&gt;&lt;BR /&gt;Thanks.</description>
    <pubDate>Fri, 05 Mar 2021 20:01:53 GMT</pubDate>
    <dc:creator>Sk1_SAS</dc:creator>
    <dc:date>2021-03-05T20:01:53Z</dc:date>
    <item>
      <title>Counting with conditions</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Counting-with-conditions/m-p/723961#M224738</link>
      <description>&lt;P&gt;Hi team,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This is my base, the table is already sorted by ID and flag.&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 temp;
infile DATALINES dsd missover;
input DATE:DDMMYY10. value ID FLAG;
FORMAT DATE DDMMYY10.;
CARDS;
15/05/2020,9360,1,1 
22/05/2020,9360,1,1 
07/08/2020,9880,1,1 
21/08/2020,9100,1,1 
21/08/2020,9880,1,2 
25/08/2020,9800,1,3 
26/08/2020,9100,1,4 
24/11/2020,9569,2,1 
17/12/2020,9600,2,1 
17/12/2020,9600,2,2 
17/12/2020,9600,2,3 
17/12/2020,9600,2,4 
17/12/2020,9600,2,5 
17/12/2020,9600,2,6 
17/12/2020,9600,2,7 
27/05/2020,9020,3,1 
10/06/2020,9519,3,1 
16/06/2020,9553,3,1 
18/06/2020,9553,3,2 
25/06/2020,9500,3,1 
10/07/2020,9498,3,1 
13/07/2020,9498,3,2 
16/07/2020,9075,3,3 
17/07/2020,9663,3,4 
17/07/2020,9368,3,5 
20/07/2020,9663,3,6 
21/07/2020,9663,3,7 
22/07/2020,9600,3,8 
;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;First i need to SUM value, if the flag is consecutive and need to be consecutive to 4 or more.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;And if is possible, i need to count if the flag is consecutive and need to be consecutive to 4 or more.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The result that i need:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Sk1_SAS_0-1614970096995.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/55512i4B2EB7A4BB8D378F/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Sk1_SAS_0-1614970096995.png" alt="Sk1_SAS_0-1614970096995.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks!!!!&lt;/P&gt;</description>
      <pubDate>Fri, 05 Mar 2021 18:48:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Counting-with-conditions/m-p/723961#M224738</guid>
      <dc:creator>Sk1_SAS</dc:creator>
      <dc:date>2021-03-05T18:48:26Z</dc:date>
    </item>
    <item>
      <title>Re: Counting with conditions</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Counting-with-conditions/m-p/723966#M224740</link>
      <description>&lt;P&gt;You've shown what you want for runs constant flag valules of length 4&amp;nbsp; &amp;nbsp;(sum of value over the 4 obs).&amp;nbsp; And you've shown what you want for shorter runs (carry forward the previous sum).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Questions:&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;If flag=1 the only value that can have such runs?&lt;/LI&gt;
&lt;LI&gt;What if a run has a length &amp;gt;4?&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;&lt;STRIKE&gt;Additional question:&amp;nbsp; Does flag always proceed in ascending order.&amp;nbsp; I.e. can flag=1 reappear after flag&amp;gt;1?&amp;nbsp;&lt;/STRIKE&gt; Oops, I see the answer.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 05 Mar 2021 19:05:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Counting-with-conditions/m-p/723966#M224740</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2021-03-05T19:05:03Z</dc:date>
    </item>
    <item>
      <title>Re: Counting with conditions</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Counting-with-conditions/m-p/723969#M224741</link>
      <description>&lt;P&gt;i need to count and sum if the FLAG is consecutive to 4 or more, like this:&lt;/P&gt;&lt;P&gt;1 2 3 4&amp;nbsp;&lt;/P&gt;&lt;P&gt;1 2 3 4 5&lt;/P&gt;&lt;P&gt;and etc...&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;if the flag is consecutive but less than 4, like:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;1 2 3&lt;/P&gt;&lt;P&gt;1 2&lt;/P&gt;&lt;P&gt;does not need to be done.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Yes,&amp;nbsp;flag always proceed in ascending order, and that example can happen.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 05 Mar 2021 19:10:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Counting-with-conditions/m-p/723969#M224741</guid>
      <dc:creator>Sk1_SAS</dc:creator>
      <dc:date>2021-03-05T19:10:04Z</dc:date>
    </item>
    <item>
      <title>Re: Counting with conditions</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Counting-with-conditions/m-p/723994#M224748</link>
      <description>&lt;P&gt;I think this will do:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data temp;
infile DATALINES dsd missover;
input DATE:DDMMYY10. value ID FLAG;
FORMAT DATE DDMMYY10.;
CARDS;
15/05/2020,9360,1,1 
22/05/2020,9360,1,1 
07/08/2020,9880,1,1 
21/08/2020,9100,1,1 
21/08/2020,9880,1,2 
25/08/2020,9800,1,3 
26/08/2020,9100,1,4 
24/11/2020,9569,2,1 
17/12/2020,9600,2,1 
17/12/2020,9600,2,2 
17/12/2020,9600,2,3 
17/12/2020,9600,2,4 
17/12/2020,9600,2,5 
17/12/2020,9600,2,6 
17/12/2020,9600,2,7 
27/05/2020,9020,3,1 
10/06/2020,9519,3,1 
16/06/2020,9553,3,1 
18/06/2020,9553,3,2 
25/06/2020,9500,3,1 
10/07/2020,9498,3,1 
13/07/2020,9498,3,2 
16/07/2020,9075,3,3 
17/07/2020,9663,3,4 
17/07/2020,9368,3,5 
20/07/2020,9663,3,6 
21/07/2020,9663,3,7 
22/07/2020,9600,3,8 
;
run;

data temp;
  set temp;
  if flag=1 then grp+1; /* create groups */ 
run;

data want;
 do _N_ = 1 by 1 until(last.grp);
   set temp;
   by grp;
   sum + value;
   cnt + 1;
 end;

 if cnt &amp;lt; 4 then call missing(sum, cnt);

 do _N_ = 1 to _N_;
  set temp;
  output;
 end;

 call missing(sum, cnt);
run;

proc print;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Bart&lt;/P&gt;</description>
      <pubDate>Fri, 05 Mar 2021 19:52:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Counting-with-conditions/m-p/723994#M224748</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2021-03-05T19:52:41Z</dc:date>
    </item>
    <item>
      <title>Re: Counting with conditions</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Counting-with-conditions/m-p/724001#M224749</link>
      <description>Very nice.&lt;BR /&gt;&lt;BR /&gt;Thanks.</description>
      <pubDate>Fri, 05 Mar 2021 20:01:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Counting-with-conditions/m-p/724001#M224749</guid>
      <dc:creator>Sk1_SAS</dc:creator>
      <dc:date>2021-03-05T20:01:53Z</dc:date>
    </item>
  </channel>
</rss>

