<?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 write a SAS program to find relationship between nodes in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-write-a-SAS-program-to-find-relationship-between-nodes/m-p/917857#M361554</link>
    <description>&lt;P&gt;Why do you call your variables Table_a and Table_b and not something like Parent and Child? The name Table... for a variable is rather confusing.&lt;/P&gt;
&lt;P&gt;Are your networks only tree structures or could they also be circular? And if not only tree structures then what should happen in such cases?&lt;/P&gt;
&lt;P&gt;Do you have one or multiple networks? (=one or multiple nodes without a parent in case of tree structures only?)&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Patrick_0-1708910772515.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/94116iEAC454ADED3FE423/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Patrick_0-1708910772515.png" alt="Patrick_0-1708910772515.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It's also worth to search the communities a bit because similar questions have been asked in the past.&lt;/P&gt;
&lt;P&gt;Below three such discussions I found via a quick search (and I know there are more discussions with solutions around).&lt;BR /&gt;&lt;A href="https://communities.sas.com/t5/SAS-Programming/Nodes-Links-Routes/m-p/80086" target="_blank"&gt;https://communities.sas.com/t5/SAS-Programming/Nodes-Links-Routes/m-p/80086&lt;/A&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;A href="https://communities.sas.com/t5/SAS-Programming/How-to-find-link-between-nodes/m-p/341819" target="_blank"&gt;https://communities.sas.com/t5/SAS-Programming/How-to-find-link-between-nodes/m-p/341819&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&lt;A href="https://communities.sas.com/t5/SAS-Programming/Find-last-child/m-p/113752" target="_blank"&gt;https://communities.sas.com/t5/SAS-Programming/Find-last-child/m-p/113752&lt;/A&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Mon, 26 Feb 2024 01:48:35 GMT</pubDate>
    <dc:creator>Patrick</dc:creator>
    <dc:date>2024-02-26T01:48:35Z</dc:date>
    <item>
      <title>How to write a SAS program to find relationship between nodes</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-write-a-SAS-program-to-find-relationship-between-nodes/m-p/917849#M361552</link>
      <description>&lt;P&gt;I have two tables:&lt;BR /&gt;&lt;BR /&gt;Table1 and Table2&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Data in Table1 can be like:&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;
&lt;P&gt;Table_a&amp;nbsp; &amp;nbsp; &amp;nbsp; Table_b&lt;/P&gt;
&lt;P&gt;A&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; B&lt;BR /&gt;A&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; C&lt;BR /&gt;C&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; E&lt;BR /&gt;B&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;D&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;
&lt;P&gt;Data in table2 can be like:&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;
&lt;P&gt;Table_name&amp;nbsp; &amp;nbsp; &amp;nbsp; Status&lt;BR /&gt;A&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Completed&lt;BR /&gt;B&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;NOTCOMPLETED&lt;BR /&gt;C&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; COMPLETED&lt;BR /&gt;D&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; NOTCOMPLETED&lt;BR /&gt;E&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; OK&lt;BR /&gt;&lt;BR /&gt;my output should be like:&lt;BR /&gt;Table1&amp;nbsp; &amp;nbsp;status1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Table2&amp;nbsp; &amp;nbsp; &amp;nbsp;status2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;table3 status3&lt;BR /&gt;A&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;Completed&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;B&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;NOTCOMPLETED&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;D&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;NOTCOMPLETED&lt;BR /&gt;A&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Completed&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;C&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;COMPLETED&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;E&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;OK&lt;BR /&gt;&lt;BR /&gt;I want to write all relations in one single row&lt;/P&gt;</description>
      <pubDate>Mon, 26 Feb 2024 00:06:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-write-a-SAS-program-to-find-relationship-between-nodes/m-p/917849#M361552</guid>
      <dc:creator>gyambqt</dc:creator>
      <dc:date>2024-02-26T00:06:13Z</dc:date>
    </item>
    <item>
      <title>Re: How to write a SAS program to find relationship between nodes</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-write-a-SAS-program-to-find-relationship-between-nodes/m-p/917857#M361554</link>
      <description>&lt;P&gt;Why do you call your variables Table_a and Table_b and not something like Parent and Child? The name Table... for a variable is rather confusing.&lt;/P&gt;
