<?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 the master key from another column using SAS in SAS Enterprise Guide</title>
    <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-find-the-master-key-from-another-column-using-SAS/m-p/860685#M42378</link>
    <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
infile datalines dlm="09"x dsd;
input id1 id2;
datalines;
11	11
11	12
11	13
12	15
12	16
15	17
18	18
18	19
20	21
22	23
22	24
;

data want;
set have;
if _n_ = 1
then do;
  length key 8;
  declare hash keys();
  keys.definekey("id1");
  keys.definedata("key");
  keys.definedone();
end;
if keys.find() = 0
then do;
  output;
  id1 = id2;
  rc = keys.add();
end;
else do;
  key = id1;
  output;
  rc = keys.add();
end;
drop rc;
run;

proc print data=want noobs;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result:&lt;/P&gt;
&lt;PRE&gt;id1	id2	key
11	11	11
11	12	11
11	13	11
12	15	11
12	16	11
15	17	11
18	18	18
18	19	18
20	21	20
22	23	22
22	24	22
&lt;/PRE&gt;</description>
    <pubDate>Fri, 24 Feb 2023 14:49:52 GMT</pubDate>
    <dc:creator>Kurt_Bremser</dc:creator>
    <dc:date>2023-02-24T14:49:52Z</dc:date>
    <item>
      <title>How to find the master key from another column using SAS</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-find-the-master-key-from-another-column-using-SAS/m-p/860638#M42369</link>
      <description>&lt;P&gt;I have a table where I have to find the values from each other columns and create a master key&lt;/P&gt;&lt;P&gt;My table :&lt;/P&gt;&lt;TABLE border="0" cellspacing="0" cellpadding="0"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;ID1&lt;/TD&gt;&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Key column needs to be generated as shown in column, how do I achieve using SAS EG.&lt;/P&gt;</description>
      <pubDate>Fri, 24 Feb 2023 11:42:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-find-the-master-key-from-another-column-using-SAS/m-p/860638#M42369</guid>
      <dc:creator>Kailash29</dc:creator>
      <dc:date>2023-02-24T11:42:03Z</dc:date>
    </item>
    <item>
      <title>Re: How to find the master key from another column using SAS</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-find-the-master-key-from-another-column-using-SAS/m-p/860645#M42370</link>
      <description>&lt;P&gt;Sorry, I don't understand the rule how to set "Key".&lt;/P&gt;
&lt;P&gt;Can you please elaborate?&lt;/P&gt;</description>
      <pubDate>Fri, 24 Feb 2023 12:16:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-find-the-master-key-from-another-column-using-SAS/m-p/860645#M42370</guid>
      <dc:creator>LinusH</dc:creator>
      <dc:date>2023-02-24T12:16:45Z</dc:date>
    </item>
    <item>
      <title>Re: How to find the master key from another column using SAS</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-find-the-master-key-from-another-column-using-SAS/m-p/860646#M42371</link>
      <description>ID1 is the master key and values associated with it in ID2 will be linked values.&lt;BR /&gt;&lt;BR /&gt;ex : ID1 = 15 and its key is 11 because&lt;BR /&gt;&lt;BR /&gt;15 in ID1 traces back to 12 in ID1 through 15 in ID2 and 12 in ID1 traces back to 11 in ID1&lt;BR /&gt;&lt;BR /&gt;Hence master key for 15 in ID1 will be 11</description>
      <pubDate>Fri, 24 Feb 2023 12:26:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-find-the-master-key-from-another-column-using-SAS/m-p/860646#M42371</guid>
      <dc:creator>Kailash29</dc:creator>
      <dc:date>2023-02-24T12:26:42Z</dc:date>
    </item>
    <item>
      <title>Re: How to find the master key from another column using SAS</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-find-the-master-key-from-another-column-using-SAS/m-p/860650#M42372</link>
      <description>&lt;P&gt;What if you have the following data. What you are going to do ?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;D1	ID2
