<?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: How to find great grand parents in the dataset in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-find-great-grand-parents-in-the-dataset/m-p/727138#M226086</link>
    <description>Hi William,&lt;BR /&gt;Thanks for you solution.. It is working perfectly fine. But, I have few doubts..&lt;BR /&gt;1) What's the purpose of using type 'C'. Is it used to mention fmtname is character variable?. I tried with using Dollar symbol($) and _CHARACTER_ but it throughing some error... It will be helpful if i got to know the purpose&lt;BR /&gt;&lt;BR /&gt;2)Why we are using hlo='0'. I know it has to do with formatting.. Because it throughing error if we removed that line. So i am not sure about the purpose of this..</description>
    <pubDate>Wed, 17 Mar 2021 15:40:17 GMT</pubDate>
    <dc:creator>_el_doredo</dc:creator>
    <dc:date>2021-03-17T15:40:17Z</dc:date>
    <item>
      <title>How to find great grand parents in the dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-find-great-grand-parents-in-the-dataset/m-p/726480#M225729</link>
      <description>&lt;P&gt;Hi Experts,&lt;/P&gt;&lt;P&gt;I need to find out great grandparent for the child using this dataset.&lt;/P&gt;&lt;P&gt;DATA hierarchy;&lt;/P&gt;&lt;P&gt;input parent $ child $;&lt;/P&gt;&lt;P&gt;cards;&lt;/P&gt;&lt;P&gt;A B&lt;/P&gt;&lt;P&gt;B K&lt;/P&gt;&lt;P&gt;K C&lt;/P&gt;&lt;P&gt;;&lt;/P&gt;&lt;P&gt;RUN;&lt;/P&gt;&lt;P&gt;In this dataset, Value C's parent is K and C's grandparent is B and C's great grandparent A. So i need result like this&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="_el_doredo_0-1615830145745.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/55973i90BB3CF24A4DA305/image-size/medium?v=v2&amp;amp;px=400" role="button" title="_el_doredo_0-1615830145745.png" alt="_el_doredo_0-1615830145745.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;I tried with PROC SQL. I am able to find out till Grandparent but i am not able to find out Great_grandparent hierarchy.&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;&lt;STRONG&gt;I used this code&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;proc sql;&lt;BR /&gt;select child,parent,&lt;BR /&gt;(select parent from hierarchy where child=a.parent) as grandparent&lt;BR /&gt;from hierarchy as a&lt;BR /&gt;where child='C';&lt;BR /&gt;quit;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;but i am not able to find great grandparent...&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Can you guys please suggest a way to achieve this use proc sql itself&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks in advance&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Regards,&lt;/P&gt;&lt;P&gt;Syed&lt;/P&gt;</description>
      <pubDate>Mon, 15 Mar 2021 17:43:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-find-great-grand-parents-in-the-dataset/m-p/726480#M225729</guid>
      <dc:creator>_el_doredo</dc:creator>
      <dc:date>2021-03-15T17:43:48Z</dc:date>
    </item>
    <item>
      <title>Re: How to find great grand parents in the dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-find-great-grand-parents-in-the-dataset/m-p/726484#M225731</link>
      <description>Have you tried repeating that one more time? Separate it into two separate steps. &lt;BR /&gt;&lt;BR /&gt;If you have SAS/OR you can also look at PROC BOM or there's a subgraph macro. Your problem is analogous to a bill of materials problem where one item is composed of others or the graph problem of looking for a path or node in a graph.</description>
      <pubDate>Mon, 15 Mar 2021 17:45:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-find-great-grand-parents-in-the-dataset/m-p/726484#M225731</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2021-03-15T17:45:20Z</dc:date>
    </item>
    <item>
      <title>Re: How to find great grand parents in the dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-find-great-grand-parents-in-the-dataset/m-p/726486#M225732</link>
      <description>&lt;P&gt;Hi Reeza,&lt;/P&gt;&lt;P&gt;So far i didn't used PROC BOM or subgraph... If you send a code using PROC BOM or subgraph means it will be useful for my learning.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I tried with repeating the code one more time but not getting expected results..&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks,&lt;BR /&gt;Syed&lt;/P&gt;</description>
      <pubDate>Mon, 15 Mar 2021 17:49:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-find-great-grand-parents-in-the-dataset/m-p/726486#M225732</guid>
      <dc:creator>_el_doredo</dc:creator>
      <dc:date>2021-03-15T17:49:17Z</dc:date>
    </item>
    <item>
      <title>Re: How to find great grand parents in the dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-find-great-grand-parents-in-the-dataset/m-p/726490#M225735</link>
      <description>&lt;P&gt;You could create a format to do the child-&amp;gt;parent lookup and then use this to create the grandparent/greatgrandparent in a datastep&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data fmt_hier / view=fmt_hier;
  retain 
    fmtName 'parent'
    type 'c'
  ;
  
  set hierarchy(rename=(child=start parent=label)) end=eof;
  output;
  
  if eof then do;
    hlo='O';
    label='?';
    output;
  end;
