<?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: Total consecutive decreases in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Total-consecutive-decreases/m-p/296890#M62282</link>
    <description>Thank you!</description>
    <pubDate>Wed, 07 Sep 2016 03:28:14 GMT</pubDate>
    <dc:creator>LaiQ</dc:creator>
    <dc:date>2016-09-07T03:28:14Z</dc:date>
    <item>
      <title>Total consecutive decreases</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Total-consecutive-decreases/m-p/296471#M62132</link>
      <description>&lt;P&gt;Hi all,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I need to find the number of months with consecutive decreases, which is 5 in this case (3 months then 2 months), ignoring those which are not consecutive:&lt;/P&gt;&lt;PRE&gt;ID  month  sales    lag(sales)    decreasecount (1 if lag(sales) &amp;gt; sales)
1   Jan    100         .               0
1   Feb    120        100              0
1   Mar     90        120              1 
1   Apr     80         90              1
1   May     70         80              1
1   Jun    110         70              0 
1   Jul     90        110              1
1   Aug    150         90              0
1   Sep    100        150              1
1   Oct     90        100              1
1   Nov    110         90              0
1   Dec    100        110              1&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Appreciate any help and thank you for the time.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 05 Sep 2016 08:33:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Total-consecutive-decreases/m-p/296471#M62132</guid>
      <dc:creator>LaiQ</dc:creator>
      <dc:date>2016-09-05T08:33:46Z</dc:date>
    </item>
    <item>
      <title>Re: Total consecutive decreases</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Total-consecutive-decreases/m-p/296479#M62134</link>
      <description>&lt;PRE&gt;

data have;
input ID  month $  sales    lag    decreasecount;
cards;
1   Jan    100         .               0
1   Feb    120        100              0
1   Mar     90        120              1 
1   Apr     80         90              1
1   May     70         80              1
1   Jun    110         70              0 
1   Jul     90        110              1
1   Aug    150         90              0
1   Sep    100        150              1
1   Oct     90        100              1
1   Nov    110         90              0
1   Dec    100        110              1
;
run;
proc summary data=have;
by id decreasecount notsorted;
output out=temp;
run;
proc summary data=temp;
where decreasecount=1 and _freq_ gt 1;
by id;
var _freq_;
output out=want(drop=_:) sum=sum;
run;

&lt;/PRE&gt;</description>
      <pubDate>Mon, 05 Sep 2016 09:10:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Total-consecutive-decreases/m-p/296479#M62134</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2016-09-05T09:10:44Z</dc:date>
    </item>
    <item>
      <title>Re: Total consecutive decreases</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Total-consecutive-decreases/m-p/296611#M62186</link>
      <description>&lt;P&gt;You might find this easier if you were to create DECREASECOUNT differently. &amp;nbsp;Here's a one-step approach that keeps on incrementing the total count of decreases. &amp;nbsp;So the final value is the only one that matters.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data want;&lt;/P&gt;
&lt;P&gt;set have end=done;&lt;/P&gt;
&lt;P&gt;if lag(sales) &amp;lt;= sales then new_decreasecount=0;&lt;/P&gt;
&lt;P&gt;else new_decreasecount + 1;&lt;/P&gt;
&lt;P&gt;if new_decreasecount &amp;gt; 1 then total_decreases_so_far + 1;&lt;/P&gt;
&lt;P&gt;if done then put total_decreases_so_far=;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;</description>
      <pubDate>Tue, 06 Sep 2016 01:05:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Total-consecutive-decreases/m-p/296611#M62186</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2016-09-06T01:05:48Z</dc:date>
    </item>
    <item>
      <title>Re: Total consecutive decreases</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Total-consecutive-decreases/m-p/296717#M62219</link>
      <description>&lt;P&gt;Redo, using your existing data set:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data want;&lt;/P&gt;