11	11
11	12
11	13
12	15
12	16
&lt;FONT color="#FF0000"&gt;&lt;STRONG&gt;16     16
16      11&lt;/STRONG&gt;&lt;/FONT&gt;
15	17
18	18
18	19
20	21
22	23
22	24&lt;/PRE&gt;</description>
      <pubDate>Fri, 24 Feb 2023 12:42:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-find-the-master-key-from-another-column-using-SAS/m-p/860650#M42372</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2023-02-24T12:42:20Z</dc:date>
    </item>
    <item>
      <title>Re: How to find the master key from another column using SAS</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-find-the-master-key-from-another-column-using-SAS/m-p/860662#M42376</link>
      <description>16 16 then key will be 11&lt;BR /&gt;16 11 scenario will not occur because ID1 value cannot be linked to its lower value in ID2</description>
      <pubDate>Fri, 24 Feb 2023 13:28:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-find-the-master-key-from-another-column-using-SAS/m-p/860662#M42376</guid>
      <dc:creator>Kailash29</dc:creator>
      <dc:date>2023-02-24T13:28:21Z</dc:date>
    </item>
    <item>
      <title>Re: How to find the master key from another column using SAS</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-find-the-master-key-from-another-column-using-SAS/m-p/860685#M42378</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
infile datalines dlm="09"x dsd;
input id1 id2;
datalines;
11	11
11	12
11	13
12	15
12	16
15	17
18	18
18	19
20	21
22	23
22	24
;

data want;
set have;
if _n_ = 1
then do;
  length key 8;
  declare hash keys();
  keys.definekey("id1");
  keys.definedata("key");
  keys.definedone();
end;
if keys.find() = 0
then do;
  output;
  id1 = id2;
  rc = keys.add();
end;
else do;
  key = id1;
  output;
  rc = keys.add();
end;
drop rc;
run;

proc print data=want noobs;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result:&lt;/P&gt;
&lt;PRE&gt;id1	id2	key
11	11	11
11	12	11
11	13	11
12	15	11
12	16	11
15	17	11
18	18	18
18	19	18
20	21	20
22	23	22
22	24	22
&lt;/PRE&gt;</description>
      <pubDate>Fri, 24 Feb 2023 14:49:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-find-the-master-key-from-another-column-using-SAS/m-p/860685#M42378</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2023-02-24T14:49:52Z</dc:date>
    </item>
    <item>
      <title>Re: How to find the master key from another column using SAS</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-find-the-master-key-from-another-column-using-SAS/m-p/860793#M42379</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/439895"&gt;@Kailash29&lt;/a&gt;&amp;nbsp;I believe you've now already received code that resolved your problem so just fyi &lt;A href="https://communities.sas.com/t5/SAS-Programming/Find-last-child/m-p/113752" target="_self"&gt;here&lt;/A&gt; an old discussion with several solutions to a related problem.&lt;/P&gt;</description>
      <pubDate>Sat, 25 Feb 2023 02:41:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-find-the-master-key-from-another-column-using-SAS/m-p/860793#M42379</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2023-02-25T02:41:28Z</dc:date>
    </item>
    <item>
      <title>Re: How to find the master key from another column using SAS</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-find-the-master-key-from-another-column-using-SAS/m-p/860833#M42380</link>
      <description>&lt;P&gt;OK. Try this one :&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
infile cards expandtabs truncover;
/*from is parent, to is child*/
input from $  to $ ;
cards;
11	11
11	12
11	13
12	15
12	16
15	17
18	18
18	19
20	21
22	23
22	24
;
run;
data full;
  set have end=last;
  if _n_ eq 1 then do;
   declare hash h();
    h.definekey('node');
     h.definedata('node');
     h.definedone();
  end;
  output;
  node=from; h.replace();
  from=to; to=node; output;
  node=from; h.replace();
  if last then h.output(dataset:'node');
  drop node;
run;


data want(keep=node household);
declare hash ha(ordered:'a');
declare hiter hi('ha');
ha.definekey('count');
ha.definedata('last');
ha.definedone();
declare hash _ha(hashexp: 16);
_ha.definekey('key');
_ha.definedone();

if 0 then set full;
declare hash from_to(dataset:'full',hashexp:20,multidata:'y');
 from_to.definekey('from');
 from_to.definedata('to');
 from_to.definedone();

if 0 then set node;
declare hash no(dataset:'node');
declare hiter hi_no('no');
 no.definekey('node');
 no.definedata('node');
 no.definedone();
 

