<?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: Compare consecutive values in a row and increment counter in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Compare-consecutive-values-in-a-row-and-increment-counter/m-p/619246#M181764</link>
    <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/290321"&gt;@damilaresamuel&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here is an attempt to achieve this.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Hope this help!&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
	input Id $ Jan Feb Mar Apr May Jun;
	datalines;
A        2        4        5       8       4       3
B       0         3        2      5        6       6
C       7         6       9       2        4       1
;
run;

proc transpose data=have out=have_tr (rename=(col1=Sales)) name=Month;
	var _numeric_;
	by Id;
run;

data have_tr2;
	set have_tr;
	by Id Month notsorted;
	_lag = lag(Sales);
	if first.Id then flag = 0;
	else if sales &amp;gt; _lag then flag + 1;
	else flag = 0;
run;

proc sql;
	create table want as 
	select Id,
		   case when max(flag) &amp;gt; 1 then max(flag)
		        when max(flag) &amp;lt;= 1 then 0
		        end as Month_increase label='Consecutive month increase'
	from have_tr2
	group by Id;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Wed, 22 Jan 2020 17:35:46 GMT</pubDate>
    <dc:creator>ed_sas_member</dc:creator>
    <dc:date>2020-01-22T17:35:46Z</dc:date>
    <item>
      <title>Compare consecutive values in a row and increment counter</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Compare-consecutive-values-in-a-row-and-increment-counter/m-p/619228#M181756</link>
      <description>&lt;P&gt;Hi Everyone. I'm relatively new to SAS Programming and would like suggestions on how to solve the following problem:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Lets say I have 6 months record of the total sales made by several salespeople.&lt;/P&gt;&lt;P&gt;I will like to know the highest number of consecutive months of sales increase for each of them, during the 6 month period.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Please see sample data below&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Id&amp;nbsp; &amp;nbsp; &amp;nbsp; Jan&amp;nbsp; &amp;nbsp;Feb&amp;nbsp; &amp;nbsp; Mar&amp;nbsp; &amp;nbsp; Apr&amp;nbsp; &amp;nbsp;May&amp;nbsp; Jun&amp;nbsp;&lt;/P&gt;&lt;P&gt;A&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 4&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 5&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;8&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;4&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;3&lt;/P&gt;&lt;P&gt;B&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;0&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;3&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 2&amp;nbsp; &amp;nbsp; &amp;nbsp; 5&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 6&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;6&lt;/P&gt;&lt;P&gt;C&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;7&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;6&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;9&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 4&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;From the above sample date, the desired result is as follows:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;For row A, output will be equal to 3 ( since the longest consecutive period of increase is from Jan to Apr&amp;nbsp; i.e sales increased for 3 consecutive months.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;For row B, output will be equal to 2 ( since the longest consecutive period of increase is from Mar to May i.e sales increased for 2 consecutive months)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;For row C, output will be equal to 0 ( since the sales did not increase in any consecutive months)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I look forward to your suggestions.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 22 Jan 2020 16:55:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Compare-consecutive-values-in-a-row-and-increment-counter/m-p/619228#M181756</guid>
      <dc:creator>damilaresamuel</dc:creator>
      <dc:date>2020-01-22T16:55:36Z</dc:date>
    </item>
    <item>
      <title>Re: Compare consecutive values in a row and increment counter</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Compare-consecutive-values-in-a-row-and-increment-counter/m-p/619246#M181764</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/290321"&gt;@damilaresamuel&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here is an attempt to achieve this.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Hope this help!&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
	input Id $ Jan Feb Mar Apr May Jun;
	datalines;
A        2        4        5       8       4       3
B       0         3        2      5        6       6
C       7         6       9       2        4       1
;
run;

proc transpose data=have out=have_tr (rename=(col1=Sales)) name=Month;
	var _numeric_;
	by Id;
run;

data have_tr2;
	set have_tr;
	by Id Month notsorted;
	_lag = lag(Sales);
	if first.Id then flag = 0;
	else if sales &amp;gt; _lag then flag + 1;
	else flag = 0;
run;

proc sql;
	create table want as 
	select Id,
		   case when max(flag) &amp;gt; 1 then max(flag)
		        when max(flag) &amp;lt;= 1 then 0
		        end as Month_increase label='Consecutive month increase'
	from have_tr2
	group by Id;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 22 Jan 2020 17:35:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Compare-consecutive-values-in-a-row-and-increment-counter/m-p/619246#M181764</guid>
      <dc:creator>ed_sas_member</dc:creator>
      <dc:date>2020-01-22T17:35:46Z</dc:date>
    </item>
    <item>
      <title>Re: Compare consecutive values in a row and increment counter</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Compare-consecutive-values-in-a-row-and-increment-counter/m-p/619285#M181780</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/290321"&gt;@damilaresamuel&lt;/a&gt;:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;First, your case C is misindentified with respect to the output, as in this row there's 1 time increase from Feb (6) to Mar (9), so for this row the output should be 1. Other than that, the task readily yields to simple array processing:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have ;                                                                                                                             
  input id :$1. jan feb mar apr may jun ;                                                                                               
  cards ;                                                                                                                               
A  2  4  5  8  4  3                                                                                                                     
B  0  3  2  5  6  6                                                                                                                     
C  7  6  9  2  4  1                                                                                                                     
;                                                                                                                                       
                                                                                                                                        
data want (drop = _:) ;                                                                                                                 
  set have ;                                                                                                                            
  array mm [*] jan--jun ;                                                                                                               
  do _i = 2 to dim (mm) ;                                                                                                               
    if mm[_i] &amp;gt; mm[_i-1] then _q = sum (_q, 1) ;                                                                                        
    else do ;                                                                                                                           
      output = _q max output ;                                                                                                          
      call missing (_q) ;                                                                                                               
    end ;                                                                                                                               
  end ;                                                                                                                                 
