<?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: Get ancestors for each children in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Get-ancestors-for-each-children/m-p/490683#M128445</link>
    <description>&lt;P&gt;Thank you. I am able to do the same thing through another means using point and last in the data step&lt;/P&gt;</description>
    <pubDate>Wed, 29 Aug 2018 00:19:54 GMT</pubDate>
    <dc:creator>anandbillava</dc:creator>
    <dc:date>2018-08-29T00:19:54Z</dc:date>
    <item>
      <title>Get ancestors for each children</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Get-ancestors-for-each-children/m-p/490584#M128382</link>
      <description>&lt;P&gt;data HIERARCHY;&lt;/P&gt;
&lt;P&gt;input Parent Child;&lt;/P&gt;
&lt;P&gt;datalines;&lt;/P&gt;
&lt;P&gt;. 1&lt;/P&gt;
&lt;P&gt;1 2&lt;/P&gt;
&lt;P&gt;1 3&lt;/P&gt;
&lt;P&gt;1 8&lt;/P&gt;
&lt;P&gt;1 9&lt;/P&gt;
&lt;P&gt;2 4&lt;/P&gt;
&lt;P&gt;3 5&lt;/P&gt;
&lt;P&gt;5 6&lt;/P&gt;
&lt;P&gt;6 7&lt;/P&gt;
&lt;P&gt;8 10&lt;/P&gt;
&lt;P&gt;10 11&lt;/P&gt;
&lt;P&gt;10 13;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;for the above dataset i want to find the ancestor lineage for each of the records. Can you please help.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Output would look as below.&lt;/P&gt;
&lt;P&gt;datalines;&lt;/P&gt;
&lt;P&gt;parent child ancestor_lineage&lt;/P&gt;
&lt;P&gt;1&amp;nbsp; &amp;nbsp;2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1&lt;/P&gt;
&lt;P&gt;1&amp;nbsp; &amp;nbsp;3&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1&lt;/P&gt;
&lt;P&gt;1&amp;nbsp; &amp;nbsp;8&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1&lt;/P&gt;
&lt;P&gt;1&amp;nbsp; &amp;nbsp;9&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1&lt;/P&gt;
&lt;P&gt;2&amp;nbsp; &amp;nbsp;4&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2,1&lt;/P&gt;
&lt;P&gt;3&amp;nbsp; &amp;nbsp;5&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;3.1&lt;/P&gt;
&lt;P&gt;5&amp;nbsp; &amp;nbsp;6&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;5,3,1&lt;/P&gt;
&lt;P&gt;6&amp;nbsp; &amp;nbsp;7&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;6,5,3,1&lt;/P&gt;
&lt;P&gt;8&amp;nbsp; &amp;nbsp;10&amp;nbsp; &amp;nbsp; &amp;nbsp;8&lt;/P&gt;
&lt;P&gt;10 11&amp;nbsp; &amp;nbsp; &amp;nbsp;10, 8&lt;/P&gt;
&lt;P&gt;10 13;&amp;nbsp; &amp;nbsp; 10,8&lt;/P&gt;</description>
      <pubDate>Tue, 28 Aug 2018 19:07:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Get-ancestors-for-each-children/m-p/490584#M128382</guid>
      <dc:creator>anandbillava</dc:creator>
      <dc:date>2018-08-28T19:07:48Z</dc:date>
    </item>
    <item>
      <title>Re: Get ancestors for each children</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Get-ancestors-for-each-children/m-p/490676#M128439</link>
      <description>&lt;P&gt;Using an index,&amp;nbsp;inspired by&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/462"&gt;@PGStats&lt;/a&gt;'s solution &lt;A href="https://communities.sas.com/t5/SAS-Programming/Parent-Child-hierarchy/td-p/293028" target="_self"&gt;here&lt;/A&gt;.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;A hash table could be used in a similar manner if&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/21262"&gt;@hashman&lt;/a&gt;&amp;nbsp;is inspired...&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data WANT;
  set HAVE;
  if PARENT;
  LINEAGE=cat(PARENT);
  KEY=PARENT;
  do I = 1 to 100 ; %* catch infinite loops ;
    set HAVE(rename=(PARENT=P CHILD=KEY)) key=KEY / unique;
    if _error_ or missing(P) then leave;
    LINEAGE=catx(',',LINEAGE,P);
    KEY=P;     
  end;
  output;
  _error_ = 0;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;TABLE style="border-collapse: collapse; width: 144pt;" border="0" width="192" cellspacing="0" cellpadding="0"&gt;
