I have a database which contains IDs , dates and costs like this:
ID | date | cost | Difference |
ID1 | 25/02/2020 | 20 | 1 |
ID1 | 26/02/2020 | 10 | . |
ID2 | 01/05/2020 | 15 | 6 |
ID2 | 07/05/2020 | 23 | . |
ID3 | 03/01/2019 | 18 | 1 |
ID3 | 04/01/2019 | 17 | 1 |
ID3 | 05/01/2019 | 11 | . |
what I want to do is to sum the cost by ID and by dates with gap of 1 to 6 days, so the required data would be as below:
ID | date | cost |
ID1 | 25/02/2020 | 30 |
ID2 | 01/05/2020 | 38 |
ID3 | 03/01/2019 | 46 |
What I tried to do is to create another date variable that takes the same date if we have gaps, and then do the sum by this date variable and the ID with this code but it doesn't work:
data new;
set old;
format new_date yymmdd10.;
if (1<=lag_Diff<=6 ) then new_date=lag_date;
if (1<=lag_Diff<=6 and 1<=lag_lag_Diff<=6) then new_date=lag_lag_date;
if new_date eq . then new_date=date;
run;
I would be thankful if you help me 🙂