<?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 perform this in sas? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Recursive-lookup-for-ID-s/m-p/259669#M50254</link>
    <description>&lt;P&gt;Hello&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/41912"&gt;@munitech4u﻿&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;That's interesting! Last week&amp;nbsp;the same sort of question was asked by someone else. Ksharp and I have provided two different solutions on 24th and 25th March, respectively. Please see &lt;A href="https://communities.sas.com/t5/Base-SAS-Programming/Combining-rows-of-data/td-p/258604" target="_blank"&gt;this thread&lt;/A&gt; and don't hesitate if you have questions (regarding my solution, I haven't analyzed Ksharp's yet).&lt;/P&gt;</description>
    <pubDate>Tue, 29 Mar 2016 13:52:48 GMT</pubDate>
    <dc:creator>FreelanceReinh</dc:creator>
    <dc:date>2016-03-29T13:52:48Z</dc:date>
    <item>
      <title>Recursive lookup for ID's</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Recursive-lookup-for-ID-s/m-p/259635#M50238</link>
      <description>&lt;P&gt;I have a dataset like this:&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;ID1 ID2&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;A1 A2&lt;BR /&gt;A2 A3&lt;BR /&gt;A4 A5&lt;BR /&gt;A6 A7&lt;BR /&gt;A7 A8&lt;BR /&gt;A1 A9&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I want an output dataset like:&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;ID Clus&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;A1 1&lt;/P&gt;
&lt;P&gt;A2 1&lt;/P&gt;
&lt;P&gt;A3 1&lt;/P&gt;
&lt;P&gt;A9 1&lt;/P&gt;
&lt;P&gt;A4 2&lt;/P&gt;
&lt;P&gt;A5 2&lt;/P&gt;
&lt;P&gt;A6 3&lt;/P&gt;
&lt;P&gt;A7 3&lt;/P&gt;
&lt;P&gt;A8 3&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Basically I want to cluster all the mapped IDs into one cluster. I tried unsuccessfully with self join.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Any ideas?&lt;/P&gt;</description>
      <pubDate>Tue, 29 Mar 2016 15:50:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Recursive-lookup-for-ID-s/m-p/259635#M50238</guid>
      <dc:creator>munitech4u</dc:creator>
      <dc:date>2016-03-29T15:50:40Z</dc:date>
    </item>
    <item>
      <title>Re: How to perform this in sas?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Recursive-lookup-for-ID-s/m-p/259660#M50249</link>
      <description>&lt;P&gt;Sorry, I am not clear on your logic here at all. &amp;nbsp;Your Have dataset doesn't seem to reflect your out dataset in any way. &amp;nbsp;Why is there only one A1 in your output dataset? &amp;nbsp;Why is A9 after A3. &amp;nbsp;What is Clus, and how is it derived? &amp;nbsp;To get a unique list:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data want;  
  set have (keep=id1 rename=(id1=id))
       have (keep=id2 rename=(id2=id));
run;
proc sort data=want nodupkey;
  by id;