&lt;P&gt;Are your networks only tree structures or could they also be circular? And if not only tree structures then what should happen in such cases?&lt;/P&gt;
&lt;P&gt;Do you have one or multiple networks? (=one or multiple nodes without a parent in case of tree structures only?)&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Patrick_0-1708910772515.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/94116iEAC454ADED3FE423/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Patrick_0-1708910772515.png" alt="Patrick_0-1708910772515.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It's also worth to search the communities a bit because similar questions have been asked in the past.&lt;/P&gt;
&lt;P&gt;Below three such discussions I found via a quick search (and I know there are more discussions with solutions around).&lt;BR /&gt;&lt;A href="https://communities.sas.com/t5/SAS-Programming/Nodes-Links-Routes/m-p/80086" target="_blank"&gt;https://communities.sas.com/t5/SAS-Programming/Nodes-Links-Routes/m-p/80086&lt;/A&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;A href="https://communities.sas.com/t5/SAS-Programming/How-to-find-link-between-nodes/m-p/341819" target="_blank"&gt;https://communities.sas.com/t5/SAS-Programming/How-to-find-link-between-nodes/m-p/341819&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&lt;A href="https://communities.sas.com/t5/SAS-Programming/Find-last-child/m-p/113752" target="_blank"&gt;https://communities.sas.com/t5/SAS-Programming/Find-last-child/m-p/113752&lt;/A&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 26 Feb 2024 01:48:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-write-a-SAS-program-to-find-relationship-between-nodes/m-p/917857#M361554</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2024-02-26T01:48:35Z</dc:date>
    </item>
    <item>
      <title>Re: How to write a SAS program to find relationship between nodes</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-write-a-SAS-program-to-find-relationship-between-nodes/m-p/917869#M361561</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt; /**********Search a tree **************/
data table1;
input Table_a   $   Table_b $;
cards;
A                  B
A                  C
C                  E
B                   D
;
data table2;
input Table_name  $   Status : $20.;
cards;
A                          Completed
B                           NOTCOMPLETED
C                          COMPLETED
D                          NOTCOMPLETED
E                              OK
;





data have;
 set table1(rename=(Table_a=_start   Table_b=_end));
run;
proc sql;
create table  ancestor as
select * from have
 where _start not in (select _end from have);
quit;
 

data want(keep=path);
if _n_ eq 1 then do;
length path _path  $ 800 ;
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 ancestor;
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; /*it is a circle.*/
  rc=ha.find_next();
end;
end;
pa.clear();
run;
data want2;
 set want;
 id+1;
 length table $ 80;
 do i=1 to countw(path,'|');
  table=scan(path,i,'|');output;
 end;
 keep id table;
run;
data want3;
if _n_=1 then do;
 if 0 then set want2;
 if 0 then set table2;
 declare hash h(dataset:'table2');
 h.definekey('Table_name');
 h.definedata('Status');
 h.definedone();
end;
set want2;
call missing(Status);
rc=h.find(key:table);
drop rc Table_name;
run;
data want4;
 set want3;
 by id;
 if first.id then n=0;
 n+1;
run;
proc sql noprint;
select distinct catt('want4(where=(n=',n,') rename=(table=table',n,' status=status',n,'))') into :merge separated by ' '
 from want4;
quit;

data want5;
 merge &amp;amp;merge.;
 by id;
 drop n;
 run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Ksharp_0-1708929001197.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/94119i350963C4B38C985A/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Ksharp_0-1708929001197.png" alt="Ksharp_0-1708929001197.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 26 Feb 2024 06:30:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-write-a-SAS-program-to-find-relationship-between-nodes/m-p/917869#M361561</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2024-02-26T06:30:13Z</dc:date>
    </item>
  </channel>
</rss>

