<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Finding depth and last child in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Finding-depth-and-last-child/m-p/428746#M105871</link>
    <description>&lt;P&gt;I would try something like this:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
  create index parent on test(parent);
quit;

data want;
  set test(where=(parent is null));
  parent=name;
  do depth=0 to 100; /* we stop at 100 in case there are circular references in data */
    set test(rename=(name=last_child)) key=parent;
    if _iorc_ then leave;
    parent=last_child;
    end;
  _error_=0; /* _ERROR_ is set when a key is not found, this is not an error here */
  keep name depth last_child;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Using SET with KEY= is normally the easiest way to traverse trees. The value 100 is used as I assume that there are no trees with a depth greater than that - so if you find that value it may mean that you have a circular reference in your data.&lt;/P&gt;</description>
    <pubDate>Thu, 18 Jan 2018 11:26:15 GMT</pubDate>
    <dc:creator>s_lassen</dc:creator>
    <dc:date>2018-01-18T11:26:15Z</dc:date>
    <item>
      <title>Finding depth and last child</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Finding-depth-and-last-child/m-p/428476#M105793</link>
      <description>&lt;P&gt;I have below dataset 'test' which has two columns name and parent:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Explanation=&amp;gt; a has no parent, a is parent of b, b is parent of c, c is parent of d, e has no parent, e is parent of f, f is parent of g,&lt;/P&gt;
&lt;P&gt;so want to found the depth of 'a' and 'e':&lt;/P&gt;
&lt;P&gt;i have given the output for your reference:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data test;&lt;/P&gt;
&lt;P&gt;input name $ parent $;&lt;/P&gt;
&lt;P&gt;datalines;&lt;/P&gt;
&lt;P&gt;a&amp;nbsp;&lt;/P&gt;
&lt;P&gt;b a&lt;/P&gt;
&lt;P&gt;c b&lt;/P&gt;
&lt;P&gt;d c&lt;/P&gt;
&lt;P&gt;e&lt;/P&gt;
&lt;P&gt;f e&lt;/P&gt;
&lt;P&gt;g f&lt;/P&gt;
&lt;P&gt;;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;****Expected Output*****&lt;/P&gt;
&lt;P&gt;name depth last_child&lt;/P&gt;
&lt;P&gt;a &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 3 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;d&lt;/P&gt;
&lt;P&gt;e &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 2 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;g&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have tried to solve this but didn't get correct output.&lt;/P&gt;</description>
      <pubDate>Wed, 17 Jan 2018 16:38:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Finding-depth-and-last-child/m-p/428476#M105793</guid>
      <dc:creator>amol123</dc:creator>
      <dc:date>2018-01-17T16:38:34Z</dc:date>
    </item>
    <item>
      <title>Re: Finding depth and last child</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Finding-depth-and-last-child/m-p/428478#M105795</link>
      <description>&lt;P&gt;Can you show what you tried so we know what approach you're taking?&amp;nbsp;&lt;/P&gt;
&lt;P&gt;There are several ways to solve this type of problem.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/187301"&gt;@amol123&lt;/a&gt; wrote:&lt;BR /&gt;
&lt;P&gt;I have below dataset 'test' which has two columns name and parent:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Explanation=&amp;gt; a has no parent, a is parent of b, b is parent of c, c is parent of d, e has no parent, e is parent of f, f is parent of g,&lt;/P&gt;
&lt;P&gt;so want to found the depth of 'a' and 'e':&lt;/P&gt;
&lt;P&gt;i have given the output for your reference:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data test;&lt;/P&gt;
&lt;P&gt;input name $ parent $;&lt;/P&gt;
&lt;P&gt;datalines;&lt;/P&gt;
&lt;P&gt;a&amp;nbsp;&lt;/P&gt;
&lt;P&gt;b a&lt;/P&gt;
&lt;P&gt;c b&lt;/P&gt;
&lt;P&gt;d c&lt;/P&gt;
&lt;P&gt;e&lt;/P&gt;
&lt;P&gt;f e&lt;/P&gt;
&lt;P&gt;g f&lt;/P&gt;
&lt;P&gt;;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;****Expected Output*****&lt;/P&gt;
&lt;P&gt;name depth last_child&lt;/P&gt;
&lt;P&gt;a &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 3 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;d&lt;/P&gt;
&lt;P&gt;e &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 2 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;g&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have tried to solve this but didn't get correct output.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 17 Jan 2018 16:39:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Finding-depth-and-last-child/m-p/428478#M105795</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2018-01-17T16:39:19Z</dc:date>
    </item>
    <item>
      <title>Re: Finding depth and last child</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Finding-depth-and-last-child/m-p/428746#M105871</link>
      <description>&lt;P&gt;I would try something like this:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
  create index parent on test(parent);