run;&lt;/PRE&gt;
&lt;P&gt;Note, I haven't tested the above - post test data in the forma of a datastep so I don't have to type it in to get tested code. &amp;nbsp;The above will give you a distinct list of id's.&lt;/P&gt;</description>
      <pubDate>Tue, 29 Mar 2016 13:38:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Recursive-lookup-for-ID-s/m-p/259660#M50249</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2016-03-29T13:38:12Z</dc:date>
    </item>
    <item>
      <title>Re: How to perform this in sas?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Recursive-lookup-for-ID-s/m-p/259661#M50250</link>
      <description>&lt;P&gt;This is a chained or recursive lookup. Do you have SAS&amp;nbsp;OR licensed? If so, PROC BOM can be useful.&lt;/P&gt;</description>
      <pubDate>Tue, 29 Mar 2016 14:44:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Recursive-lookup-for-ID-s/m-p/259661#M50250</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2016-03-29T14:44:33Z</dc:date>
    </item>
    <item>
      <title>Re: How to perform this in sas?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Recursive-lookup-for-ID-s/m-p/259669#M50254</link>
      <description>&lt;P&gt;Hello&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/41912"&gt;@munitech4u﻿&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;That's interesting! Last week&amp;nbsp;the same sort of question was asked by someone else. Ksharp and I have provided two different solutions on 24th and 25th March, respectively. Please see &lt;A href="https://communities.sas.com/t5/Base-SAS-Programming/Combining-rows-of-data/td-p/258604" target="_blank"&gt;this thread&lt;/A&gt; and don't hesitate if you have questions (regarding my solution, I haven't analyzed Ksharp's yet).&lt;/P&gt;</description>
      <pubDate>Tue, 29 Mar 2016 13:52:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Recursive-lookup-for-ID-s/m-p/259669#M50254</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2016-03-29T13:52:48Z</dc:date>
    </item>
    <item>
      <title>Re: How to perform this in sas?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Recursive-lookup-for-ID-s/m-p/259707#M50268</link>
      <description>Mine, is even more complex, as the relationship is not just one way, it can be two way. So like in the example give in other solution, the player id can occur in assignment id and vice versa.</description>
      <pubDate>Tue, 29 Mar 2016 15:04:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Recursive-lookup-for-ID-s/m-p/259707#M50268</guid>
      <dc:creator>munitech4u</dc:creator>
      <dc:date>2016-03-29T15:04:17Z</dc:date>
    </item>
    <item>
      <title>Re: How to perform this in sas?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Recursive-lookup-for-ID-s/m-p/259708#M50269</link>
      <description>clus is nothing but a category we need to assing for related IDs</description>
      <pubDate>Tue, 29 Mar 2016 15:05:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Recursive-lookup-for-ID-s/m-p/259708#M50269</guid>
      <dc:creator>munitech4u</dc:creator>
      <dc:date>2016-03-29T15:05:17Z</dc:date>
    </item>
    <item>
      <title>Re: How to perform this in sas?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Recursive-lookup-for-ID-s/m-p/259715#M50273</link>
      <description>&lt;P&gt;This is your problem:&lt;/P&gt;
&lt;P&gt;&lt;A href="https://communities.sas.com/t5/Base-SAS-Programming/Find-last-child/m-p/113752/highlight/true#M23446" target="_blank"&gt;https://communities.sas.com/t5/Base-SAS-Programming/Find-last-child/m-p/113752/highlight/true#M23446&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 29 Mar 2016 15:13:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Recursive-lookup-for-ID-s/m-p/259715#M50273</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2016-03-29T15:13:53Z</dc:date>
    </item>
    <item>
      <title>Re: How to perform this in sas?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Recursive-lookup-for-ID-s/m-p/259718#M50275</link>
      <description>&lt;P&gt;I have an idea, but not sure how to execute this:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;1. Get a dataset with all distinct IDs.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;2. Now Pick the first ID, and look for all the possible&amp;nbsp;IDs in ID1 or ID2 that occurs with this ID, put them in cluster 1.&lt;/P&gt;
