If I understand correctly what you're after then the following should work.
data have;
infile datalines truncover dlm=',' dsd;
input Id $ Date:ddmmyy10. Flag:$1.;
format date ddmmyy10.;
datalines;
1,31/01/2013,Y,
1,28/02/2013,Y,
1,31/03/2013,Y,
1,30/04/2013,
1,31/05/2013,
1,30/06/2013,
1,31/07/2013,
1,31/08/2013,
1,30/09/2013,Y,
1,31/10/2013,Y,
1,30/11/2013,Y,
1,31/12/2013,Y,
1,31/12/2014,Y,
1,30/11/2015,,
1,31/12/2015,Y,
2,31/01/2013,Y,
2,28/02/2013,Y,
2,31/03/2013,Y,
2,30/09/2013,Y,
2,31/10/2013,Y,
2,30/11/2013,Y,
2,31/12/2013,Y,
;
run;
proc sort data=have;
by id date;
run;
data want1(drop=_:);
set have curobs=curobs nobs=nobs;
by id flag notsorted;
format start_dt end_dt ddmmyy10.;
_lag_date=lag(date);
if flag='Y' then
do;
/* start_dt */
if first.flag then start_dt=date;
else if intnx('month',_lag_date,1,'e') ne date then start_dt=date;
/* end_dt */
if last.flag then end_dt=date;
else if curobs<nobs then
do;
curobs+1;
set have(keep=date rename=(date=_next_date)) point=curobs;
if intnx('month',_next_date,-1,'e') ne date then end_dt=date;
end;
end;
run;
... View more