<?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: Merge data sets with alternative 2 keys in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Merge-data-sets-with-alternative-2-keys/m-p/830716#M328254</link>
    <description>&lt;P&gt;I will change the data in data sets and then your code will not work well.&lt;BR /&gt;&lt;BR /&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data t1;
format date date9.;
input Key1 Key2 wealth date : date9.;
cards;
11111 487777 100000 10AUG2022
22222 987231 200000 10AUG2022
33333 321567 300000 10AUG2022
. 897277 400000 10AUG2022
;
Run;


Data t2;
format date date9.;
input Key1 Key2 wealth date : date9.;
cards;
11111 487777 120000 01JAN2022
22222 987231 170000 01JAN2022
33333 . 200000 01JAN2022
44444 897277 0 01JAN2022
;
Run&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Sat, 27 Aug 2022 06:43:07 GMT</pubDate>
    <dc:creator>Ronein</dc:creator>
    <dc:date>2022-08-27T06:43:07Z</dc:date>
    <item>
      <title>Merge data sets with alternative 2 keys</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merge-data-sets-with-alternative-2-keys/m-p/830465#M328154</link>
      <description>&lt;P&gt;Hello&lt;/P&gt;
&lt;P&gt;I want to merge 2 data sets.&lt;/P&gt;
&lt;P&gt;The issue is that there are 2 optional Keys to merge between the data sets(Key1,Key2).&lt;/P&gt;
&lt;P&gt;Sometimes Key1 is available and sometimes Key2 is available and sometimes both are available.&lt;/P&gt;
&lt;P&gt;What do you think is a better way to merge : way1 or way2?&lt;/P&gt;
&lt;P&gt;Maybe there is an alternative better way that you can recommend?&lt;/P&gt;
&lt;P&gt;thanks a lot&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data t1;
format date date9.;
input Key1 Key2 wealth date : date9.;
cards;
11111 487777 100000 10AUG2022
22222 987231 200000 10AUG2022
33333 . 300000 10AUG2022
. 897277 400000 10AUG2022
;
Run;


Data t2;
format date date9.;
input Key1 Key2 wealth date : date9.;
cards;
11111 487777 120000 01JAN2022
22222 987231 170000 01JAN2022
33333 . 200000 01JAN2022
. 897277 0 01JAN2022
;
Run;

proc sql;
create table way1 as
select a.Key1,a.Key2, a.wealth_10Aug2022,b.wealth_01JAN2022
from t1(rename=(wealth=wealth_10Aug2022)) as a
left join t2(rename=(wealth=wealth_01JAN2022)) as b
on a.Key1=b.Key1  OR a.Key2=b.Key2
order by a.key1,a.key2
;
quit;


proc sql;
create table way2 as
select a.Key1,a.Key2,a.wealth_10Aug2022,coalesce(b.wealth_01JAN2022,c.wealth_01JAN2022) as wealth_01JAN2022
from t1(rename=(wealth=wealth_10Aug2022)) as a
left join t2(rename=(wealth=wealth_01JAN2022)) as b
on a.Key1=b.Key1   
left join t2(rename=(wealth=wealth_01JAN2022)) as c
on  a.Key2=c.Key2
order by a.key1,a.key2
;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 26 Aug 2022 02:47:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merge-data-sets-with-alternative-2-keys/m-p/830465#M328154</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2022-08-26T02:47:13Z</dc:date>
    </item>
    <item>
      <title>Re: Merge data sets with alternative 2 keys</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merge-data-sets-with-alternative-2-keys/m-p/830714#M328252</link>
      <description>&lt;P&gt;Based on the example data you have provided you can use both keys even for the rows where one of the keys is missing. That's because joining a missing key to a missing key is still a match:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table way1 as
select a.Key1,a.Key2, a.wealth_10Aug2022,b.wealth_01JAN2022
from t1(rename=(wealth=wealth_10Aug2022)) as a
left join t2(rename=(wealth=wealth_01JAN2022)) as b
on a.Key1=b.Key1 and a.Key2=b.Key2
order by a.key1,a.key2
;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 27 Aug 2022 01:40:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merge-data-sets-with-alternative-2-keys/m-p/830714#M328252</guid>
      <dc:creator>SASKiwi</dc:creator>
      <dc:date>2022-08-27T01:40:57Z</dc:date>
    </item>
    <item>
      <title>Re: Merge data sets with alternative 2 keys</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merge-data-sets-with-alternative-2-keys/m-p/830716#M328254</link>
      <description>&lt;P&gt;I will change the data in data sets and then your code will not work well.&lt;BR /&gt;&lt;BR /&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data t1;