&lt;P&gt;3. Now&amp;nbsp;Get&amp;nbsp;all other distinct IDs that occur above and repeat the step above. Keep updating the cluster.&lt;/P&gt;
&lt;P&gt;4. Loop until we reach at a point, when we have iterated through all the IDs in cluster 1.&lt;/P&gt;
&lt;P&gt;5. Eliminate the cluster 1 IDs from the dataset with distinct IDs and the dataset we are looking into.&lt;/P&gt;
&lt;P&gt;6. Repeat step 2-5 until we are finished with all IDs.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Example:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Distinct IDs&lt;/P&gt;
&lt;P&gt;A1&lt;/P&gt;
&lt;P&gt;A2&lt;/P&gt;
&lt;P&gt;A3&lt;/P&gt;
&lt;P&gt;A4&lt;/P&gt;
&lt;P&gt;A5&lt;/P&gt;
&lt;P&gt;A6&lt;/P&gt;
&lt;P&gt;A7&lt;/P&gt;
&lt;P&gt;A8&lt;/P&gt;
&lt;P&gt;A9&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;First ID: A1&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;All possible combination from dataset:&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;A1 A2&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;A1 A9&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Cluster 1: A1,A2,A9&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Loop Through cluster 1 except A1: For A2, possible combination:&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;A2 A3&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Update cluster 1: A1,A2,A3,A9&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Loop Through cluster 1 except A1,A2: For A9, possible combination:&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;A9 A1&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Update cluster 1: A1,A2,A3,A9&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Eliminate A1,A2,A3,A9 from distinct IDs and observations wherever they occur in dataset&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Repeat above steps, keep incrementing the cluster.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 29 Mar 2016 15:30:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Recursive-lookup-for-ID-s/m-p/259718#M50275</guid>
      <dc:creator>munitech4u</dc:creator>
      <dc:date>2016-03-29T15:30:10Z</dc:date>
    </item>
    <item>
      <title>Re: How to perform this in sas?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Recursive-lookup-for-ID-s/m-p/259719#M50276</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/41912"&gt;@munitech4u&lt;/a&gt; wrote:&lt;BR /&gt;Mine, is even more complex, as the relationship is not just one way, it can be two way. So like in the example give in other solution, the player id can occur in assignment id and vice versa.&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;I don't think that this makes it significantly more complex. In order to use the solutions in the other recent thread as much as possible, one could regard your ID1 and ID2 as completely separate sets of IDs (like the assignment and player IDs in the other thread), then assign the cluster numbers (these correspond&amp;nbsp;to&amp;nbsp;the group numbers g in my solution in the other thread) and finally merge the ID1 and ID2 values within each cluster. But intuitively I guess that the solution can be even simpler, given that there is only one type of ID.&lt;/P&gt;</description>
      <pubDate>Tue, 29 Mar 2016 15:18:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Recursive-lookup-for-ID-s/m-p/259719#M50276</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2016-03-29T15:18:05Z</dc:date>
    </item>
    <item>
      <title>Re: How to perform this in sas?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Recursive-lookup-for-ID-s/m-p/259723#M50277</link>
      <description>&lt;P&gt;Yea, Seem like I need to go through it. The solution is based on the lag value which, is not the case with me. It does not necessarily have a relationship with lag.&lt;/P&gt;</description>
      <pubDate>Tue, 29 Mar 2016 15:31:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Recursive-lookup-for-ID-s/m-p/259723#M50277</guid>
      <dc:creator>munitech4u</dc:creator>
      <dc:date>2016-03-29T15:31:51Z</dc:date>
    </item>
    <item>
      <title>Re: How to perform this in sas?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Recursive-lookup-for-ID-s/m-p/259728#M50279</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/41912"&gt;@munitech4u﻿&lt;/a&gt;:&amp;nbsp;I'm already in the process of adapting my macro ITER to your needs. (It was really interesting to develop it.)&lt;/P&gt;</description>
      <pubDate>Tue, 29 Mar 2016 15:31:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Recursive-lookup-for-ID-s/m-p/259728#M50279</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2016-03-29T15:31:23Z</dc:date>
    </item>
    <item>
      <title>Re: How to perform this in sas?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Recursive-lookup-for-ID-s/m-p/259731#M50281</link>
      <description>Cool, I am looking forward to it too. I have posted a logic solution, but not sure, as of now, how to implement it!</description>
      <pubDate>Tue, 29 Mar 2016 15:33:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Recursive-lookup-for-ID-s/m-p/259731#M50281</guid>
      <dc:creator>munitech4u</dc:creator>
      <dc:date>2016-03-29T15:33:42Z</dc:date>
    </item>
    <item>
      <title>Re: How to perform this in sas?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Recursive-lookup-for-ID-s/m-p/259738#M50285</link>
      <description>&lt;P&gt;&lt;A href="https://communities.sas.com/t5/Base-SAS-Programming/How-to-create-a-group-ID-for-interlinked-ID-numbers/m-p/132931/highlight/true#M27056" target="_blank"&gt;https://communities.sas.com/t5/Base-SAS-Programming/How-to-create-a-group-ID-for-interlinked-ID-numbers/m-p/132931/highlight/true#M27056&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;A href="https://communities.sas.com/t5/tkb/articleprintpage/tkb-id/library/article-id/1045" target="_blank"&gt;https://communities.sas.com/t5/tkb/articleprintpage/tkb-id/library/article-id/1045&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;Use the Subgraphs macro&lt;/P&gt;</description>
      <pubDate>Tue, 29 Mar 2016 15:37:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Recursive-lookup-for-ID-s/m-p/259738#M50285</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2016-03-29T15:37:38Z</dc:date>
    </item>
    <item>
      <title>Re: How to perform this in sas?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Recursive-lookup-for-ID-s/m-p/259763#M50297</link>
      <description>&lt;P&gt;Here's an adapted version ITER4U of my previous macro ITER. I haven't tested it thoroughly yet, but at least it produces the correct result on your test data. Maybe it's unnecessarily complicated and it definitely could be polished here and there because it's an adaptation of code which was developed for a slightly different task.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input ID1 $ ID2 $;
