<?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 if any of a set of variables matches in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Merge-if-any-of-a-set-of-variables-matches/m-p/546033#M151143</link>
    <description>&lt;P&gt;Thanks, but again incomplete. In your sql code three datasets are mentioned: a, b and want. Which of the three is "have"?&lt;/P&gt;</description>
    <pubDate>Tue, 26 Mar 2019 06:47:04 GMT</pubDate>
    <dc:creator>andreas_lds</dc:creator>
    <dc:date>2019-03-26T06:47:04Z</dc:date>
    <item>
      <title>Merge if any of a set of variables matches</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merge-if-any-of-a-set-of-variables-matches/m-p/545918#M151099</link>
      <description>&lt;P&gt;I want to merge (using left join) using the id variable in each set and based on var1, var2, var3...etc. (var1-36).&amp;nbsp; However, I don't want to require that they all match.&amp;nbsp; For instance, I want the information to merge whether the only match is a.var2 and b.var7 or if ten match or if they all match.&amp;nbsp; Furthermore, there are missing values for some variables for almost all observations, so I want to ensure that missing matches are not captured.&amp;nbsp; Any help is appreciated.&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table want as
select 		a.*, b.info
from 		have1 as a left join have2 as b
on			a.id = b.id and
 (a.var1-36 = b.var1-36)
order by           id, date;
quit; &lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 25 Mar 2019 18:30:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merge-if-any-of-a-set-of-variables-matches/m-p/545918#M151099</guid>
      <dc:creator>jjb123</dc:creator>
      <dc:date>2019-03-25T18:30:09Z</dc:date>
    </item>
    <item>
      <title>Re: Merge if any of a set of variables matches</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merge-if-any-of-a-set-of-variables-matches/m-p/545922#M151102</link>
      <description>&lt;P&gt;Are you saying you want a match of the IDs match and a.var1 is equal to any of the fields b.var1 through b.var36 ?&lt;/P&gt;</description>
      <pubDate>Mon, 25 Mar 2019 18:48:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merge-if-any-of-a-set-of-variables-matches/m-p/545922#M151102</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2019-03-25T18:48:40Z</dc:date>
    </item>
    <item>
      <title>Re: Merge if any of a set of variables matches</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merge-if-any-of-a-set-of-variables-matches/m-p/545930#M151106</link>
      <description>&lt;P&gt;Sort of.&amp;nbsp; I want a match if the IDs match and any of a.var1, a.var2, a.var3 (i.e., a.var1-36) is equal to any of b.var1, b.var2, b.var3 (i.e., b.var1-36).&amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 25 Mar 2019 19:11:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merge-if-any-of-a-set-of-variables-matches/m-p/545930#M151106</guid>
      <dc:creator>jjb123</dc:creator>
      <dc:date>2019-03-25T19:11:05Z</dc:date>
    </item>
    <item>
      <title>Re: Merge if any of a set of variables matches</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merge-if-any-of-a-set-of-variables-matches/m-p/545952#M151115</link>
      <description>&lt;P&gt;Example input data from both sets and the result needed.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 25 Mar 2019 20:21:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merge-if-any-of-a-set-of-variables-matches/m-p/545952#M151115</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2019-03-25T20:21:21Z</dc:date>
    </item>
    <item>
      <title>Re: Merge if any of a set of variables matches</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merge-if-any-of-a-set-of-variables-matches/m-p/545987#M151128</link>
      <description>&lt;P&gt;Assume the first data set is the have dataset (it can be have1 and have2 for simplicity).&amp;nbsp; Assume the second is the desired result.&amp;nbsp; The variables I used are a little different, so I'll update my original code as well.&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table want as
select 		a.*, b.info as merged_info
from 		have as a left join have as b
on			a.unique ne b.unique and a.id = b.id and
 (a.var1-4 = b.var1-4)
order by           unique, id;
quit; &lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="image.png" style="width: 600px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/28195i1C9833F1FEE717E2/image-size/large?v=v2&amp;amp;px=999" role="button" title="image.png" alt="image.png" /&gt;&lt;/span&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="image.png" style="width: 600px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/28196iEAC29158A9C5DACB/image-size/large?v=v2&amp;amp;px=999" role="button" title="image.png" alt="image.png" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 25 Mar 2019 22:45:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merge-if-any-of-a-set-of-variables-matches/m-p/545987#M151128</guid>
      <dc:creator>jjb123</dc:creator>
      <dc:date>2019-03-25T22:45:10Z</dc:date>
    </item>
    <item>
      <title>Re: Merge if any of a set of variables matches</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merge-if-any-of-a-set-of-variables-matches/m-p/545989#M151130</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/180584"&gt;@jjb123&lt;/a&gt;&amp;nbsp;wrote&lt;BR /&gt;
