I think my brain melted trying to understand the 'sort_chain' variable, but the following code produces the rest of them in the format you specified - I think I'm just not understanding how to derive sort_chain from this.
data have;
infile cards dsd truncover firstobs=1 dlm=',';
length cid parent_cid $1;
input cid parent_cid;
cards;
a,
b,a
c,b
d,a
e,
f,
g,f
;
run;
proc sql noprint;
create table c2num as
select 'fc2num' as fmtname length=8,
'C' as type length=1, cid as start, monotonic() as label
from have;
quit;
data num2c (rename=(_start=start _label=label));
set c2num (rename=(start=_label label=_start));
fmtname='fnum2c';
type='N';
run;
data c2num;
set c2num end=last;
output;
n+1;
if last then do;
start=' ';
label=0;
output;
call symputx("ncids",n);
end;
drop n;
run;
proc format cntlin=c2num; run;
proc format cntlin=num2c; run;
data want;
set have (rename=(cid=_CID parent_CID=_PC)) end=last;
array nparents {0:&ncids} _temporary_ (%eval(&ncids+1)*0);
array p {&ncids} _temporary_;
nparents[put(_CID,$fc2num.)*1]+nparents[put(_PC,$fc2num.)*1]+1;
p[put(_CID,$fc2num.)*1]=put(_PC,$fc2num.)*1;
if last then do;
do i=1 to dim(p);
cid=put(i,fnum2c.);
sort_cid=cid;
level=nparents[i];
output;
do j=1 to dim(p);
if p[j]=i then do;
sort_cid=put(j,fnum2c.);
level=nparents[j];
output;
do k=1 to dim(p);
if p[k]=j then do;
sort_cid=put(k,fnum2c.);
level=nparents[k];
output;
end;
end;
end;
end;
level=nparents[i];
_i=i;
do while (level>1);
sort_cid=put(p[_i],fnum2c.);
level=nparents[p[_i]];
output;
_i=p[_i];
end;
end;
end;
keep cid sort_cid level;
run;
proc sort data=want; by cid sort_cid; run;
proc print data=want; run;
... View more