<?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 Summ values between two dates for each row within a group in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Summ-values-between-two-dates-for-each-row-within-a-group/m-p/664943#M198724</link>
    <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
infile datalines missover; 
   input lag24 ddmmyy12. @12 id @14 date ddmmyy12. @25 score;
   format date ddmmyy10.;
   format lag24 ddmmyy10.;
   datalines; 
           1 21-09-2009 1.4   
25-09-2007 1 25-09-2009 1.4
16-11-2007 1 16-11-2009 2.2
03-02-2008 1 03-02-2010 2.6
           2 21-09-2009 1.4   
25-09-2007 2 25-09-2009 1.4
16-11-2007 2 16-11-2009 2.2
03-02-2012 2 03-02-2014 2.6
;       
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I need the following data. We start with the last row within the group variable "ID", and check if the previous dates are&amp;nbsp;within two years of the last date. Lets say the last date in the ID=1 is&amp;nbsp;03-02-2010 and the previous date is&amp;nbsp;16-11-2009 which is between&amp;nbsp;03-02-2008 and&amp;nbsp;03-02-2010, so take we sum of score (2.6+2.2). Then again check if the date before the previous date, ie. 2nd row from top&amp;nbsp;25-09-2009 is between&amp;nbsp;03-02-2008 and&amp;nbsp;03-02-2010,&amp;nbsp;so take we sum of score (2.6+2.2+1.4) and again for the first row&amp;nbsp;(2.6+2.2+1.41.4=7.6) which is diplayed on the last row. Similarly we take the 2nd last row as "index row" and check if the previous dates are between the date of index date and lag24=(index date-24 months).&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
infile datalines missover; 
  input lag24 ddmmyy12. @12 id @14 date ddmmyy12. @25 score @29 sum;
   format date ddmmyy10.;
   format lag24 ddmmyy10.;
   datalines; 
           1 21-09-2009 1.4 
25-09-2007 1 25-09-2009 1.4 2.8
16-11-2007 1 16-11-2009 2.2 6.2
03-02-2008 1 03-02-2010 2.6 7.6
           2 21-09-2009 1.4   
25-09-2007 2 25-09-2009 1.4 2.8
16-11-2007 2 16-11-2009 2.2 5
03-02-2012 2 03-02-2014 2.6
;       
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;DIV class="sas-author-username"&gt;&amp;nbsp;&lt;/DIV&gt;&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/16120"&gt;@Kurt&lt;/a&gt;&lt;/P&gt;</description>
    <pubDate>Thu, 25 Jun 2020 10:11:49 GMT</pubDate>
    <dc:creator>ifti_ch2002</dc:creator>
    <dc:date>2020-06-25T10:11:49Z</dc:date>
    <item>
      <title>Summ values between two dates for each row within a group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Summ-values-between-two-dates-for-each-row-within-a-group/m-p/664943#M198724</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
infile datalines missover; 
   input lag24 ddmmyy12. @12 id @14 date ddmmyy12. @25 score;
   format date ddmmyy10.;
   format lag24 ddmmyy10.;
   datalines; 
           1 21-09-2009 1.4   
25-09-2007 1 25-09-2009 1.4
16-11-2007 1 16-11-2009 2.2
03-02-2008 1 03-02-2010 2.6
           2 21-09-2009 1.4   
25-09-2007 2 25-09-2009 1.4
16-11-2007 2 16-11-2009 2.2
03-02-2012 2 03-02-2014 2.6
;       
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I need the following data. We start with the last row within the group variable "ID", and check if the previous dates are&amp;nbsp;within two years of the last date. Lets say the last date in the ID=1 is&amp;nbsp;03-02-2010 and the previous date is&amp;nbsp;16-11-2009 which is between&amp;nbsp;03-02-2008 and&amp;nbsp;03-02-2010, so take we sum of score (2.6+2.2). Then again check if the date before the previous date, ie. 2nd row from top&amp;nbsp;25-09-2009 is between&amp;nbsp;03-02-2008 and&amp;nbsp;03-02-2010,&amp;nbsp;so take we sum of score (2.6+2.2+1.4) and again for the first row&amp;nbsp;(2.6+2.2+1.41.4=7.6) which is diplayed on the last row. Similarly we take the 2nd last row as "index row" and check if the previous dates are between the date of index date and lag24=(index date-24 months).&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
infile datalines missover; 
  input lag24 ddmmyy12. @12 id @14 date ddmmyy12. @25 score @29 sum;
   format date ddmmyy10.;
   format lag24 ddmmyy10.;
   datalines; 
           1 21-09-2009 1.4 
