This? Data have;
input Time ID County $ FocalEvent;
cards;
1 222 ABC 0
2 222 ABC 0
3 222 ABC 0
4 222 ABC 0
5 222 ABC 1
6 222 ABC 0
7 222 ABC 0
8 222 ABC 0
9 222 ABC 0
10 222 ABC 0
11 222 ABC 0
12 222 ABC 0
13 222 ABC 0
1 333 BBB 0
2 333 BBB 0
3 333 BBB 0
4 333 BBB 0
5 333 BBB 0
6 333 BBB 0
7 333 BBB 0
8 333 BBB 0
9 333 BBB 0
10 333 BBB 0
11 333 BBB 0
12 333 BBB 0
13 333 BBB 0
1 444 CCC 0
2 444 CCC 0
3 444 CCC 0
4 444 CCC 0
5 444 CCC 1
6 444 CCC 0
7 444 CCC 0
8 444 CCC 0
9 444 CCC 0
10 444 CCC 1
11 444 CCC 0
12 444 CCC 0
13 444 CCC 0
1 555 DDD 0
2 555 DDD 0
3 555 DDD 0
4 555 DDD 0
5 555 DDD 0
6 555 DDD 0
7 555 DDD 0
;
run;
proc means data=have nway noprint;
class time;
var FocalEvent;
output out=time_indicator(keep=Time FocalEvent) max = FocalEvent;
run;
proc means data=have nway noprint;
class ID;
var FocalEvent;
output out=control_indicator(keep=ID NotControl) max = NotControl;
run;
data want_step1_control;
set time_indicator;
retain treated;
if _N_ = 1 then do;
treated = 0;
BeforeAfter2 = FocalEvent;
BeforeAfter3 = FocalEvent;
end;
else do;
BeforeAfter2 = FocalEvent + lag(FocalEvent);
BeforeAfter3 = FocalEvent + lag(BeforeAfter2);
if BeforeAfter2 = . then BeforeAfter2 = FocalEvent;
if BeforeAfter3 = . then BeforeAfter3 = BeforeAfter2;
end;
treated = min(treated + FocalEvent, 1);
run;
proc sort data=want_step1_control;
by descending time;
run;
data want_step2_control;
set want_step1_control;
if _N_ =1 then do;
Before1 = FocalEvent;
Before2 = FocalEvent;
Before3 = FocalEvent;
end;
else do;
Before1 = FocalEvent + lag(FocalEvent);
Before2 = FocalEvent + lag(Before1);
Before3 = FocalEvent + lag(Before2);
end;
Duration2 = min(Before2 + BeforeAfter2, 1);
Duration3 = min(Before3 + BeforeAfter3, 1);
drop before1-before3 FocalEvent;
run;
proc sort data=want_step2_control;
by time;
title "indicator for control";
proc print; run;
data want_step1_not_control;
merge have control_indicator;
by ID;
retain treated;
if NotControl then do;
if first.id then do;
treated = 0;
BeforeAfter2 = FocalEvent;
BeforeAfter3 = FocalEvent;
end;
else do;
BeforeAfter2 = FocalEvent + lag(FocalEvent);
BeforeAfter3 = FocalEvent + lag(BeforeAfter2);
if BeforeAfter2 = . then BeforeAfter2 = 0;
if BeforeAfter3 = . then BeforeAfter3 = 0;
end;
end;
else delete;
treated = min(treated + FocalEvent, 1);
run;
proc sort data=want_step1_not_control;
by id descending time;
run;
data want_step2_not_control;
set want_step1_not_control;
by ID;
retain treated;
if NotControl then do;
if first.id then do;
Before1 = FocalEvent;
Before2 = FocalEvent;
Before3 = FocalEvent;
end;
else do;
Before1 = FocalEvent + lag(FocalEvent);
Before2 = FocalEvent + lag(Before1);
Before3 = FocalEvent + lag(Before2);
end;
Duration2 = min(Before2 + BeforeAfter2, 1);
Duration3 = min(Before3 + BeforeAfter3, 1);
end;
treated = min(treated + FocalEvent, 1);
drop before1-before3;
run;
title "indicators for not control";
proc print; run;
data want_step3_control;
merge have control_indicator;
by ID;
if NotControl then delete;
treated = 0;
proc sort data= want_step2_control;
by time;
proc sort data= want_step3_control;
by time;
data want_step4_control;
merge want_step2_control want_step3_control;
by time;
data want;
set want_step2_not_control want_step4_control;
proc sort data=want;
by id time;
title "Merged results";
proc print; run;
... View more