Hi, Have done the first three. You can work out the fourth from the code, just need to mix up counts on the where - have to leave now. data have; attrib FIRM format=$1. YEAR format=best. NAME format=$200. DUMMY CHANGE format=best.; infile datalines delimiter=','; input firm $ year name $ dummy change; datalines; A,2004,Mr.X,0,0 A,2005,Mr.X,0,0 A,2006,Mr.Y,0,1 A,2007,Mr.Y,0,0 A,2008,Mr.Y,0,0 B,2004,Mr.S,0,0 B,2005,Mr.S,0,0 B,2006,Mr.T,1,1 B,2007,Mr.T,1,0 B,2008,Mr.T,1,0 C,2004,Mr.M,1,0 C,2005,Mr.M,1,0 C,2006,Mr.M,1,0 C,2007,Mr.N,1,1 C,2008,Mr.N,1,0 D,2004,Mr.R,1,0 D,2005,Mr.R,1,0 D,2006,Mr.V,0,1 D,2007,Mr.V,0,0 D,2008,Mr.V,0,0 ; run; proc sql; create table subset1 as select * from have group by firm having sum(dummy)=0; create table subset2 as select * from have group by firm having sum(dummy)=5; quit; data tmp; set have; length flag $4; retain flag; by firm; if first.firm then flag='pre'; if change=1 then flag='on'; if change=0 and flag='on' then flag='post'; run; proc sql; create table subset3 as select * from have where firm in (select distinct firm from (select * from tmp where flag="pre") group by firm having sum(dummy)=0) and firm in (select distinct firm from (select * from tmp where flag="post") group by firm having sum(dummy)>0); quit;
... View more