run;
proc format cntlin=fmt_hier;
run;

data parents;
  set hierarchy;
  
  grandparent = put(parent, $parent.);
  greatgrandparent = put(grandparent, $parent.);
run;

proc print data=parents;
  id child;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Result:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="JonathanWilliamson_1-1615832560105.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/55975i3344C2A700937EEC/image-size/medium?v=v2&amp;amp;px=400" role="button" title="JonathanWilliamson_1-1615832560105.png" alt="JonathanWilliamson_1-1615832560105.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 15 Mar 2021 18:22:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-find-great-grand-parents-in-the-dataset/m-p/726490#M225735</guid>
      <dc:creator>JonathanWill</dc:creator>
      <dc:date>2021-03-15T18:22:51Z</dc:date>
    </item>
    <item>
      <title>Re: How to find great grand parents in the dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-find-great-grand-parents-in-the-dataset/m-p/726494#M225737</link>
      <description>&lt;P&gt;A hash approach&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA hierarchy;
input parent $ child $;
cards;
A B
B K
K C
;

data want(drop = rc);

   format child parent grandparent greatgrandparent;

   if _N_ = 1 then do;
      dcl hash h(dataset : "hierarchy");
      h.definekey("child");
      h.definedata("parent");
      h.definedone();
   end;
   
   set hierarchy;

   rc = h.find(key : parent);

   if rc = 0 then do;
      grandparent = parent;
      rc = h.find(key : grandparent);
   end;

   if rc = 0 then greatgrandparent = parent;

   rc = h.find();

run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Result:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;child parent grandparent greatgrandparent 
B     A          
K     B      A   
C     K      B           A &lt;/PRE&gt;</description>
      <pubDate>Mon, 15 Mar 2021 18:28:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-find-great-grand-parents-in-the-dataset/m-p/726494#M225737</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2021-03-15T18:28:07Z</dc:date>
    </item>
    <item>
      <title>Re: How to find great grand parents in the dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-find-great-grand-parents-in-the-dataset/m-p/726495#M225738</link>
      <description>&lt;P&gt;This is a ready-made task for using hash objects in a data step:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA hierarchy;
  input child $ parent $ ;
cards;
B A
K B
C K
RUN;
data want (drop=_:);
  set hierarchy;
  if _n_=1 then do;
    if 0 then set hierarchy (rename=(child=_c parent=_p));
    declare hash h (dataset:'hierarchy (rename=(child=_c parent=_p))');
      h.definekey('_C');
      h.definedata('_P');
      h.definedone();
      h.output(dataset:'h');
  end;
  _rc=h.find(key:parent);
  if _rc=0 then do;
    grandparent=_P;
    _rc=h.find(key:grandparent);
    if _rc=0 then great_grandparent=_P;
  end;
  if great_grandparent^=' ';
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 15 Mar 2021 18:27:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-find-great-grand-parents-in-the-dataset/m-p/726495#M225738</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2021-03-15T18:27:13Z</dc:date>
    </item>
    <item>
      <title>Re: How to find great grand parents in the dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-find-great-grand-parents-in-the-dataset/m-p/726518#M225749</link>
      <description>Please show what you've tried. You really are just repeating the exact same logic one more time, replacing the parents with the grandparents to find the great grand parents.</description>
      <pubDate>Mon, 15 Mar 2021 19:37:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-find-great-grand-parents-in-the-dataset/m-p/726518#M225749</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2021-03-15T19:37:06Z</dc:date>
    </item>
    <item>
      <title>Re: How to find great grand parents in the dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-find-great-grand-parents-in-the-dataset/m-p/726712#M225853</link>
      <description>&lt;PRE&gt;DATA hierarchy;
input parent $ child $;
cards;
A B
B K
K C
;

data have;
set hierarchy;
rename parent=_start child=_end;
run;

 
proc sql;
create table ancient as
 select _start,_end from have
  where _start not in (select distinct _end from have);
run;
data want(keep=path);
if _n_ eq 1 then do;
length path _path  $ 400 ;
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;
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 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;
   
   count+1;n=count;
   path=catx('|',path,_end);
   pa.add();
   path=_path;
 end; else output;
  rc=ha.find_next();