format date date9.;
input Key1 Key2 wealth date : date9.;
cards;
11111 487777 100000 10AUG2022
22222 987231 200000 10AUG2022
33333 321567 300000 10AUG2022
. 897277 400000 10AUG2022
;
Run;


Data t2;
format date date9.;
input Key1 Key2 wealth date : date9.;
cards;
11111 487777 120000 01JAN2022
22222 987231 170000 01JAN2022
33333 . 200000 01JAN2022
44444 897277 0 01JAN2022
;
Run&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 27 Aug 2022 06:43:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merge-data-sets-with-alternative-2-keys/m-p/830716#M328254</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2022-08-27T06:43:07Z</dc:date>
    </item>
    <item>
      <title>Re: Merge data sets with alternative 2 keys</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merge-data-sets-with-alternative-2-keys/m-p/830726#M328258</link>
      <description>&lt;P&gt;That's one of these questions where you need to be very specific and besides of representative sample data best also share the desired result.&lt;/P&gt;
&lt;P&gt;What should happen if there is a match on one and on two keys? (look at rows 3 and 5 in below result)&lt;/P&gt;
&lt;P&gt;What if all keys are missing?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data t1;
  format date date9.;
  input id Key1 Key2 wealth date : date9.;
  cards;
1 11111 487777 100000 10AUG2022
2 22222 987231 200000 10AUG2022
3 33333 321567 300000 10AUG2022
4 . 897277 400000 10AUG2022
;

Data t2;
  format date date9.;
  input id Key1 Key2 wealth date : date9.;
  cards;
1 11111 487777 120000 01JAN2022
2 22222 987231 170000 01JAN2022
3 33333 . 200000 01JAN2022
4 44444 897277 0 01JAN2022
;

proc sql;
  create table way1 as
    select 
      a.id as a_id,
      b.id as b_id,
      a.Key1 as a_key1,
      a.Key2 as a_key2, 
      b.Key1 as b_key1,
      b.Key2 as b_key2, 
      a.wealth as wealth_10Aug2022,
      b.wealth as wealth_01JAN2022
    from t1 as a
      left join t2 as b
        on 
          (a.Key1=b.Key1 or n(a.key1,b.key1)=1) 
          and (a.Key2=b.Key2 or n(a.key2,b.key2)=1) 
          and n(a.Key1,a.Key2) &amp;gt;0
      order by a.id, a.key1,a.key2
  ;
quit;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Patrick_0-1661601126346.png" style="width: 580px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/74785iF69A9C4FBA217532/image-dimensions/580x145?v=v2" width="580" height="145" role="button" title="Patrick_0-1661601126346.png" alt="Patrick_0-1661601126346.png" /&gt;&lt;/span&gt;&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>Sat, 27 Aug 2022 11:54:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merge-data-sets-with-alternative-2-keys/m-p/830726#M328258</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2022-08-27T11:54:06Z</dc:date>
    </item>
    <item>
      <title>Re: Merge data sets with alternative 2 keys</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merge-data-sets-with-alternative-2-keys/m-p/830729#M328261</link>
      <description>Hello&lt;BR /&gt;If all keys are missing then it shouldn't be displayed on the wanted data set.&lt;BR /&gt;&lt;BR /&gt;If there is a match on two keys then it should be displayed. &lt;BR /&gt;&lt;BR /&gt;If there is a match on only one key ) key1 or key2) then it should be displayed</description>
      <pubDate>Sat, 27 Aug 2022 12:15:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merge-data-sets-with-alternative-2-keys/m-p/830729#M328261</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2022-08-27T12:15:32Z</dc:date>
    </item>
    <item>
      <title>Re: Merge data sets with alternative 2 keys</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merge-data-sets-with-alternative-2-keys/m-p/830730#M328262</link>
      <description>What does it mean &lt;BR /&gt;n(a.key1,b.key1)=1)</description>
      <pubDate>Sat, 27 Aug 2022 12:17:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merge-data-sets-with-alternative-2-keys/m-p/830730#M328262</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2022-08-27T12:17:21Z</dc:date>
    </item>
    <item>
      <title>Re: Merge data sets with alternative 2 keys</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merge-data-sets-with-alternative-2-keys/m-p/830731#M328263</link>
      <description>&lt;P&gt;&lt;A href="https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.2/lefunctionsref/n0h1d8gxwr1ml7n1qdt35v3i6u7r.htm" target="_blank"&gt;https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.2/lefunctionsref/n0h1d8gxwr1ml7n1qdt35v3i6u7r.htm&lt;/A&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 27 Aug 2022 12:20:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merge-data-sets-with-alternative-2-keys/m-p/830731#M328263</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2022-08-27T12:20:39Z</dc:date>
    </item>
    <item>
      <title>Re: Merge data sets with alternative 2 keys</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merge-data-sets-with-alternative-2-keys/m-p/830733#M328265</link>
      <description>I know this functions but why should you use it in this example ?</description>
      <pubDate>Sat, 27 Aug 2022 12:34:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merge-data-sets-with-alternative-2-keys/m-p/830733#M328265</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2022-08-27T12:34:06Z</dc:date>
    </item>
    <item>
      <title>Re: Merge data sets with alternative 2 keys</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merge-data-sets-with-alternative-2-keys/m-p/830734#M328266</link>
      <description>The number of rows in wanted data set should be equal to number of rows in data set t1. In your code there is increase in number of rows and then it is not the desire result</description>
      <pubDate>Sat, 27 Aug 2022 12:39:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merge-data-sets-with-alternative-2-keys/m-p/830734#M328266</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2022-08-27T12:39:04Z</dc:date>
    </item>
    <item>
      <title>Re: Merge data sets with alternative 2 keys</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merge-data-sets-with-alternative-2-keys/m-p/830744#M328274</link>
      <description>&lt;P&gt;I am not sure I understand the join logic you are looking for.&lt;/P&gt;
