<?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: Column search to produce final column in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/Column-search-to-produce-final-column/m-p/473666#M71056</link>
    <description>&lt;P&gt;&amp;nbsp;It's not the prettiest, but you may try the following:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;PROC SQL;
    CREATE TABLE table3 AS
    SELECT t1.FINALKEY3 AS finalkey 
    FROM table1 AS t1
    LEFT JOIN table2 AS t2 WHERE (t2.KEY = t1.OLDKEY1 OR t2.KEY = t1.NEWKEY2);
QUIT;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 27 Jun 2018 11:46:45 GMT</pubDate>
    <dc:creator>lorenzo_hg</dc:creator>
    <dc:date>2018-06-27T11:46:45Z</dc:date>
    <item>
      <title>Column search to produce final column</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Column-search-to-produce-final-column/m-p/473573#M71049</link>
      <description>&lt;P&gt;Hi.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Looking for some assistance.&amp;nbsp; I have two tables that I need combine to create a third.&amp;nbsp; &amp;nbsp;In one table I have three column of keys- (key1-key3).&amp;nbsp; This table is automatically updated to consolidate and match using some fuzzy match resulting in a person's key&amp;nbsp;being changed throughout the year from [oldkey1] to [newkey2] to a final key named [finalkey3].&amp;nbsp; &amp;nbsp;This can happen when an individual&amp;nbsp;visits different locations and are&amp;nbsp;assigned different keys.&amp;nbsp;&amp;nbsp; (see row 3 and row 4 #10082782) which resulted in a final key determination of 36678 for this one individual.)&amp;nbsp; &amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have another table that I need to match against this three column key table.&amp;nbsp; This other table contains a [key] which can be matched against the first table to arrive at a finalkey which will be the official assignment for each person.&amp;nbsp; In this second table I have isolated just one person, but this person's data was entered under two different keys 10082792 &amp;amp; 71595)- but is ultimately person id#36678.&amp;nbsp; Does anyone know how I can hit this second table against the first table to run and check each column (oldkey1, newkey2) to arrive at a final key?&amp;nbsp; &amp;nbsp;Any help would be appreciated.&amp;nbsp; Thank you in advance&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;TABLE width="248"&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD width="80"&gt;table 1&lt;/TD&gt;
&lt;TD width="87"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD width="81"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;OLDKEY1&lt;/TD&gt;
&lt;TD&gt;NEWKEY2&lt;/TD&gt;
&lt;TD&gt;FINALKEY3&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;8172456&lt;/TD&gt;
&lt;TD&gt;5230020&lt;/TD&gt;
&lt;TD&gt;5230020&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;43015&lt;/TD&gt;
&lt;TD&gt;8016157&lt;/TD&gt;
&lt;TD&gt;8016157&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;&lt;STRONG&gt;2208&lt;/STRONG&gt;&lt;/TD&gt;
&lt;TD&gt;&lt;STRONG&gt;10082792&lt;/STRONG&gt;&lt;/TD&gt;
&lt;TD&gt;&lt;STRONG&gt;36678&lt;/STRONG&gt;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;&lt;STRONG&gt;71595&lt;/STRONG&gt;&lt;/TD&gt;
&lt;TD&gt;&lt;STRONG&gt;10082792&lt;/STRONG&gt;&lt;/TD&gt;
&lt;TD&gt;&lt;STRONG&gt;36678&lt;/STRONG&gt;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;85165&lt;/TD&gt;
&lt;TD&gt;6572623&lt;/TD&gt;
&lt;TD&gt;6572623&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;98048&lt;/TD&gt;
&lt;TD&gt;7377116&lt;/TD&gt;
&lt;TD&gt;7377116&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;102813&lt;/TD&gt;
&lt;TD&gt;1405523&lt;/TD&gt;
&lt;TD&gt;1405523&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;115732&lt;/TD&gt;
&lt;TD&gt;2129398&lt;/TD&gt;
&lt;TD&gt;2129398&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;TABLE width="248"&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD width="80"&gt;table 2&lt;/TD&gt;
&lt;TD width="87"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD width="81"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;KEY&lt;/TD&gt;
&lt;TD&gt;PID&lt;/TD&gt;
&lt;TD&gt;PID2&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;10082792&lt;/TD&gt;
&lt;TD&gt;2&lt;/TD&gt;
&lt;TD&gt;8&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;71595&lt;/TD&gt;
&lt;TD&gt;9&lt;/TD&gt;
&lt;TD&gt;0&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;TABLE width="301"&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD width="80"&gt;results table&lt;/TD&gt;
&lt;TD width="87"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD width="81"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD width="53"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;KEY&lt;/TD&gt;
&lt;TD&gt;PID&lt;/TD&gt;
&lt;TD&gt;PID2&lt;/TD&gt;
&lt;TD&gt;finalkey&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;10082792&lt;/TD&gt;
&lt;TD&gt;2&lt;/TD&gt;
&lt;TD&gt;8&lt;/TD&gt;
&lt;TD&gt;&lt;STRONG&gt;36678&lt;/STRONG&gt;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;71595&lt;/TD&gt;
&lt;TD&gt;9&lt;/TD&gt;
&lt;TD&gt;0&lt;/TD&gt;
&lt;TD&gt;&lt;STRONG&gt;36678&lt;/STRONG&gt;&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 27 Jun 2018 00:04:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Column-search-to-produce-final-column/m-p/473573#M71049</guid>
      <dc:creator>BaileyY</dc:creator>
      <dc:date>2018-06-27T00:04:21Z</dc:date>
    </item>
    <item>
      <title>Re: Column search to produce final column</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Column-search-to-produce-final-column/m-p/473666#M71056</link>
      <description>&lt;P&gt;&amp;nbsp;It's not the prettiest, but you may try the following:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;PROC SQL;
    CREATE TABLE table3 AS
    SELECT t1.FINALKEY3 AS finalkey 
    FROM table1 AS t1
    LEFT JOIN table2 AS t2 WHERE (t2.KEY = t1.OLDKEY1 OR t2.KEY = t1.NEWKEY2);