&lt;TBODY&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD width="64" height="20" style="height: 15.0pt; width: 48pt;"&gt;PARENT&lt;/TD&gt;
&lt;TD width="64" style="width: 48pt;"&gt;CHILD&lt;/TD&gt;
&lt;TD width="64" class="xl65" style="width: 48pt;"&gt;LINEAGE&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD height="20" align="right" style="height: 15.0pt;"&gt;1&lt;/TD&gt;
&lt;TD align="right"&gt;2&lt;/TD&gt;
&lt;TD class="xl65"&gt;1&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD height="20" align="right" style="height: 15.0pt;"&gt;1&lt;/TD&gt;
&lt;TD align="right"&gt;3&lt;/TD&gt;
&lt;TD class="xl65"&gt;1&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD height="20" align="right" style="height: 15.0pt;"&gt;1&lt;/TD&gt;
&lt;TD align="right"&gt;8&lt;/TD&gt;
&lt;TD class="xl65"&gt;1&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD height="20" align="right" style="height: 15.0pt;"&gt;1&lt;/TD&gt;
&lt;TD align="right"&gt;9&lt;/TD&gt;
&lt;TD class="xl65"&gt;1&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD height="20" align="right" style="height: 15.0pt;"&gt;2&lt;/TD&gt;
&lt;TD align="right"&gt;4&lt;/TD&gt;
&lt;TD class="xl65"&gt;2,1&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD height="20" align="right" style="height: 15.0pt;"&gt;3&lt;/TD&gt;
&lt;TD align="right"&gt;5&lt;/TD&gt;
&lt;TD class="xl65"&gt;3,1&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD height="20" align="right" style="height: 15.0pt;"&gt;5&lt;/TD&gt;
&lt;TD align="right"&gt;6&lt;/TD&gt;
&lt;TD class="xl65"&gt;5,3,1&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD height="20" align="right" style="height: 15.0pt;"&gt;6&lt;/TD&gt;
&lt;TD align="right"&gt;7&lt;/TD&gt;
&lt;TD class="xl65"&gt;6,5,3,1&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD height="20" align="right" style="height: 15.0pt;"&gt;8&lt;/TD&gt;
&lt;TD align="right"&gt;10&lt;/TD&gt;
&lt;TD class="xl65"&gt;8,1&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD height="20" align="right" style="height: 15.0pt;"&gt;10&lt;/TD&gt;
&lt;TD align="right"&gt;11&lt;/TD&gt;
&lt;TD class="xl65"&gt;10,8,1&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD height="20" align="right" style="height: 15.0pt;"&gt;10&lt;/TD&gt;
&lt;TD align="right"&gt;13&lt;/TD&gt;
&lt;TD class="xl65"&gt;10,8,1&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 29 Aug 2018 04:01:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Get-ancestors-for-each-children/m-p/490676#M128439</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2018-08-29T04:01:17Z</dc:date>
    </item>
    <item>
      <title>Re: Get ancestors for each children</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Get-ancestors-for-each-children/m-p/490683#M128445</link>
      <description>&lt;P&gt;Thank you. I am able to do the same thing through another means using point and last in the data step&lt;/P&gt;</description>
      <pubDate>Wed, 29 Aug 2018 00:19:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Get-ancestors-for-each-children/m-p/490683#M128445</guid>
      <dc:creator>anandbillava</dc:creator>
      <dc:date>2018-08-29T00:19:54Z</dc:date>
    </item>
    <item>
      <title>Re: Get ancestors for each children</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Get-ancestors-for-each-children/m-p/490711#M128463</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/16856"&gt;@anandbillava&lt;/a&gt;&amp;nbsp;I'd like to see that code. Please post!&lt;/P&gt;</description>
      <pubDate>Wed, 29 Aug 2018 03:28:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Get-ancestors-for-each-children/m-p/490711#M128463</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2018-08-29T03:28:47Z</dc:date>
    </item>
    <item>
      <title>Re: Get ancestors for each children</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Get-ancestors-for-each-children/m-p/490784#M128513</link>
      <description>&lt;P&gt;Here it is&lt;/P&gt;
