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