QUIT;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 27 Jun 2018 11:46:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Column-search-to-produce-final-column/m-p/473666#M71056</guid>
      <dc:creator>lorenzo_hg</dc:creator>
      <dc:date>2018-06-27T11:46:45Z</dc:date>
    </item>
    <item>
      <title>Re: Column search to produce final column</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Column-search-to-produce-final-column/m-p/473788#M71060</link>
      <description>&lt;P&gt;Matching&amp;nbsp;one var in a table against multiple vars in another table is a good situation for applying the WHICHN function (or WHICHC for character variables):&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data table1     ;
  input OLDKEY1 NEWKEY2 FINALKEY3 ;
datalines;
8172456 5230020 5230020 
43015 8016157 8016157 
2208 10082792 36678 
71595 10082792 36678 
85165 6572623 6572623 
98048 7377116 7377116 
102813 1405523 1405523 
115732 2129398 2129398 
run;

data table2;     
  input KEY PID PID2 ;
datalines;
10082792 2 8 
71595 9 0 
run;

proc sql;
  create table want as 
    select distinct t2.*,t1.finalkey3
    from 
      table2 as t2
    left join
      table1 as t1
    on whichn(t2.key,t1.finalkey3,t1.oldkey1,t1.newkey2);
quit;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The select expression needs the DISTINCT to avoid duplicate results.&amp;nbsp; Otherwise the first record in table 2 matches 2 instances in table 1, both having the same FINALKEY3.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The WHICHN function compares the first argument to the list of subsequent arguments and returns the position of the first match.&amp;nbsp; If there are no matches, it returns 0.&lt;/P&gt;</description>
      <pubDate>Wed, 27 Jun 2018 15:24:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Column-search-to-produce-final-column/m-p/473788#M71060</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2018-06-27T15:24:50Z</dc:date>
    </item>
    <item>
      <title>Re: Column search to produce final column</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Column-search-to-produce-final-column/m-p/473813#M71061</link>
      <description>Hi.  Thank you so much for taking time to offer me help.  I read up on this function and it sounds like it should do what I am expecting to be done.  I have a quick question- Appears that this proc sql will be performing cartesian join and it has taken over 30 minutes already to run- and its still running.  Each of the tables contain about 4+ million records.  Do you know of a way to speed up the process?</description>
      <pubDate>Wed, 27 Jun 2018 16:23:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Column-search-to-produce-final-column/m-p/473813#M71061</guid>
      <dc:creator>BaileyY</dc:creator>
      <dc:date>2018-06-27T16:23:31Z</dc:date>
    </item>
    <item>
      <title>Re: Column search to produce final column</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Column-search-to-produce-final-column/m-p/473856#M71066</link>
      <description>&lt;P&gt;Ah yes.&amp;nbsp; It occurs to me that using WHICHN might have convinced proc sql not to index table1 for lookup, which would result in using a cartesian join..&amp;nbsp; In that case, you can build your own lookup table incorporating all three variables as a single column (lookupkey):&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
  create table need as select oldkey1 as lookupkey,finalkey3 from table1 
       union           select newkey2 as lookupkey,finalkey3 from table1
       union           select finalkey3 as lookupkey,finalkey3 from table1;
  create table want as select t2.*, n.finalkey3 from table2 as t2 left join need as n on t2.key=n.lookupkey;
quit;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I think that PROC SQL might decide to make an index of need based on lookupkey.&amp;nbsp;&amp;nbsp; See if that's faster.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also, the nice thing about the UNION operator is that it removes all duplicate&amp;nbsp;&amp;nbsp; LOOKUPKEY/FINALKEY combinations.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 27 Jun 2018 18:13:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Column-search-to-produce-final-column/m-p/473856#M71066</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2018-06-27T18:13:48Z</dc:date>
    </item>
    <item>
      <title>Re: Column search to produce final column</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Column-search-to-produce-final-column/m-p/473867#M71067</link>
      <description>&lt;P&gt;This data step code builds its own lookup table as a hash object.&amp;nbsp; It might be faster than the SQL:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set table1 (in=int1)  table2;
  if _n_=1 then do;
    declare hash h ();
      h.definekey('lookupkey');
      h.definedata('finalkey3');
      h.definedone();
  end;
  ** If this is a table 1 record, add lookup keys to the hash object **;
  if int1 then do lookupkey=oldkey1,newkey2,finalkey3;
    rc=h.add();
  end;

  ** Now continue processing only table2 records **;
  if int1=0;
  rc=h.find(key:key);
  drop oldkey1--finalkey3 lookupkey rc;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The RC (return code) variable yields a zero only when the FIND method or ADD method is successful.&amp;nbsp; I generate it not for testing purposes, but only to avoid error messages.&lt;/P&gt;</description>
      <pubDate>Wed, 27 Jun 2018 18:37:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Column-search-to-produce-final-column/m-p/473867#M71067</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2018-06-27T18:37:39Z</dc:date>
    </item>
  </channel>
</rss>