25-09-2007 1 25-09-2009 1.4 2.8
16-11-2007 1 16-11-2009 2.2 6.2
03-02-2008 1 03-02-2010 2.6 7.6
           2 21-09-2009 1.4   
25-09-2007 2 25-09-2009 1.4 2.8
16-11-2007 2 16-11-2009 2.2 5
03-02-2012 2 03-02-2014 2.6
;       
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;DIV class="sas-author-username"&gt;&amp;nbsp;&lt;/DIV&gt;&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/16120"&gt;@Kurt&lt;/a&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 25 Jun 2020 10:11:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Summ-values-between-two-dates-for-each-row-within-a-group/m-p/664943#M198724</guid>
      <dc:creator>ifti_ch2002</dc:creator>
      <dc:date>2020-06-25T10:11:49Z</dc:date>
    </item>
    <item>
      <title>Re: Summ values between two dates for each row within a group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Summ-values-between-two-dates-for-each-row-within-a-group/m-p/664952#M198729</link>
      <description>&lt;P&gt;Try this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table want as
  select
    a.id,
    a.date,
    a.score,
    case
      when sum(b.score) = a.score
      then .
      else sum(b.score)
    end as sum
  from have a
  left join have b
  on a.id = b.id and b.date between a.lag24 and a.date
  group by
    a.id,
    a.date,
    a.score
;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 25 Jun 2020 10:59:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Summ-values-between-two-dates-for-each-row-within-a-group/m-p/664952#M198729</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-06-25T10:59:27Z</dc:date>
    </item>
    <item>
      <title>Re: Summ values between two dates for each row within a group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Summ-values-between-two-dates-for-each-row-within-a-group/m-p/664969#M198735</link>
      <description>&lt;P&gt;Here is a data step solution:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have;
  by id;
  if first.id then do;
    _first_=_N_;
    _sum_=score;
    end;
  else do;
    _sum_+score;
    do _first_=_first_ to _N_;
      set have(keep=date score rename=(date=_date_ score=_score_)) point=_first_;
      if _date_&amp;gt;=lag24 then leave;
      _sum_=sum-_score_;
      end;
    sum=_sum_;
    end;
  retain _:;
  drop _:;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;BTW, I think there is an error in your WANT data step, I think the SUM in row 3 should be 5, not 6.2.&lt;/P&gt;</description>
      <pubDate>Thu, 25 Jun 2020 11:49:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Summ-values-between-two-dates-for-each-row-within-a-group/m-p/664969#M198735</guid>
      <dc:creator>s_lassen</dc:creator>
      <dc:date>2020-06-25T11:49:23Z</dc:date>
    </item>
    <item>
      <title>Re: Summ values between two dates for each row within a group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Summ-values-between-two-dates-for-each-row-within-a-group/m-p/665507#M199001</link>
      <description>Great, It works</description>
      <pubDate>Sat, 27 Jun 2020 09:05:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Summ-values-between-two-dates-for-each-row-within-a-group/m-p/665507#M199001</guid>
      <dc:creator>ifti_ch2002</dc:creator>
      <dc:date>2020-06-27T09:05:05Z</dc:date>
    </item>
    <item>
      <title>Re: Summ values between two dates for each row within a group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Summ-values-between-two-dates-for-each-row-within-a-group/m-p/665508#M199002</link>
      <description>Thanks for your time, i have not tested yet, it will definitely work, i hope that</description>
      <pubDate>Sat, 27 Jun 2020 09:07:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Summ-values-between-two-dates-for-each-row-within-a-group/m-p/665508#M199002</guid>
      <dc:creator>ifti_ch2002</dc:creator>
      <dc:date>2020-06-27T09:07:27Z</dc:date>
    </item>
  </channel>
</rss>

