I can't get your data to match with your description of the rules. But this should get you started. data temp;
set have(keep=individual firm individual_exit_year);
run;
proc sort data=temp out=base nodup;
by individual firm individual_exit_year;
run;
data base2;
set base;
do year=2005 to 2023;
output;
end;
run;
proc sql;
create table temp2
as select distinct investment_year as inv_year
from have
;
create table base3
as select distinct a.*, b.investment_year, c.inv_year
from base2 a
left join have b
on a.individual=b.individual
and a.firm=b.firm
and a.individual_exit_year=b.individual_exit_year
and a.year=b.investment_year
left join temp2 c
on a.year=c.inv_year
order by individual,year, firm
;
quit;
data want;
set base3;
retain treatment;
by individual year firm;
if first.indiviudal then
treatment=0;
if investment_year=inv_year and investment_year ne . then
treatment=1;
else if investment_year=. and inv_year ne . then treatment=0;
run;
... View more