do while(hi_no.next()=0);
 household+1; output;
 count=1;
 key=node;_ha.add();
 last=node;ha.add();
 rc=hi.first();
 do while(rc=0);
   from=last;rx=from_to.find();
   do while(rx=0);
     key=to;ry=_ha.check();
      if ry ne 0 then do;
       node=to;output;rr=no.remove(key:node);
       key=to;_ha.add();
       count+1;
       last=to;ha.add();
      end;
      rx=from_to.find_next();
   end;
   rc=hi.next();
end;
ha.clear();_ha.clear();
end;
stop;
run;



proc sql;
create table temp as
select distinct * from have where from ne to;

create table ancestor as
select distinct from
 from temp
  where from not in (select to from temp);

create table ancestor2 as
select * from want where node in (select from from ancestor);

create table ancestor3 as
select a.*,b.node as key 
 from want as a left join ancestor2 as b
  on a.household=b.household;

create table final_want as
select a.*,b.key 
 from have as a left join ancestor3 as b
  on a.from=b.node;
quit;&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-1677324489805.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/80841i1B774B6E9DEB7E05/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Ksharp_0-1677324489805.png" alt="Ksharp_0-1677324489805.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 25 Feb 2023 11:28:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-find-the-master-key-from-another-column-using-SAS/m-p/860833#M42380</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2023-02-25T11:28:27Z</dc:date>
    </item>
    <item>
      <title>Re: How to find the master key from another column using SAS</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-find-the-master-key-from-another-column-using-SAS/m-p/860849#M42381</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/439895"&gt;@Kailash29&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;16 16 then key will be 11&lt;BR /&gt;16 11 scenario will not occur because ID1 value cannot be linked to its lower value in ID2&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;So if the data is sorted by ID1 and the values are integers you could do it with a simple temporary array.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input ID1	ID2;
cards;
11	11
11	12
11	13
12	15
12	16
15	17
18	18
18	19
20	21
22	23
22	24
;

data want;
  array keys [11:24] _temporary_;
  set have ;
  by id1 ;
  key = keys[id1];
  if missing(key) then key=id1;
  keys[id2]=key;
run;

proc print;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;To generalize either make the array larger than the largest possible value:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;  array keys [1000000] _temporary_;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Or to mimic the way I did it in the example pre-calculate the min and max actual values.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql noprint;
select min(min(id1,id2)),max(max(id2,id2))
  into :lbound,:ubound