quit;

data want;
  set test(where=(parent is null));
  parent=name;
  do depth=0 to 100; /* we stop at 100 in case there are circular references in data */
    set test(rename=(name=last_child)) key=parent;
    if _iorc_ then leave;
    parent=last_child;
    end;
  _error_=0; /* _ERROR_ is set when a key is not found, this is not an error here */
  keep name depth last_child;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Using SET with KEY= is normally the easiest way to traverse trees. The value 100 is used as I assume that there are no trees with a depth greater than that - so if you find that value it may mean that you have a circular reference in your data.&lt;/P&gt;</description>
      <pubDate>Thu, 18 Jan 2018 11:26:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Finding-depth-and-last-child/m-p/428746#M105871</guid>
      <dc:creator>s_lassen</dc:creator>
      <dc:date>2018-01-18T11:26:15Z</dc:date>
    </item>
    <item>
      <title>Re: Finding depth and last child</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Finding-depth-and-last-child/m-p/428753#M105874</link>
      <description>&lt;P&gt;Yet another way of doing this - starting from the bottom. May be better if several nodes can have the same parent:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
  create index name on test(name);
  create table want as select
    name as last_child,
    0 as depth,
    parent
  from test
  where name not in(select parent from test);
quit;

data want;
  modify want;
  do while(parent ne ' ');
    name=parent;
    set test key=name;
    depth=depth+1;
    end;
  parent=name;
  replace;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 18 Jan 2018 12:02:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Finding-depth-and-last-child/m-p/428753#M105874</guid>
      <dc:creator>s_lassen</dc:creator>
      <dc:date>2018-01-18T12:02:07Z</dc:date>
    </item>
    <item>
      <title>Re: Finding depth and last child</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Finding-depth-and-last-child/m-p/428756#M105876</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/187301"&gt;@amol123&lt;/a&gt;&lt;/P&gt;
&lt;P&gt;Have you already searched the communities here. I know for sure that similar questions have been asked in the past (as I've been one of the people asking) and that solutions have been provided.&lt;/P&gt;</description>
      <pubDate>Thu, 18 Jan 2018 12:06:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Finding-depth-and-last-child/m-p/428756#M105876</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2018-01-18T12:06:56Z</dc:date>
    </item>
    <item>
      <title>Re: Finding depth and last child</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Finding-depth-and-last-child/m-p/428773#M105879</link>
      <description>&lt;P&gt;Does parent could have multiple child , if it was ,then you could end with multiple last child .&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;

data test;
input name $ parent $;
datalines;
a .
b a
c b
d c
e .
f e
g f
;
run;


data have;
 set test(rename=(parent=_start name=_end));
run;

proc sql;
create table parent as
 select *
  from have 
   where _start in (select _end from have where _start is missing);
quit;



data want(keep=path);
if _n_ eq 1 then do;
length path _path  $ 700 ;
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 parent;
count=1;n=1;_n=1;
path=catx('|',_start,_end);
*putlog 'WARNING:Found  ' _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 output;
 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;
   
  * putlog 'WARNING:Found  ' _end;
   count+1;n=count;
   path=catx('|',path,_end);
   pa.add(); 
   path=_path;
 end;
  rc=ha.find_next();
end;
end;
pa.clear();

run;


&lt;/PRE&gt;</description>
      <pubDate>Fri, 19 Jan 2018 13:57:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Finding-depth-and-last-child/m-p/428773#M105879</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2018-01-19T13:57:40Z</dc:date>
    </item>
    <item>
      <title>Re: Finding depth and last child</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Finding-depth-and-last-child/m-p/428883#M105903</link>
      <description>&lt;P&gt;Thank you so much!!!!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 18 Jan 2018 17:16:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Finding-depth-and-last-child/m-p/428883#M105903</guid>
      <dc:creator>amol123</dc:creator>
      <dc:date>2018-01-18T17:16:51Z</dc:date>
    </item>
  </channel>
</rss>

