用的例子跟楼主的有差异,看看是不是符合要求:
data tm;
input subject $ drug $ begin:yymmdd10. end:yymmdd10. @@;
format begin end yymmdd10.;
cards;
001 A 2014-6-1 2014-6-3
001 B 2014-6-2 2014-6-5
001 C 2014-6-8 2014-6-10
002 A 2014-6-8 2014-6-13
002 B 2014-6-2 2014-6-5
002 C 2014-6-8 2014-6-10
;
proc sort data=tm;
by subject begin end;
run;
data dur;
set tm;
lagend=lag(end);
if subject ne lag(subject) then call missing(lagend);
if lagend>=begin then begin1=lagend+1;
else begin1=begin;
dur=end-begin1+1;
drop begin1 lagend;
run;
proc sql;
create table wanted as
select subject, sum(dur) as totdur
from dur
group by 1;
quit;