from have
;
quit;
....
array keys [&amp;amp;lbound,&amp;amp;ubound] _temporary_;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 25 Feb 2023 15:08:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-find-the-master-key-from-another-column-using-SAS/m-p/860849#M42381</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-02-25T15:08:25Z</dc:date>
    </item>
    <item>
      <title>Re: How to find the master key from another column using SAS</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-find-the-master-key-from-another-column-using-SAS/m-p/861043#M42385</link>
      <description>Hi Ksharp.. Thanks for your quick response..&lt;BR /&gt;&lt;BR /&gt;When I saw deep into the data, I did find the scenario which you mention:&lt;BR /&gt;&lt;BR /&gt;if 16 16 occurs I need the key as 11&lt;BR /&gt;and 16 11 occurs as well the key needs to be 11</description>
      <pubDate>Mon, 27 Feb 2023 10:38:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-find-the-master-key-from-another-column-using-SAS/m-p/861043#M42385</guid>
      <dc:creator>Kailash29</dc:creator>
      <dc:date>2023-02-27T10:38:54Z</dc:date>
    </item>
    <item>
      <title>Re: How to find the master key from another column using SAS</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-find-the-master-key-from-another-column-using-SAS/m-p/861063#M42386</link>
      <description>Why not it is 16 ?</description>
      <pubDate>Mon, 27 Feb 2023 11:58:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-find-the-master-key-from-another-column-using-SAS/m-p/861063#M42386</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2023-02-27T11:58:25Z</dc:date>
    </item>
    <item>
      <title>Re: How to find the master key from another column using SAS</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-find-the-master-key-from-another-column-using-SAS/m-p/861093#M42387</link>
      <description>Because 16 is already connect to 12 and 12 back tracts towards 11</description>
      <pubDate>Mon, 27 Feb 2023 13:33:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-find-the-master-key-from-another-column-using-SAS/m-p/861093#M42387</guid>
      <dc:creator>Kailash29</dc:creator>
      <dc:date>2023-02-27T13:33:12Z</dc:date>
    </item>
    <item>
      <title>Re: How to find the master key from another column using SAS</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-find-the-master-key-from-another-column-using-SAS/m-p/861335#M42388</link>
      <description>Then what you gonna to do ? &lt;BR /&gt;If you have the follwing data ?&lt;BR /&gt;&lt;BR /&gt;D1 ID2&lt;BR /&gt;11 11&lt;BR /&gt;11 12&lt;BR /&gt;11 13&lt;BR /&gt;12 15&lt;BR /&gt;12 16&lt;BR /&gt;12  11   &amp;lt;-----&lt;BR /&gt;16  16&lt;BR /&gt;16  11</description>
      <pubDate>Tue, 28 Feb 2023 11:34:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-find-the-master-key-from-another-column-using-SAS/m-p/861335#M42388</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2023-02-28T11:34:06Z</dc:date>
    </item>
    <item>
      <title>Re: How to find the master key from another column using SAS</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-find-the-master-key-from-another-column-using-SAS/m-p/861666#M42396</link>
      <description>If 12 11 occurs then it would be 11 again because 12 is already connected to 11 earlier</description>
      <pubDate>Wed, 01 Mar 2023 07:22:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-find-the-master-key-from-another-column-using-SAS/m-p/861666#M42396</guid>
      <dc:creator>Kailash29</dc:creator>
      <dc:date>2023-03-01T07:22:45Z</dc:date>
    </item>
    <item>
      <title>Re: How to find the master key from another column using SAS</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-find-the-master-key-from-another-column-using-SAS/m-p/861731#M42399</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/*
So you want the first value for ancestor ?
*/
data have;
infile cards expandtabs truncover;
input from $  to $ ;
cards;
11 11
11 12
11 13
12 15
12 16
16  11
15 17
18 18
18 19
20 21
22 23
22 24
;
run;
data full;
  set have end=last;
  if _n_ eq 1 then do;
   declare hash h();
    h.definekey('node');
     h.definedata('node');
     h.definedone();
  end;
  output;
  node=from; h.replace();
  from=to; to=node; output;
  node=from; h.replace();
  if last then h.output(dataset:'node');
  drop node;
run;


data want(keep=node household);
declare hash ha(ordered:'a');
declare hiter hi('ha');
ha.definekey('count');
ha.definedata('last');
ha.definedone();
declare hash _ha(hashexp: 16);
_ha.definekey('key');
_ha.definedone();

if 0 then set full;
declare hash from_to(dataset:'full',hashexp:20,multidata:'y');
 from_to.definekey('from');
 from_to.definedata('to');
 from_to.definedone();

if 0 then set node;
declare hash no(dataset:'node');
declare hiter hi_no('no');
 no.definekey('node');
 no.definedata('node');
 no.definedone();
 

do while(hi_no.next()=0);
 household+1; output;
 count=1;
 key=node;_ha.add();
 last=node;ha.add();
 rc=hi.first();
 do while(rc=0);
   from=last;rx=from_to.find();
   do while(rx=0);
     key=to;ry=_ha.check();
      if ry ne 0 then do;
       node=to;output;rr=no.remove(key:node);
       key=to;_ha.add();
       count+1;
       last=to;ha.add();
      end;
      rx=from_to.find_next();
   end;
   rc=hi.next();
end;
ha.clear();_ha.clear();
end;
stop;
run;

data final_want;
 if _n_=1 then do;
   if 0 then set want;
   declare hash h(dataset:'want');
   h.definekey('node');
   h.definedata('household');
   h.definedone();
 end;
set have;
call missing(household);
rc=h.find(key:from);
drop rc node;
run;
proc sort data=final_want;by household;run;
data final_want;
 set final_want;
 by household;
 retain key;
 if first.household then key=from;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 01 Mar 2023 13:52:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-find-the-master-key-from-another-column-using-SAS/m-p/861731#M42399</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2023-03-01T13:52:02Z</dc:date>
    </item>
  </channel>
</rss>

