<?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: How to compare row to row value in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-compare-row-to-row-value/m-p/505029#M135222</link>
    <description>&lt;P&gt;Sort and use some less-than-simple by-group processing:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input CUSTOMER_ID policy $2. date anydtdte23.;
format date date9.;
cards;
1057086 a 21AUG2014
1057086 b 25AUG2014
1057086 c 17SEP2014
1057086 d 17SEP2014
1057086 e 19SEP2014
1057086 f 26SEP2014
1057086 g 26SEP2014
1057086 h 26Oct2015
1057086 i 21Dec2016
1020006 j 22Nov2011
1020006 k 21Dec2012
1020006 l 30Jan2015
1020006 m 30Jan2015
1020006 o n 30Jan2015
1020006 p 30Jan2015
1020006 q 30Jan2015
1020006 r 30Jan2015
1020006 s 31Jan2018
;
run;

proc sort data=have;
by customer_id date;
run;

data want;
set have;
by customer_id date;
retain count sec_count;
if first.customer_id
then do;
  count = 0;
  sec_count = 1;
end;
else if first.date
then do;
  count + sec_count;
  sec_count = 1;
end;
else sec_count + 1;
drop sec_count;
run;

proc print data=want noobs;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result:&lt;/P&gt;
&lt;PRE&gt;CUSTOMER_
    ID       policy         date    count

 1020006       o               .      0  
 1020006       j       22NOV2011      1  
 1020006       k       21DEC2012      2  
 1020006       l       30JAN2015      3  
 1020006       m       30JAN2015      3  
 1020006       p       30JAN2015      3  
 1020006       q       30JAN2015      3  
 1020006       r       30JAN2015      3  
 1020006       s       31JAN2018      8  
 1057086       a       21AUG2014      0  
 1057086       b       25AUG2014      1  
 1057086       c       17SEP2014      2  
 1057086       d       17SEP2014      2  
 1057086       e       19SEP2014      4  
 1057086       f       26SEP2014      5  
 1057086       g       26SEP2014      5  
 1057086       h       26OCT2015      7  
 1057086       i       21DEC2016      8  
&lt;/PRE&gt;</description>
    <pubDate>Wed, 17 Oct 2018 11:18:42 GMT</pubDate>
    <dc:creator>Kurt_Bremser</dc:creator>
    <dc:date>2018-10-17T11:18:42Z</dc:date>
    <item>
      <title>How to compare row to row value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-compare-row-to-row-value/m-p/505025#M135220</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;Please refer the following input and output.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data have;&lt;BR /&gt;input CUSTOMER_ID policy $2. date anydtdte23.;&lt;BR /&gt;format date date9.;&lt;BR /&gt;cards;&lt;BR /&gt;1057086 a 21AUG2014&lt;BR /&gt;1057086 b 25AUG2014&lt;BR /&gt;1057086 c 17SEP2014&lt;BR /&gt;1057086 d 17SEP2014&lt;BR /&gt;1057086 e 19SEP2014&lt;BR /&gt;1057086 f 26SEP2014&lt;BR /&gt;1057086 g 26SEP2014&lt;BR /&gt;1057086 h 26Oct2015&lt;BR /&gt;1057086 i 21Dec2016&lt;BR /&gt;1020006 j 22Nov2011&lt;BR /&gt;1020006 k 21Dec2012&lt;BR /&gt;1020006 l 30Jan2015&lt;BR /&gt;1020006 m 30Jan2015&lt;BR /&gt;1020006 o n 30Jan2015&lt;BR /&gt;1020006 p 30Jan2015&lt;BR /&gt;1020006 q 30Jan2015&lt;BR /&gt;1020006 r 30Jan2015&lt;BR /&gt;1020006 s 31Jan2018&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Output Want in following format.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;CUSTOMER_ID policy date Count&lt;BR /&gt;1020006 o . 0&lt;BR /&gt;1020006 j 22-Nov-11 1&lt;BR /&gt;1020006 k 21-Dec-12 2&lt;BR /&gt;1020006 l 30-Jan-15 3&lt;BR /&gt;1020006 m 30-Jan-15 3&lt;BR /&gt;1020006 p 30-Jan-15 3&lt;BR /&gt;1020006 q 30-Jan-15 3&lt;BR /&gt;1020006 r 30-Jan-15 3&lt;BR /&gt;1020006 s 31-Jan-18 8&lt;BR /&gt;1057086 a 21-Aug-14 0&lt;BR /&gt;1057086 b 25-Aug-14 1&lt;BR /&gt;1057086 c 17-Sep-14 2&lt;BR /&gt;1057086 d 17-Sep-14 2&lt;BR /&gt;1057086 e 19-Sep-14 4&lt;BR /&gt;1057086 f 26-Sep-14 5&lt;BR /&gt;1057086 g 26-Sep-14 5&lt;BR /&gt;1057086 h 26-Oct-15 7&lt;BR /&gt;1057086 i 21-Dec-16 8&lt;/P&gt;</description>
      <pubDate>Wed, 17 Oct 2018 10:58:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-compare-row-to-row-value/m-p/505025#M135220</guid>
      <dc:creator>Shantaram</dc:creator>
      <dc:date>2018-10-17T10:58:57Z</dc:date>
    </item>
    <item>
      <title>Re: How to compare row to row value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-compare-row-to-row-value/m-p/505029#M135222</link>
      <description>&lt;P&gt;Sort and use some less-than-simple by-group processing:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input CUSTOMER_ID policy $2. date anydtdte23.;
