data temp2(keep=f2 f12 num_in_firm) ;
set temp1;
by f2;
retain num_in_firm 0;
if first.f2 then num_in_firm=1;
else num_in_firm=num_in_firm+1;
proc sort;
by f2;
run;
data temp30(keep=f2 firm_id);
set temp2;
if num_in_firm ne 1 then delete;
run;
data temp3;
set temp30;
firm_id=_n_;
proc sort;
by f2;
run;
data temp4;
merge temp2 temp3;
by f2;
run;
proc sql;
create table temp5 as
select count(temp4.f2) as repeat_num, temp4.*
from temp4
group by f2
order by firm_id,num_in_firm,repeat_num ;
quit;
data temp6;
set temp5;
total_num=_n_;
run;
proc sql;
create table want as
select f2,firm_id, f12, num_in_firm,repeat_num,total_num
from temp6;