I'll use the retain and the first and last. Something like this (I don't know if works because have written it by memory, but you can start by it) proc sorta data=; by MemberID program_start_date;run; data output; set input; by MemberID; retain xxx _sum .; if first MemberID then do; xxx=program_end_date; end; if program_start_date=xxx+1 then do; _sum=sum(_sum,duration); xxx=program_end_date; end; if last.MemberID then output; run; The problem is if yuo need the max totalday or the last total_day. My code do the last, but with some changment you can do the first option
... View more