&lt;P&gt;set have end=done;&lt;/P&gt;
&lt;P&gt;by decreasecount notsorted;&lt;/P&gt;
&lt;P&gt;if decreasecount=1 and first.decreasecount=0 or last.decreasecount=0 then total_decrease + 1;&lt;/P&gt;
&lt;P&gt;if done then put total_decrease=;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;</description>
      <pubDate>Tue, 06 Sep 2016 12:40:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Total-consecutive-decreases/m-p/296717#M62219</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2016-09-06T12:40:32Z</dc:date>
    </item>
    <item>
      <title>Re: Total consecutive decreases</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Total-consecutive-decreases/m-p/296786#M62238</link>
      <description>&lt;P&gt;You&amp;nbsp;only need basic data:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input ID  month $  sales;
cards;
1   Jan    100  
1   Feb    120  
1   Mar     90  
1   Apr     80  
1   May     70  
1   Jun    110  
1   Jul     90  
1   Aug    150  
1   Sep    100  
1   Oct     90  
1   Nov    110  
1   Dec    100  
;

data want;
decreases = 0;
consecutive = 0;
do until(last.id);
    set have; by id;
    if previousSales &amp;gt; sales then do;
        consecutive + 1;
        decreases + (consecutive&amp;gt;1) + (consecutive=2);
        end;
    else consecutive = 0;
    previousSales = sales;
    end;
keep id decreases;
run;

proc print noobs; var id decreases; run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 06 Sep 2016 17:32:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Total-consecutive-decreases/m-p/296786#M62238</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2016-09-06T17:32:23Z</dc:date>
    </item>
    <item>
      <title>Re: Total consecutive decreases</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Total-consecutive-decreases/m-p/296888#M62280</link>
      <description>&lt;P&gt;Here is a commented version of the code.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
/* Initialize the counts before each ID */
decreases = 0;
consecutive = 0;
/* Loop over all observation in an ID group */
do until(last.id);
    /* Read obs, create automatic variable last.id */
    set have; by id;
    /* previousSales is given a value at the end of the loop. On the first
       iteration, previousSales is missing (-Infinity) and the comparison
       is false */
    if previousSales &amp;gt; sales then do;
        consecutive + 1; /* Count number of consecutive decreases */
        /* As the number of consecutive decreases goes up, the total count 
           is increased following the sequence 0, 2, 1, 1, 1... created by
           the sum of two logical expressions. When consecutive = 1, both 
           conditions are false (0+0). When consecutive = 2, both conditions
           are true (1+1). When consecutive &amp;gt; 2 only the first logical
           condition is true (1+0). */
        decreases + (consecutive&amp;gt;1) + (consecutive=2);
        end;
    else consecutive = 0; /* Reset consecutive count */
    previousSales = sales; /* Remember for next loop iteration */
    end;
keep id decreases;
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 07 Sep 2016 03:24:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Total-consecutive-decreases/m-p/296888#M62280</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2016-09-07T03:24:39Z</dc:date>
    </item>
    <item>
      <title>Re: Total consecutive decreases</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Total-consecutive-decreases/m-p/296889#M62281</link>
      <description>Thank you so much! Appreciate your help!</description>
      <pubDate>Wed, 07 Sep 2016 03:27:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Total-consecutive-decreases/m-p/296889#M62281</guid>
      <dc:creator>LaiQ</dc:creator>
      <dc:date>2016-09-07T03:27:54Z</dc:date>
    </item>
    <item>
      <title>Re: Total consecutive decreases</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Total-consecutive-decreases/m-p/296890#M62282</link>
      <description>Thank you!</description>
      <pubDate>Wed, 07 Sep 2016 03:28:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Total-consecutive-decreases/m-p/296890#M62282</guid>
      <dc:creator>LaiQ</dc:creator>
      <dc:date>2016-09-07T03:28:14Z</dc:date>
    </item>
    <item>
      <title>Re: Total consecutive decreases</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Total-consecutive-decreases/m-p/296891#M62283</link>
      <description>Noted, will look into the codes. Thank you!</description>
      <pubDate>Wed, 07 Sep 2016 03:28:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Total-consecutive-decreases/m-p/296891#M62283</guid>
      <dc:creator>LaiQ</dc:creator>
      <dc:date>2016-09-07T03:28:36Z</dc:date>
    </item>
  </channel>
</rss>