cards;
A1 A2
A2 A3
A3 A1
A4 A5
A6 A7
A7 A8
A1 A9
;

/* Macro to perform the iterative algorithm */

%macro iter4u(dsin=have  /* input dataset                 */
           , dsout=want  /* output dataset                */
           , v1=id1      /* variable name for first IDs   */
           , v2=id2      /* variable name for second IDs  */
           );
%local i n1 n2;

data &amp;amp;dsin.2;
set &amp;amp;dsin
    &amp;amp;dsin(rename=(&amp;amp;v1=&amp;amp;v2 &amp;amp;v2=&amp;amp;v1));
run;

proc sort data=&amp;amp;dsin.2;
by &amp;amp;v1;
run;

%let i=1;

/* Assign initial group numbers g */

data t1;
set &amp;amp;dsin.2;
by &amp;amp;v1;
g+first.&amp;amp;v1;
run;

proc sql noprint;        /* The sum of all group numbers */
select sum(g) into :n1   /* is used to detect changes    */
from t1;                 /* after reassigning groups.    */
quit;

%if &amp;amp;n1&amp;gt;=%sysfunc(constant(EXACTINT))
%then %put %str(WAR)NING: Sum of group numbers has become too large. Results may be incorrect!;

%do %until(&amp;amp;n1=&amp;amp;n2);
  %let i=%eval(3-&amp;amp;i); /* toggle i between 1 and 2 */

  proc sql noprint;         /* If one ID2 has been assigned      */
  create table t&amp;amp;i as       /* multiple group numbers, they are  */
  select &amp;amp;v1, &amp;amp;v2, min(g) as g  /* replaced by their minimum.    */
  from t%eval(3-&amp;amp;i)         /* In the next iteration the same    */
  group by &amp;amp;&amp;amp;v&amp;amp;i;           /* procedure is applied to ID1       */
                            /* rather than ID2.                  */
  select sum(g) into :n&amp;amp;i   /* And so on, alternating, until     */
  from t&amp;amp;i;                 /* no reassignments occur, i.e., t1  */
  quit;                     /* and t2 are equal up to sort order */
%end;                       /* and hence &amp;amp;n1=&amp;amp;n2.                */

proc sql;                   /* Bring ID1 and ID2 together        */
create table uni as       
select g, &amp;amp;v1 as ID from t1
union
select g, &amp;amp;v2 as ID from t1
quit;

proc sql noprint;           /* Assign preliminary cluster numbers */
create table &amp;amp;dsout.0 as 
select ID, min(g) as g
from uni
group by ID;  
quit;

/* Create result dataset */

proc sort data=&amp;amp;dsout.0;
by g ID;
run;

data &amp;amp;dsout;
set &amp;amp;dsout.0;
by g;
Clus+first.g;              /* Renumber the clusters as 1, 2, 3, ... */
drop g;
run;
%mend iter4u;

%iter4u;

