<?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: match on one of possible keys in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/match-on-one-of-possible-keys/m-p/432709#M107197</link>
    <description>&lt;P&gt;Thank you.&lt;/P&gt;
&lt;P&gt;it should do what I need (there was an error in my data example).&lt;/P&gt;
&lt;P&gt;I prefer the sql solution over the transpose +&amp;nbsp;merge since I hope to pass it to the DBMS for execution.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 31 Jan 2018 13:21:00 GMT</pubDate>
    <dc:creator>ciro</dc:creator>
    <dc:date>2018-01-31T13:21:00Z</dc:date>
    <item>
      <title>match on one of possible keys</title>
      <link>https://communities.sas.com/t5/SAS-Programming/match-on-one-of-possible-keys/m-p/432637#M107177</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;I have two datasets to merge by two variables as in the example.&lt;/P&gt;
&lt;P&gt;the first&amp;nbsp;variables is common to both dataset (in the example key0).&lt;/P&gt;
&lt;P&gt;the second is called "key" in the second dataset and can be either "key1" or "key2" or both or neither in the first dataset.&lt;/P&gt;
&lt;P&gt;I would like to match each record of the second dataset by key0 and the first matchable between key1 and key2.&lt;/P&gt;
&lt;P&gt;moreover I would like have a variable "keymach" that tells which&amp;nbsp;of the two variables (key1 and/or key2)&amp;nbsp;is equal to key.&amp;nbsp;.&lt;/P&gt;
&lt;P&gt;any&amp;nbsp;hint is appreciated .&lt;/P&gt;
&lt;P&gt;moreover the data are very large dbms tables,so any suggestion to have an efficient code is preferred.&lt;/P&gt;
&lt;P&gt;thank you very much in advance&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;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data a;&lt;BR /&gt;input key0 key1 key2 value_A;&lt;BR /&gt;cards;&lt;BR /&gt;1 1 1 10&lt;BR /&gt;1 2 3 15&lt;BR /&gt;1 3 4 20&lt;BR /&gt;;&lt;BR /&gt;data b;&lt;BR /&gt;input key0 key;&lt;BR /&gt;cards;&lt;BR /&gt;1 1 &lt;BR /&gt;1 2&lt;BR /&gt;1 3&lt;BR /&gt;1 4&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data want;&lt;BR /&gt;input key0 key1 key2 value_A key keymatch $2.;&lt;BR /&gt;cards;&lt;BR /&gt;1 1 1 10 1 11&lt;BR /&gt;1 2 3 15 2 10&lt;BR /&gt;1 2 3 15 3 01&lt;BR /&gt;1 3 4 20 4 01&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;</description>
      <pubDate>Wed, 31 Jan 2018 08:57:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/match-on-one-of-possible-keys/m-p/432637#M107177</guid>
      <dc:creator>ciro</dc:creator>
      <dc:date>2018-01-31T08:57:23Z</dc:date>
    </item>
    <item>
      <title>Re: match on one of possible keys</title>
      <link>https://communities.sas.com/t5/SAS-Programming/match-on-one-of-possible-keys/m-p/432643#M107181</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This is just a start as I don't really understand your want dataset:&lt;/P&gt;
&lt;PRE&gt;data a;
  input key0 key1 key2 value_A;
cards;
1 1 1 10
1 2 3 15
1 3 4 20
;
run;
data b;
  input key0 key;
cards;
1 1 
1 2
1 3
1 4
;
run;
proc transpose data=b out=inter prefix=val;
  by key0;
  var key;
run;
data want;
  merge a inter;
  by key0;
  array val{4};
  do i=1 to 4;
    if val{i}=key1 and key=. then key=i;
  end;
run;&lt;/PRE&gt;
&lt;P&gt;So basically transpose the second table up, merge on key0 then use an array to find the data you want.&lt;/P&gt;</description>
      <pubDate>Wed, 31 Jan 2018 09:21:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/match-on-one-of-possible-keys/m-p/432643#M107181</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2018-01-31T09:21:26Z</dc:date>
    </item>
    <item>
      <title>Re: match on one of possible keys</title>
      <link>https://communities.sas.com/t5/SAS-Programming/match-on-one-of-possible-keys/m-p/432697#M107189</link>
      <description>&lt;P&gt;This seems to match your specification:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
  create table want as select
  a.key0,
  a.key1,
  a.key2,
  a.value_A,
  b.key,
  cats(b.key=a.key1,b.key=a.key2) length=2 as keymatch
  from a,b
  where a.key0=b.key0
    and (a.key1=b.key or
         a.key2=b.key)
  ;
quit;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;But it does not quite match the data you present. Why is the second&amp;nbsp;last row in B not matched to the last row in A in your example?&lt;/P&gt;</description>
      <pubDate>Wed, 31 Jan 2018 12:25:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/match-on-one-of-possible-keys/m-p/432697#M107189</guid>
      <dc:creator>s_lassen</dc:creator>
      <dc:date>2018-01-31T12:25:22Z</dc:date>
    </item>
    <item>
      <title>Re: match on one of possible keys</title>
      <link>https://communities.sas.com/t5/SAS-Programming/match-on-one-of-possible-keys/m-p/432709#M107197</link>
      <description>&lt;P&gt;Thank you.&lt;/P&gt;
&lt;P&gt;it should do what I need (there was an error in my data example).&lt;/P&gt;
&lt;P&gt;I prefer the sql solution over the transpose +&amp;nbsp;merge since I hope to pass it to the DBMS for execution.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 31 Jan 2018 13:21:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/match-on-one-of-possible-keys/m-p/432709#M107197</guid>
      <dc:creator>ciro</dc:creator>
      <dc:date>2018-01-31T13:21:00Z</dc:date>
    </item>
  </channel>
</rss>