&lt;P&gt;data test;&lt;BR /&gt; input child parent;&lt;BR /&gt;datalines;&lt;BR /&gt;23 11&lt;BR /&gt;99 88&lt;BR /&gt;77 66&lt;BR /&gt;72 68&lt;BR /&gt;88 77&lt;BR /&gt;66 1&lt;BR /&gt;68 55&lt;BR /&gt;69 55&lt;BR /&gt;70 55&lt;BR /&gt;55 56&lt;BR /&gt;03 99&lt;/P&gt;
&lt;P&gt;;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data lineage(keep=hist1-hist10 flag);&lt;BR /&gt; &lt;BR /&gt; set test;&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt; array hist(10);&lt;BR /&gt; count=1;&lt;BR /&gt; &lt;BR /&gt; hist(count)=child;&lt;BR /&gt; &lt;BR /&gt; &lt;BR /&gt; do i=1 to last while (count&amp;lt;dim(hist)-1);&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt; set test(rename=(child=child1 parent=parent1)) nobs=last point=i;&lt;/P&gt;
&lt;P&gt;if parent=child1 then do;&lt;BR /&gt; count+1;&lt;BR /&gt; &lt;BR /&gt; hist(count)=child1;&lt;BR /&gt; child=child1;&lt;BR /&gt; parent=parent1;&lt;BR /&gt; &lt;BR /&gt; i=1;&lt;BR /&gt; end;&lt;BR /&gt; end;&lt;BR /&gt; &lt;BR /&gt; count+1;&lt;BR /&gt; hist(count)=parent;&lt;BR /&gt; flag = parent;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 29 Aug 2018 11:32:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Get-ancestors-for-each-children/m-p/490784#M128513</guid>
      <dc:creator>anandbillava</dc:creator>
      <dc:date>2018-08-29T11:32:10Z</dc:date>
    </item>
    <item>
      <title>Re: Get ancestors for each children</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Get-ancestors-for-each-children/m-p/491055#M128675</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/16961"&gt;@ChrisNZ&lt;/a&gt;:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But of course:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have ;                                         
  input parent child ;                              
  cards ;                                           
 .   1                                              
 1   2                                              
 1   3                                              
 1   8                                              
 1   9                                              
 2   4                                              
 3   5                                              
 5   6                                              
 6   7                                              
 8  10                                              
10  11                                              
10  13                                              
run ;                                               
                                                    
data want ;                                         
  if _n_ = 1 then do ;                              
    dcl hash h (dataset: "have (where = (N (parent))") ; 
    h.defineKey ("child") ;                         
    h.defineData ("parent") ;                       
    h.defineDone () ;                               
  end ;                                             
  set have (where = (N (parent))) ;                    
  lineage = put (parent, 32.-L) ;              
  do while (not h.find(key:parent)) ;  
    lineage = catx (",", lineage, parent) ;         
  end ;                                             
run ;                                               
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Best,&lt;/P&gt;
&lt;P&gt;Paul D.&lt;/P&gt;</description>
      <pubDate>Thu, 30 Aug 2018 02:51:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Get-ancestors-for-each-children/m-p/491055#M128675</guid>
      <dc:creator>hashman</dc:creator>
      <dc:date>2018-08-30T02:51:38Z</dc:date>
    </item>
    <item>
      <title>Re: Get ancestors for each children</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Get-ancestors-for-each-children/m-p/491056#M128676</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/21262"&gt;@hashman&lt;/a&gt;&amp;nbsp;Almost! You are overwriting&amp;nbsp;the parent &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 30 Aug 2018 02:56:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Get-ancestors-for-each-children/m-p/491056#M128676</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2018-08-30T02:56:58Z</dc:date>
    </item>
    <item>
      <title>Re: Get ancestors for each children</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Get-ancestors-for-each-children/m-p/491060#M128679</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/16961"&gt;@ChrisNZ&lt;/a&gt;:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;A good eye. Quite forgot to resurrect the killed parents:&lt;/P&gt;
&lt;PRE&gt;data want ;                                        
  if _n_ = 1 then do ;                             
    dcl hash h (dataset:"have(where=(N(parent))") ;
    h.defineKey ("child") ;                        
    h.defineData ("parent") ;                      
    h.defineDone () ;                              
  end ;                                            
  set have (where=(N(parent))) ;                   
  _n_ = parent ;                                   
  lineage = put (parent, 32.-L) ;                  
  do while (not h.find(key:parent)) ;              
    lineage = catx (",", lineage, parent) ;        
  end ;                                            
  parent = _n_ ;                                   
run ;                                              
&lt;/PRE&gt;
&lt;P&gt;Best,&lt;/P&gt;
&lt;P&gt;Paul D.&lt;/P&gt;</description>
      <pubDate>Thu, 30 Aug 2018 03:23:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Get-ancestors-for-each-children/m-p/491060#M128679</guid>
      <dc:creator>hashman</dc:creator>
      <dc:date>2018-08-30T03:23:16Z</dc:date>
    </item>
    <item>
      <title>Re: Get ancestors for each children</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Get-ancestors-for-each-children/m-p/491062#M128680</link>
      <description>&lt;P&gt;Miracle!&lt;/P&gt;</description>
      <pubDate>Thu, 30 Aug 2018 03:24:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Get-ancestors-for-each-children/m-p/491062#M128680</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2018-08-30T03:24:16Z</dc:date>
    </item>
    <item>
      <title>Re: Get ancestors for each children</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Get-ancestors-for-each-children/m-p/491063#M128681</link>
      <description>&lt;P&gt;What can't you do with the power of SAS &lt;span class="lia-unicode-emoji" title=":winking_face:"&gt;😉&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 30 Aug 2018 03:26:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Get-ancestors-for-each-children/m-p/491063#M128681</guid>
      <dc:creator>hashman</dc:creator>
      <dc:date>2018-08-30T03:26:22Z</dc:date>
    </item>
  </channel>
</rss>