proc print data=want noobs;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 29 Mar 2016 16:19:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Recursive-lookup-for-ID-s/m-p/259763#M50297</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2016-03-29T16:19:01Z</dc:date>
    </item>
    <item>
      <title>Re: How to perform this in sas?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Recursive-lookup-for-ID-s/m-p/259786#M50304</link>
      <description>Can you please explain? How does this work?&lt;BR /&gt;&lt;BR /&gt;data t1;&lt;BR /&gt;set &amp;amp;dsin.2;&lt;BR /&gt;by &amp;amp;v1;&lt;BR /&gt;g+first.&amp;amp;v1;&lt;BR /&gt;run;&lt;BR /&gt;&lt;BR /&gt;And the purpose of it?</description>
      <pubDate>Tue, 29 Mar 2016 16:53:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Recursive-lookup-for-ID-s/m-p/259786#M50304</guid>
      <dc:creator>munitech4u</dc:creator>
      <dc:date>2016-03-29T16:53:43Z</dc:date>
    </item>
    <item>
      <title>Re: How to perform this in sas?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Recursive-lookup-for-ID-s/m-p/259806#M50313</link>
      <description>&lt;P&gt;This step is based on the intermediate dataset HAVE2 (&amp;amp;dsin.2) which contains all ID1-ID2 pairs and ID2-ID1 pairs (but with the variable names swapped) of the original input dataset HAVE (&amp;amp;dsin). I newly introduced this dataset into the macro to reflect the fact that in your application ID1 and ID2 are actually taken from a single set of IDs. This dataset was sorted by ID1 (&amp;amp;v1) in the previous step.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Now the first ID1 BY group is assigned g=1, the second is assigned g=2 and so on. (The sum statement g+... increments g by 1 whenever a new ID1 BY group starts, i.e., if FIRST.ID1=1.) These group numbers g can be regarded as preliminary cluster numbers. In the subsequent iterative steps these numbers are changed (replaced by a minimum group number) if it turns out that one ID has been assigned different group numbers (due to links to different other IDs). This process is iterated until no further renumberings occur.&lt;/P&gt;</description>
      <pubDate>Tue, 29 Mar 2016 17:23:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Recursive-lookup-for-ID-s/m-p/259806#M50313</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2016-03-29T17:23:58Z</dc:date>
    </item>
    <item>
      <title>Re: How to perform this in sas?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Recursive-lookup-for-ID-s/m-p/259812#M50317</link>
      <description>A1	A2	1&lt;BR /&gt;A1	A9	1&lt;BR /&gt;A2	A3	2&lt;BR /&gt;A2	A1	2&lt;BR /&gt;A3	A2	1&lt;BR /&gt;A4	A5	4&lt;BR /&gt;A5	A4	5&lt;BR /&gt;A6	A7	6&lt;BR /&gt;A7	A8	7&lt;BR /&gt;A7	A6	7&lt;BR /&gt;A8	A7	6&lt;BR /&gt;A9	A1	2&lt;BR /&gt;&lt;BR /&gt;But in the dataset g is like above. Why the 4th and 5th obs are 1 and 4?</description>
      <pubDate>Tue, 29 Mar 2016 17:36:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Recursive-lookup-for-ID-s/m-p/259812#M50317</guid>
      <dc:creator>munitech4u</dc:creator>
      <dc:date>2016-03-29T17:36:34Z</dc:date>
    </item>
    <item>
      <title>Re: How to perform this in sas?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Recursive-lookup-for-ID-s/m-p/259813#M50318</link>
      <description>&lt;P&gt;Here's the subgraph macro solution courtesy of&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/462"&gt;@PGStats﻿&lt;/a&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It uses hash tables, which I'm not even going to pretend to understand. The cluster ID's are not the same, but the clusters are.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The forum or Chrome is messing with the code though, you need to replace the &lt;U&gt;&lt;STRONG&gt;&lt;FONT color="#FF0000"&gt;ampersand&lt;/FONT&gt;colon&lt;FONT color="#FF0000"&gt;semicolon&lt;/FONT&gt;&lt;/STRONG&gt;&lt;/U&gt; with a single colon to make it work.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/*
The SubGraphs macro

This SAS macro finds the disjoint subgraphs in a graph described by a set of arcs.
The input dataset requires only two variables of the same length and type, character
or numeric. Each observation describes an arc in the graph. The output dataset will
contain two variables: Node will be similar in size and type to the input variables
and Clust will be a number identifying the clusters.

Macro arguments

arcs : input dataset name
from : first arc variable (optional, default=from)
to : second arc variable (optional, default=to)
out : output dataset name (optional, default=Clusters)
exp : power of two exponent of internal hash size (optional, default=8)

Tested with SAS version 9.3

PGStats

*/

/* Example : From a list of American city pairs separated by less than 800 miles,
we identify two regions where one could get from one city to the next without
ever travelling more than 800 miles. */

/*Example data view: convert city distance matrix to city pairs list.
Keep nearby cities (&amp;lt;800 miles apart) only. */
/*
data Nearby(keep=from to Miles) / view=Nearby;
set Sashelp.Mileages;
length from to $15;
array c{*} _NUMERIC_;
from = upcase(compress(City,". -"));
do i = 1 to dim(c);
	if c{i} &amp;gt; 0 and c{i} &amp;lt; 800 then do;
		to = upcase(vname(c{i}));
		Miles = c{i};
		output;
	end;
end;
run;
*/
	
/* Macro definition */
%macro SubGraphs(arcs,from=from,to=to,out=Clusters,exp=8);
data _null_;
if 0 then set &amp;amp;arcs(keep=&amp;amp;from rename=(&amp;amp;from=node)); /* get node data type */
length clust 8;
declare hash nodes(hashexp:&amp;amp;exp);
nodes.defineKey('node');
nodes.defineData('node', 'clust');
nodes.defineDone();
declare hiter nodeList('nodes');

