data a;
input year ida idb;
cards;
1999 1 3
1999 2 4
1999 33 66
1999 4 1
1999 1 6
1999 9 5
1999 5 7
1999 6 1
1999 7 11
1999 22 33
1999 88 99
;
data b;
set a;
ida_c=cats("*",ida,"*");
idb_c=cats("*",idb,"*");
n_obs=_n_;
run;
data c;
length searched members $100;
retain group 0 searched " ";
set b;
if index(searched,cats("*",_n_,"*"))=0;
group+1;
grp=cats(ida_c,idb_c);
if _n_=1 then do;
searched="*1*";
members="1*";
n=1;
end;
else do; members=" "; n=0; end;
do until (find=0);
find=0;
do obs=2 to nobs;
if index(searched,cats("*",obs,"*"))=0 then do;
set b point=obs nobs=nobs;
if index(grp,strip(ida_c)) or index(grp,strip(idb_c)) then do;
find=1;
n+1;
searched=cats(searched,obs,"*");
members=cats(members,obs,"*");
if index(grp,strip(ida_c))=0 then grp=cats(grp,ida_c);
if index(grp,strip(idb_c))=0 then grp=cats(grp,idb_c);
end;
end;
end;
end;
put group= members= n= grp=;
do j=1 to n;
n_obs=input(scan(members,j,"*"),best.);
output;
end;
keep group n_obs;
run;
proc sort data=c; by n_obs; run;
data d;
merge b c;
by n_obs;
drop ida_c idb_c n_obs;
run;