end;
end;
pa.clear();
run;
&lt;/PRE&gt;</description>
      <pubDate>Tue, 16 Mar 2021 12:00:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-find-great-grand-parents-in-the-dataset/m-p/726712#M225853</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2021-03-16T12:00:54Z</dc:date>
    </item>
    <item>
      <title>Re: How to find great grand parents in the dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-find-great-grand-parents-in-the-dataset/m-p/727134#M226085</link>
      <description>&lt;P&gt;Hi Reez,&lt;/P&gt;&lt;P&gt;I tried this way and it worked.I am looking for any better code to use with all type of hierarchy scenarios&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data hierarchy;&lt;BR /&gt;input parent $ child $ ;&lt;BR /&gt;cards;&lt;BR /&gt;A B&lt;BR /&gt;B K&lt;BR /&gt;K C&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;proc sql;&lt;BR /&gt;create table master as&lt;BR /&gt;select A.parent from hierarchy as A&lt;BR /&gt;union all&lt;BR /&gt;select A.child from hierarchy as A&lt;BR /&gt;where A.child not in (select B.parent from hierarchy as B)&lt;BR /&gt;;&lt;BR /&gt;quit;&lt;/P&gt;&lt;P&gt;data dummy(drop=counter);&lt;BR /&gt;set master;&lt;BR /&gt;counter=_n_;&lt;BR /&gt;if counter=1 then relationship='great grand parent';&lt;BR /&gt;else if counter=2 then relationship='grand parent';&lt;BR /&gt;else if counter=3 then relationship='parent';&lt;BR /&gt;else if counter=4 then relationship='child';&lt;BR /&gt;proc print noobs;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;proc transpose data=dummy out=final(drop=_name_);&lt;BR /&gt;id relationship;&lt;BR /&gt;var parent;&lt;BR /&gt;run;&lt;/P&gt;</description>
      <pubDate>Wed, 17 Mar 2021 15:28:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-find-great-grand-parents-in-the-dataset/m-p/727134#M226085</guid>
      <dc:creator>_el_doredo</dc:creator>
      <dc:date>2021-03-17T15:28:26Z</dc:date>
    </item>
    <item>
      <title>Re: How to find great grand parents in the dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-find-great-grand-parents-in-the-dataset/m-p/727138#M226086</link>
      <description>Hi William,&lt;BR /&gt;Thanks for you solution.. It is working perfectly fine. But, I have few doubts..&lt;BR /&gt;1) What's the purpose of using type 'C'. Is it used to mention fmtname is character variable?. I tried with using Dollar symbol($) and _CHARACTER_ but it throughing some error... It will be helpful if i got to know the purpose&lt;BR /&gt;&lt;BR /&gt;2)Why we are using hlo='0'. I know it has to do with formatting.. Because it throughing error if we removed that line. So i am not sure about the purpose of this..</description>
      <pubDate>Wed, 17 Mar 2021 15:40:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-find-great-grand-parents-in-the-dataset/m-p/727138#M226086</guid>
      <dc:creator>_el_doredo</dc:creator>
      <dc:date>2021-03-17T15:40:17Z</dc:date>
    </item>
    <item>
      <title>Re: How to find great grand parents in the dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-find-great-grand-parents-in-the-dataset/m-p/727139#M226087</link>
      <description>Hi Clemmensen,&lt;BR /&gt;Thanks for your solution... It working fine as expected.. I am not familiar with hash approach.. So thanks for introducing one new concept to me...</description>
      <pubDate>Wed, 17 Mar 2021 15:41:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-find-great-grand-parents-in-the-dataset/m-p/727139#M226087</guid>
      <dc:creator>_el_doredo</dc:creator>
      <dc:date>2021-03-17T15:41:33Z</dc:date>
    </item>
    <item>
      <title>Re: How to find great grand parents in the dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-find-great-grand-parents-in-the-dataset/m-p/727142#M226088</link>
      <description>Hi Mkeintz,&lt;BR /&gt;Thanks for your solution... It working fine as expected.. I am not familiar with hash approach.. So thanks for introducing one new concept to me...</description>
      <pubDate>Wed, 17 Mar 2021 15:49:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-find-great-grand-parents-in-the-dataset/m-p/727142#M226088</guid>
      <dc:creator>_el_doredo</dc:creator>
      <dc:date>2021-03-17T15:49:41Z</dc:date>
    </item>
    <item>
      <title>Re: How to find great grand parents in the dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-find-great-grand-parents-in-the-dataset/m-p/727143#M226089</link>
      <description>Hi Ksharp,&lt;BR /&gt;Thanks for the solution.Let me try this from my end</description>
      <pubDate>Wed, 17 Mar 2021 15:50:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-find-great-grand-parents-in-the-dataset/m-p/727143#M226089</guid>
      <dc:creator>_el_doredo</dc:creator>
      <dc:date>2021-03-17T15:50:57Z</dc:date>
    </item>
    <item>
      <title>Re: How to find great grand parents in the dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-find-great-grand-parents-in-the-dataset/m-p/727339#M226198</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/366820"&gt;@_el_doredo&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;1) Correct.&amp;nbsp; The type = 'C' identifies it as a character format.&amp;nbsp; &amp;nbsp;'N' would be numeric format, 'I' numeric informat.&amp;nbsp; &amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;2) The if eof block that contains hlo='O' creates an extra entry in the format that specifies the OTHER value.&amp;nbsp; So this will be used for any input that is not in the known values.&amp;nbsp; &amp;nbsp;If you do not have this in the format any unknown input value will simply give the input value back.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;i.e. Using the format above &lt;CODE&gt;put("Z", $parent.)&lt;/CODE&gt; would give "?". Without the OTHER entry it would give "Z".&lt;BR /&gt;&lt;BR /&gt;If you didn't want the OTHER entry then you should remove the whole of the block of code 'if eof then...' to the 'end;'.&amp;nbsp; If you removed only the hlo='O' line then you will get a duplicate 'start' value.&amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 18 Mar 2021 09:59:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-find-great-grand-parents-in-the-dataset/m-p/727339#M226198</guid>
      <dc:creator>JonathanWill</dc:creator>
      <dc:date>2021-03-18T09:59:27Z</dc:date>
    </item>
    <item>
      <title>Re: How to find great grand parents in the dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-find-great-grand-parents-in-the-dataset/m-p/727452#M226252</link>
      <description>&lt;P&gt;Here is how I would do it in SQL, although I prefer the hash approach:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
  create view _3gens as select h.*, h2.parent as grandparent 
   from hierarchy as h
   inner join 
        hierarchy as h2
   on h.parent=h2.child;

  create table want as select _3gens.*, h3.parent as greatgrandparent
   from _3gens
   inner join
       hierarchy as h3
  on _3gens.grandparent=h3.child;
