🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Obsidian | Level 7

## Summ values between two dates for each rown within a group

Hi,

I have following dataset

```data have;
infile datalines missover;
input lag24 mmddyy10. id date  mmddyy10. score;
format date mmddyy10.;
format lag24 mmddyy10.;
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-2008 2 03-02-2010 2.6
;
run;```

I need the following data. We start with the last row within the group variable "ID", and check if the previous dates are within two years of the last date. Lets say the last date in the ID=1 is 03-02-2010 and the previous date is 16-11-2009 which is between 03-02-2008 and 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 25-09-2009 is between 03-02-2008 and 03-02-2010, so take we sum of score (2.6+2.2+1.4) and again for the first row (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).

```data want;
infile datalines missover;
input lag24 mmddyy10. id date  mmddyy10. score sum;
format date mmddyy10.;
format lag24 mmddyy10.;
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;```
1 ACCEPTED SOLUTION

Accepted Solutions
Obsidian | Level 7

## Re: Summ values between two dates for each rown within a group

3 REPLIES 3
Obsidian | Level 7

## Re: Summ values between two dates for each rown within a group

One mistake. Date in Last row of data "have" is same as in data "want"

```data have;
infile datalines missover;
input lag24 mmddyy10. id date  mmddyy10. score;
format date mmddyy10.;
format lag24 mmddyy10.;
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;```
Super User

## Re: Summ values between two dates for each rown within a group

Please correct your data step code so that it works and delivers valid data. Your dates are (among other things) in  DMY order, so the MMDDYY informat won't work.

Obsidian | Level 7

## Re: Summ values between two dates for each rown within a group

Discussion stats
• 3 replies
• 485 views
• 0 likes
• 2 in conversation