&lt;P&gt;.... Furthermore, there are missing values for some variables for almost all observations, so I want to ensure that missing matches are not captured.&amp;nbsp; Any help is appreciated.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So what do you want to do if the variable is missing in one&amp;nbsp; data set and not missing in the other? Does that constitute a non-match?&lt;/P&gt;</description>
      <pubDate>Mon, 25 Mar 2019 22:47:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merge-if-any-of-a-set-of-variables-matches/m-p/545989#M151130</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2019-03-25T22:47:39Z</dc:date>
    </item>
    <item>
      <title>Re: Merge if any of a set of variables matches</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merge-if-any-of-a-set-of-variables-matches/m-p/545992#M151131</link>
      <description>&lt;P&gt;I'm not sure I understand your question.&amp;nbsp; A missing value (which would only be a match if it matches to another missing value) should always be considered a non-match.&amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 25 Mar 2019 23:23:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merge-if-any-of-a-set-of-variables-matches/m-p/545992#M151131</guid>
      <dc:creator>jjb123</dc:creator>
      <dc:date>2019-03-25T23:23:38Z</dc:date>
    </item>
    <item>
      <title>Re: Merge if any of a set of variables matches</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merge-if-any-of-a-set-of-variables-matches/m-p/546005#M151133</link>
      <description>If you want (tested) code provide data in usable form.</description>
      <pubDate>Tue, 26 Mar 2019 01:52:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merge-if-any-of-a-set-of-variables-matches/m-p/546005#M151133</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2019-03-26T01:52:00Z</dc:date>
    </item>
    <item>
      <title>Re: Merge if any of a set of variables matches</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merge-if-any-of-a-set-of-variables-matches/m-p/546019#M151137</link>
      <description>&lt;P&gt;Here you go.&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input unique id var1 var2 var3 var4 info;
datalines;
1 1 . . . 1234 54
2 1 32423 3713 1234 328931 26
3 1 3713 1234 123412 3253 82
4 2 4567 . . 12 93
5 2 . . . 1267 102
6 2 12 145 86 92 96
7 3 . 8214 1479 . 123
8 3 . . . . 85
9 3 987 345 7528 93842 146
;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 26 Mar 2019 03:32:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merge-if-any-of-a-set-of-variables-matches/m-p/546019#M151137</guid>
      <dc:creator>jjb123</dc:creator>
      <dc:date>2019-03-26T03:32:40Z</dc:date>
    </item>
    <item>
      <title>Re: Merge if any of a set of variables matches</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merge-if-any-of-a-set-of-variables-matches/m-p/546033#M151143</link>
      <description>&lt;P&gt;Thanks, but again incomplete. In your sql code three datasets are mentioned: a, b and want. Which of the three is "have"?&lt;/P&gt;</description>
      <pubDate>Tue, 26 Mar 2019 06:47:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merge-if-any-of-a-set-of-variables-matches/m-p/546033#M151143</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2019-03-26T06:47:04Z</dc:date>
    </item>
    <item>
      <title>Re: Merge if any of a set of variables matches</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merge-if-any-of-a-set-of-variables-matches/m-p/546035#M151144</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/180584"&gt;@jjb123&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If using SQL with SAS tables then here one way to go.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input unique id var1 var2 var3 var4 info;
datalines;
1 1 . . . 1234 54
2 1 32423 3713 1234 328931 26
3 1 3713 1234 123412 3253 82
4 2 4567 . . 12 93
5 2 . . . 1267 102
6 2 12 145 86 92 96
7 3 . 8214 1479 . 123
8 3 . . . . 85
9 3 987 345 7528 93842 146
;
run;

proc sql;
  create table want as
    select  a.*, b.info as merged_info
      from  have as a left join have as b
        on  a.unique ne b.unique and a.id = b.id and
            (
              whichn(a.var1,.,b.var1,b.var2,b.var3,b.var4) &amp;gt;1 or
              whichn(a.var2,.,b.var1,b.var2,b.var3,b.var4) &amp;gt;1 or
              whichn(a.var3,.,b.var1,b.var2,b.var3,b.var4) &amp;gt;1 or
              whichn(a.var4,.,b.var1,b.var2,b.var3,b.var4) &amp;gt;1
            )
      order by unique, id
      ;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 26 Mar 2019 07:10:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merge-if-any-of-a-set-of-variables-matches/m-p/546035#M151144</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2019-03-26T07:10:33Z</dc:date>
    </item>
    <item>
      <title>Re: Merge if any of a set of variables matches</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merge-if-any-of-a-set-of-variables-matches/m-p/546311#M151240</link>
      <description>&lt;P&gt;This works like a charm. Thank you.&lt;/P&gt;</description>
      <pubDate>Tue, 26 Mar 2019 21:11:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merge-if-any-of-a-set-of-variables-matches/m-p/546311#M151240</guid>
      <dc:creator>jjb123</dc:creator>
      <dc:date>2019-03-26T21:11:09Z</dc:date>
    </item>
  </channel>
</rss>