do newClust = 1 by 1 while(not endLoop);
	set &amp;amp;arcs end=endLoop;
	call missing(clust); node = &amp;amp;from;
	if 0^=nodes.find() then nodes.add(); 
	fromClust = clust; 
	call missing(clust); node = &amp;amp;to;
	if 0^=nodes.find() then nodes.add(); 
	toClust = clust;
	if n(fromClust, toClust) = 0 then do;
		nodes.replace(key:&amp;amp;from, data&amp;amp;colon;&amp;amp;from, data&amp;amp;colon;newClust);
		nodes.replace(key:&amp;amp;to, data&amp;amp;colon;&amp;amp;to, data&amp;amp;colon;newClust);
	end;
	else if missing(toClust) then 
		nodes.replace(key:&amp;amp;to, data&amp;amp;colon;&amp;amp;to, data&amp;amp;colon;fromClust);
	else if missing(fromClust) then 
		nodes.replace(key:&amp;amp;from, data&amp;amp;colon;&amp;amp;from, data&amp;amp;colon;toClust);
	else if fromClust ne toClust then do;
		rc = nodeList.first();
		do while (rc = 0);
			if clust = fromClust then 
				nodes.replace(key:node, data&amp;amp;colon;node, data&amp;amp;colon;toClust);
			rc = nodeList.next();
		end;
	end;
end;
nodes.output(dataset:"&amp;amp;out");
stop;
run;
%mend SubGraphs;

/* Example cont'd. Call the macro with the city pairs list dataview 
and print the results. */
/*
options mprint symbolgen;
%SubGraphs(Nearby,out=Regions,exp=4);

proc sql;
select clust as Region, node as City 
from Regions order by clust, node;
quit; 
*/



data have;

input ID1 $ ID2 $;
cards;
A1 A2
A2 A3
A4 A5
A6 A7
A7 A8
A1 A9
;
run;

%SubGraphs(have,from=id1,to=id2,out=Clusters,exp=8);

proc sort data=clusters;
by clust node;
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 29 Mar 2016 17:49:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Recursive-lookup-for-ID-s/m-p/259813#M50318</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2016-03-29T17:49:21Z</dc:date>
    </item>
    <item>
      <title>Re: How to perform this in sas?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Recursive-lookup-for-ID-s/m-p/259814#M50319</link>
      <description>&lt;P&gt;I reckon your output is based on a different version of T1, because when I run the following steps, I obtain the result shown further below.&amp;nbsp;Please note that datasets T1 and T2 are overwritten again and again in the iteration (%DO %UNTIL loop).&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input ID1 $ ID2 $;
cards;
A1 A2
A2 A3
A3 A1
A4 A5
A6 A7
A7 A8
A1 A9
;

data have2;
set have
    have(rename=(ID1=ID2 ID2=ID1));
run;

proc sort data=have2;
by ID1;
run;

data t1;
set have2;
by ID1;
g+first.ID1;
run;

proc print data=t1 noobs;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result:&lt;/P&gt;
&lt;PRE&gt;ID1    ID2    g

A1     A2     1
A1     A9     1
A1     A3     1
A2     A3     2
A2     A1     2
A3     A1     3
A3     A2     3
A4     A5     4
A5     A4     5
A6     A7     6
A7     A8     7
A7     A6     7
A8     A7     8
A9     A1     9&lt;/PRE&gt;</description>
      <pubDate>Tue, 29 Mar 2016 17:46:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Recursive-lookup-for-ID-s/m-p/259814#M50319</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2016-03-29T17:46:14Z</dc:date>
    </item>
    <item>
      <title>Re: How to perform this in sas?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Recursive-lookup-for-ID-s/m-p/259816#M50320</link>
      <description>&lt;P&gt;I posted the macro here as well if you wanted to try it:&lt;/P&gt;
&lt;P&gt;&lt;A href="https://gist.github.com/statgeek/14e3aa2a9f718f551cd98134e9ceed30" target="_blank"&gt;https://gist.github.com/statgeek/14e3aa2a9f718f551cd98134e9ceed30&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 29 Mar 2016 17:50:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Recursive-lookup-for-ID-s/m-p/259816#M50320</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2016-03-29T17:50:32Z</dc:date>
    </item>
  </channel>
</rss>

