BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
shasank
Quartz | Level 8

Hi SAS Community,

 

I need your help with a problem.

 

Have   
IDFlagdateStartdateCost
    
13317-Jul-1318-Jul-131307.77
13317-Jul-1318-Jul-1396.6
13317-Jul-1321-Jul-1355.74
13317-Jul-1322-Jul-13169.86
13317-Jul-1322-Jul-1358.16
13317-Jul-1322-Jul-1330.51
13317-Jul-1328-Aug-1334.93
13317-Jul-1330-Aug-130
13317-Jul-1330-Aug-130
13317-Jul-135-Sep-1334.93
13317-Jul-1320-Sep-133.34
13317-Jul-1323-Sep-1383.48
13418-Jul-1321-Oct-1313.11
13519-Jul-1325-Oct-130
13620-Jul-1325-Oct-1311.5
13721-Jul-1325-Oct-1310.48
13822-Jul-1325-Oct-13217.07
13923-Jul-1325-Oct-1383.48
14024-Jul-1325-Oct-130
14125-Jul-1325-Oct-1349.59
14226-Jul-1325-Oct-1363.2
14327-Jul-1327-Oct-1320.18
14428-Jul-134-Nov-1311.49
14529-Jul-137-Nov-1329.45

 

The problem I need to solve is to compress the above to accumulate the costs for 30 day intervals by ID.

In other words, for first row Intervel_start  = Flag_date or and Intervel_end = Interval_start + 30.

For the second row, Interval_start = Previous row Interval_end + 1 and Interval_end = Interval_start+30 and so on for all rows. Once this is done the startdate and cost within these interval ranges need to be accumulated per each range for each ID. 

 

This is the mock output for the above data-set. I have tried using lag function to get the ranges but seems to not work. 

 

Want    
IDFlagdateIntervel_StartIntervel_EndCum_Cost
13317-Jul-1317-Jul-1316-Aug-131718.64
13317-Jul-1317-Aug-1316-Sep-1369.86
13317-Jul-1317-Sep-1317-Oct-1386.82
13317-Jul-1318-Oct-1317-Nov-13509.55

 

 

Thank you for your time and help.

 

Best,

SC.

 

1 ACCEPTED SOLUTION

Accepted Solutions
PGStats
Opal | Level 21

You could do this with SQL, using SAS date interval functions:

 

proc sql;
select 
    id,
    flagDate,
    intnx("month", flagDate, 
        intck("month", flagDate, startDate, "continuous"), "same") 
        as intervalStart format=yymmdd10.,    
    intnx("month", flagDate, 
        intck("month", flagDate, startDate, "continuous")+1, "same")-1 
        as intervalEnd format=yymmdd10.,
    sum(cost) as totalCost
from have
group by id, flagDate, calculated intervalStart, calculated intervalEnd;
quit;
PG

View solution in original post

3 REPLIES 3
PGStats
Opal | Level 21

You could do this with SQL, using SAS date interval functions:

 

proc sql;
select 
    id,
    flagDate,
    intnx("month", flagDate, 
        intck("month", flagDate, startDate, "continuous"), "same") 
        as intervalStart format=yymmdd10.,    
    intnx("month", flagDate, 
        intck("month", flagDate, startDate, "continuous")+1, "same")-1 
        as intervalEnd format=yymmdd10.,
    sum(cost) as totalCost
from have
group by id, flagDate, calculated intervalStart, calculated intervalEnd;
quit;
PG
andreas_lds
Jade | Level 19

Please post data in usable form, after more than 50 posts you should know that tables provide no information about the data-types you have.

 

After you did that, please explain the fourth row in your output-table. The data you showed in that row is not in your have-table.

shasank
Quartz | Level 8
Hi Andreas,
Thank you for taking time to reply. Sorry for not providing enough information. The sequence of transformation starts with calculating the interval_start and Interval_end from the flag date with 30 day interval and then if the the startdate is within this interval then those costs are accumulated in that want table row.

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 3 replies
  • 911 views
  • 0 likes
  • 3 in conversation