It is not getting you all the pah, it give you the longest path.
If you only want the first and last part ,Change code as:
data have;
input _end _start ;
cards;
2 1
3 2
4 2
5 2
6 3
10 6
7 6
8 7
9 7
;
run;
proc sql;
create table ancient as
select * from have
where _start not in (select _end from have);
quit;
data want(keep= first last); /*<------*/
if _n_ eq 1 then do;
length path _path $ 200 ;
if 0 then set have;
declare hash ha(hashexp:20,dataset:'have(where=(_start is not missing and _end is not missing))',multidata:'y');
ha.definekey('_start');
ha.definedata('_end');
ha.definedone();
declare hash pa(ordered:'y');
declare hiter hi_path('pa');
pa.definekey('n');
pa.definedata('n','path');
pa.definedone();
end;
set ancient;
length first last $ 80; /*<-------*/
count=1;n=1;_n=1;
path=catx('|',_start,_end);
pa.add();
do while(hi_path.next()=0);
if n ne 1 then pa.remove(key:_n);_n=n;
_path=path;
_start=scan(path,-1,'|');
rc=ha.find();
if rc ne 0 then do;first=scan(path,1,'|');last=scan(path,-1,'|');output;end; /*<-------*/
do while(rc=0);
if not findw(path,strip(_end),'|') then do;
if length(path)+length(_end)+1 gt lengthc(path) then do;
putlog 'ERROR: The length of path and _path are set too short';
stop;
end;
count+1;n=count;
path=catx('|',path,_end);
pa.add();
path=_path;
end;
else do;first=scan(path,1,'|');last=scan(path,-1,'|');output;end;/*<------*/
rc=ha.find_next();
end;
end;
pa.clear();
run;
... View more