Hi Xia, I made minor changes in the code, to show just the straight path traversed: data have; infile datalines; length From To $11.; input ID From $ To $; datalines; 100 c1 c2 100 c2 c3 100 c3 c4 100 c4 c5 100 c5 c6 100 c6 c7 100 c3 c8 100 c9 c10 100 c4 c11 100 c5 c12 ; run; data want (keep = ID path); if _n_ eq 1 then do; length path _path $ 32000 ; if 0 then set have; declare hash ha(hashexp:20,dataset:'have(where=(From is not missing and To is not missing))',multidata:'Y'); ha.definekey('ID','From'); ha.definedata('To'); ha.definedone(); declare hash pa(ordered:'Y'); declare hiter hi_path('pa'); pa.definekey('count'); pa.definedata('path'); pa.definedone(); end; set have; count=1; path=catx(' ',From,To); pa.add(); do while(hi_path.next()=0); _path=path; From=scan(path,-1,' '); rc=ha.find(); do while(rc=0); if not find(path,strip(To)) then do; count+1; path=catx(' ',path,To); pa.add(); path=_path; end; /*else if scan(path,1,' ')=To then do;loop=catx(' ',path,To);output;end;*/ rc=ha.find_next(); end; end; pa.clear(); run; proc sql noprint; select max(countw(path)) into : n separated by ' ' from want; quit; data final_want; id+1; set want; array x{*} $ 40 c1-c&n ; do i=1 to dim(x); x{i}=scan(path,i); end; drop i path id; run; The output I am getting is as below: The obs from 2 to 6 are part of obs 1. How can I avoid them in the ouput dataset? Deep
... View more