run ;           
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Kind regards&lt;/P&gt;
&lt;P&gt;Paul D.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 22 Jan 2020 19:26:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Compare-consecutive-values-in-a-row-and-increment-counter/m-p/619285#M181780</guid>
      <dc:creator>hashman</dc:creator>
      <dc:date>2020-01-22T19:26:48Z</dc:date>
    </item>
    <item>
      <title>Re: Compare consecutive values in a row and increment counter</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Compare-consecutive-values-in-a-row-and-increment-counter/m-p/619457#M181872</link>
      <description>&lt;P&gt;Yes, you are right!&amp;nbsp;&lt;SPAN&gt;&amp;nbsp;Case C is misidentified&amp;nbsp;with respect to the output. Output should be 1.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;Your solution works perfectly, and is very neat.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I definitely need to study more on arrays. Perhaps you could suggest some resources.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks you!&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 23 Jan 2020 11:05:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Compare-consecutive-values-in-a-row-and-increment-counter/m-p/619457#M181872</guid>
      <dc:creator>damilaresamuel</dc:creator>
      <dc:date>2020-01-23T11:05:22Z</dc:date>
    </item>
    <item>
      <title>Re: Compare consecutive values in a row and increment counter</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Compare-consecutive-values-in-a-row-and-increment-counter/m-p/619458#M181873</link>
      <description>&lt;P&gt;Yes it definitely helps.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;As pointed out by another user, output of row C is misidentified; desired output should be 1, not 0.&lt;/P&gt;&lt;P&gt;But I understand your solution perfectly. I will only need to exclude the case statement.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you!&lt;/P&gt;</description>
      <pubDate>Thu, 23 Jan 2020 11:10:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Compare-consecutive-values-in-a-row-and-increment-counter/m-p/619458#M181873</guid>
      <dc:creator>damilaresamuel</dc:creator>
      <dc:date>2020-01-23T11:10:44Z</dc:date>
    </item>
    <item>
      <title>Re: Compare consecutive values in a row and increment counter</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Compare-consecutive-values-in-a-row-and-increment-counter/m-p/619488#M181898</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have ;                                                                                                                             
  input id :$1. jan feb mar apr may jun ;                                                                                               
  cards ;                                                                                                                               
A  2  4  5  8  4  3                                                                                                                     
B  0  3  2  5  6  6                                                                                                                     
C  7  6  9  2  4  1                                                                                                                     
;                                                                                                                                       
                                                                                                                                        
data want  ;                                                                                                                 
  set have ;                                                                                                                            
  array x{*} jan--jun ;   
  do i = 1 to dim (x)-1;
     n=0;
     do j = i+1 to dim (x) ;
        if x{j}&amp;gt;x{j-1} then n+1;
         else leave;
     end;
     want=max(want,n);
  end;
drop i j n;
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 23 Jan 2020 13:21:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Compare-consecutive-values-in-a-row-and-increment-counter/m-p/619488#M181898</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2020-01-23T13:21:58Z</dc:date>
    </item>
    <item>
      <title>Re: Compare consecutive values in a row and increment counter</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Compare-consecutive-values-in-a-row-and-increment-counter/m-p/619523#M181918</link>
      <description>&lt;P&gt;Hi Paul,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Upon further review, I realised that your solution doesn't fully solve my problem.&lt;BR /&gt;It doesn't return desired output for some scenarios (Please see new input below)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;jan&amp;nbsp; feb&amp;nbsp; mar&amp;nbsp; apr&amp;nbsp; may&amp;nbsp; jun&lt;/P&gt;&lt;P&gt;A&amp;nbsp; &amp;nbsp; 2&amp;nbsp; &amp;nbsp; &amp;nbsp;3&amp;nbsp; &amp;nbsp; &amp;nbsp; 0&amp;nbsp; &amp;nbsp; &amp;nbsp; 5&amp;nbsp; &amp;nbsp; &amp;nbsp; 6&amp;nbsp; &amp;nbsp; &amp;nbsp; 7&lt;BR /&gt;B&amp;nbsp; &amp;nbsp; 5&amp;nbsp; &amp;nbsp; &amp;nbsp;4&amp;nbsp; &amp;nbsp; &amp;nbsp; 3&amp;nbsp; &amp;nbsp; &amp;nbsp; 2&amp;nbsp; &amp;nbsp; &amp;nbsp;4&amp;nbsp; &amp;nbsp; &amp;nbsp; 5&lt;BR /&gt;C&amp;nbsp; &amp;nbsp; 2&amp;nbsp; &amp;nbsp; &amp;nbsp;2&amp;nbsp; &amp;nbsp; &amp;nbsp;2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;4&amp;nbsp; &amp;nbsp; &amp;nbsp;5&amp;nbsp; &amp;nbsp; &amp;nbsp; 6&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;After running the programme, the outputs for A, B &amp;amp; C are 1, 'null' &amp;amp; 'null' respectively.&lt;BR /&gt;These are incorrect.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Output for A should be 3 (i.e. mar to jun)&amp;nbsp;&lt;/P&gt;&lt;P&gt;Output for B should be&amp;nbsp;2 (i.e. apr to jun)&lt;/P&gt;&lt;P&gt;Output for C should be&amp;nbsp;3 (i.e. mar to jun)&lt;/P&gt;</description>
      <pubDate>Thu, 23 Jan 2020 14:28:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Compare-consecutive-values-in-a-row-and-increment-counter/m-p/619523#M181918</guid>
      <dc:creator>damilaresamuel</dc:creator>
      <dc:date>2020-01-23T14:28:36Z</dc:date>
    </item>
  </channel>
</rss>