format date date9.;
cards;
1057086 a 21AUG2014
1057086 b 25AUG2014
1057086 c 17SEP2014
1057086 d 17SEP2014
1057086 e 19SEP2014
1057086 f 26SEP2014
1057086 g 26SEP2014
1057086 h 26Oct2015
1057086 i 21Dec2016
1020006 j 22Nov2011
1020006 k 21Dec2012
1020006 l 30Jan2015
1020006 m 30Jan2015
1020006 o n 30Jan2015
1020006 p 30Jan2015
1020006 q 30Jan2015
1020006 r 30Jan2015
1020006 s 31Jan2018
;
run;

proc sort data=have;
by customer_id date;
run;

data want;
set have;
by customer_id date;
retain count sec_count;
if first.customer_id
then do;
  count = 0;
  sec_count = 1;
end;
else if first.date
then do;
  count + sec_count;
  sec_count = 1;
end;
else sec_count + 1;
drop sec_count;
run;

proc print data=want noobs;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result:&lt;/P&gt;
&lt;PRE&gt;CUSTOMER_
    ID       policy         date    count

 1020006       o               .      0  
 1020006       j       22NOV2011      1  
 1020006       k       21DEC2012      2  
 1020006       l       30JAN2015      3  
 1020006       m       30JAN2015      3  
 1020006       p       30JAN2015      3  
 1020006       q       30JAN2015      3  
 1020006       r       30JAN2015      3  
 1020006       s       31JAN2018      8  
 1057086       a       21AUG2014      0  
 1057086       b       25AUG2014      1  
 1057086       c       17SEP2014      2  
 1057086       d       17SEP2014      2  
 1057086       e       19SEP2014      4  
 1057086       f       26SEP2014      5  
 1057086       g       26SEP2014      5  
 1057086       h       26OCT2015      7  
 1057086       i       21DEC2016      8  
&lt;/PRE&gt;</description>
      <pubDate>Wed, 17 Oct 2018 11:18:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-compare-row-to-row-value/m-p/505029#M135222</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2018-10-17T11:18:42Z</dc:date>
    </item>
    <item>
      <title>Re: How to compare row to row value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-compare-row-to-row-value/m-p/505434#M135349</link>
      <description>&lt;P&gt;Thanks&amp;nbsp;KurtBremser.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN class="login-bold"&gt;Its working.&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 18 Oct 2018 05:24:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-compare-row-to-row-value/m-p/505434#M135349</guid>
      <dc:creator>Shantaram</dc:creator>
      <dc:date>2018-10-18T05:24:34Z</dc:date>
    </item>
  </channel>
</rss>