quit;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Or, if you want to do it all in one statement:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
  create table want as select _3gens.*,h3.parent as greatgrandparent from
   (select h.*, h2.parent as grandparent from
      hierarchy as h   inner join 
      hierarchy as h2  on h.parent=h2.child) 
   as _3gens
   inner join
     hierarchy as h3
  on _3gens.grandparent=h3.child;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;But imagine how ugly this would look if you wanted, say 6 generations.&amp;nbsp; The hash coding approach would be far easier to expand.&lt;/P&gt;</description>
      <pubDate>Thu, 18 Mar 2021 15:47:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-find-great-grand-parents-in-the-dataset/m-p/727452#M226252</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2021-03-18T15:47:40Z</dc:date>
    </item>
    <item>
      <title>Re: How to find great grand parents in the dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-find-great-grand-parents-in-the-dataset/m-p/727464#M226263</link>
      <description>&lt;P&gt;I said in a separate response that I preferred the hash approach to the sql approach, primarily because the hash approach is easier to expand to more generations.&amp;nbsp; Here's a hash usage that goes to 6 generations, with only the most minor modifications to the program.&amp;nbsp; (I have added 2 generations to your sample):&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA hierarchy;
  input child $ parent $ ;
cards;
B A
K B
C K
P C
O P
RUN;

data want (drop=_:);
  set hierarchy;
  if _n_=1 then do;
    if 0 then set hierarchy (rename=(child=_c parent=_p));
    declare hash h (dataset:'hierarchy (rename=(child=_c parent=_p))');
      h.definekey('_C');
      h.definedata('_P');
      h.definedone();
      h.output(dataset:'h');
  end;
  array lineage {*} $1 child parent gpar g_gpar gg_gpar ggg_gpar;
  do _i=2 to dim(lineage)-1 while (h.find(key:lineage{_i})=0);
    lineage{_i+1}=_p;
  end;
  if lineage{dim(lineage)}^=' ';
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;To modify this to any number of generations, all you have to do is to modify the number of variables named in the LINEAGE array.&amp;nbsp; Compare that to what would be required in an SQL approach.&lt;/P&gt;</description>
      <pubDate>Thu, 18 Mar 2021 16:31:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-find-great-grand-parents-in-the-dataset/m-p/727464#M226263</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2021-03-18T16:31:05Z</dc:date>
    </item>
  </channel>
</rss>