&lt;P&gt;Let's just try something an you can tell use why that is or isn't what you want.&lt;/P&gt;
&lt;P&gt;This will test if BOTH keys match (ignoring missing values).&lt;/P&gt;
&lt;P&gt;I used a LEFT JOIN so values from T2 that do not match anything are ignored.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data t1;
  row+1;
  input Key1 Key2 wealth date :date.;
  format date date9.;
cards;
11111 487777 100000 10AUG2022
22222 987231 200000 10AUG2022
33333 321567 300000 10AUG2022
. 897277 400000 10AUG2022
;

data t2;
  row+1;
  input Key1 Key2 wealth date :date.;
  format date date9.;
cards;
11111 487777 120000 01JAN2022
22222 987231 170000 01JAN2022
33333 . 200000 01JAN2022
44444 897277 0 01JAN2022
;


proc sql;
create table want as
  select
    a.row as aRow
  , b.row as bRow 
  , a.Key1 as aKey1
  , b.Key1 as bKey1
  , a.Key2 as aKey2
  , b.Key2 as bKey2
  , a.wealth as aWealth
  , b.wealth as bWealth
  , a.date as aDate
  , b.date as bDate
  from t1 a 
  left join t2 b
    on (a.key1=b.key1 or missing(a.key1) or missing(b.key1))
   and (a.key2=b.key2 or missing(a.key2) or missing(b.key2))
  order by 1,2
;
quit;

proc print;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result&lt;/P&gt;
&lt;PRE&gt;                                                                a         b
Obs    aRow    bRow    aKey1    bKey1     aKey2     bKey2    Wealth    Wealth        aDate        bDate

 1       1       1     11111    11111    487777    487777    100000    120000    10AUG2022    01JAN2022
 2       2       2     22222    22222    987231    987231    200000    170000    10AUG2022    01JAN2022
 3       3       3     33333    33333    321567         .    300000    200000    10AUG2022    01JAN2022
 4       4       3         .    33333    897277         .    400000    200000    10AUG2022    01JAN2022
 5       4       4         .    44444    897277    897277    400000         0    10AUG2022    01JAN2022
&lt;/PRE&gt;
&lt;P&gt;If you want to eliminate that 4th observation in the output where both matches are based on missing values then add an additional condition that excludes such cases.&amp;nbsp; So basically test if either there are two non-missing values of KEY1 or two non-missing values of KEY2.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;   and (n(a.key1,b.key1)&amp;gt;1 or n(a.key2,b.key2)&amp;gt;1)
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;It might be harder to eliminate other types of duplicates.&amp;nbsp; For that you probably need to set some type of ordering criteria and then pick the first (or last) match based on the ordering criteria.&lt;/P&gt;</description>
      <pubDate>Sat, 27 Aug 2022 15:02:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merge-data-sets-with-alternative-2-keys/m-p/830744#M328274</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-08-27T15:02:05Z</dc:date>
    </item>
    <item>
      <title>Re: Merge data sets with alternative 2 keys</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merge-data-sets-with-alternative-2-keys/m-p/830745#M328275</link>
      <description>In my original post I showed way1 and way2 and both are working well</description>
      <pubDate>Sat, 27 Aug 2022 15:32:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merge-data-sets-with-alternative-2-keys/m-p/830745#M328275</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2022-08-27T15:32:38Z</dc:date>
    </item>
  </channel>
</rss>

