<?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: SQL equivalent of Merge by if a and not b in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/SQL-equivalent-of-Merge-by-if-a-and-not-b/m-p/832455#M329037</link>
    <description>&lt;P&gt;Actually without examples of the data sets A and B I would not suggest an SQL "equivalent" because MERGE treats same-named variables that appear in both data sets, other than the by variables, quite differently than an SQL join of any flavor. If you have other variables of the same name to get the the same result with SQL you would need either Coalesce(b.varname,a.varname) as Varname for Numeric variables or Coalescec(b.charvar, a.charvar) as Charvar for each pair of like named variables.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 09 Sep 2022 10:14:08 GMT</pubDate>
    <dc:creator>ballardw</dc:creator>
    <dc:date>2022-09-09T10:14:08Z</dc:date>
    <item>
      <title>SQL equivalent of Merge by if a and not b</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SQL-equivalent-of-Merge-by-if-a-and-not-b/m-p/832421#M329016</link>
      <description>&lt;P&gt;Hello everyone&amp;nbsp;&lt;/P&gt;&lt;P&gt;Could you please help me to find the Proc SQL equivalent for the following SAS Data Step? Thanks a lot&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;data keepc;
merge a b;
by member_i;
if a and not b;
run;&lt;/CODE&gt;&lt;/PRE&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;</description>
      <pubDate>Fri, 09 Sep 2022 00:30:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SQL-equivalent-of-Merge-by-if-a-and-not-b/m-p/832421#M329016</guid>
      <dc:creator>wbsjd</dc:creator>
      <dc:date>2022-09-09T00:30:06Z</dc:date>
    </item>
    <item>
      <title>Re: SQL equivalent of Merge by if a and not b</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SQL-equivalent-of-Merge-by-if-a-and-not-b/m-p/832427#M329021</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
  create table keep as
  select A.*
  from a as a
  left join
 (select member_i 
  from b) as B
  on a.member_i = b.member_i
  where missing(b.member_i)  
;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;There's a few different ways of doing this and the above is one.&lt;/P&gt;</description>
      <pubDate>Fri, 09 Sep 2022 02:37:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SQL-equivalent-of-Merge-by-if-a-and-not-b/m-p/832427#M329021</guid>
      <dc:creator>SASKiwi</dc:creator>
      <dc:date>2022-09-09T02:37:43Z</dc:date>
    </item>
    <item>
      <title>Re: SQL equivalent of Merge by if a and not b</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SQL-equivalent-of-Merge-by-if-a-and-not-b/m-p/832433#M329022</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table keepc as
  select *
  from a
  where a.member_i not in (select member_i from b)
;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Be aware that sub-selects are notoriously slow.&lt;/P&gt;
&lt;P&gt;The fastest method for your task is this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data keepc;
set a;
if _n_ = 1
then do;
  declare hash b (dataset:"b");
  b.definekey("member_i");
  b.definedone();
end;
if b.check() ne 0;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;as it requires no prior sorting.&lt;/P&gt;</description>
      <pubDate>Fri, 09 Sep 2022 04:42:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SQL-equivalent-of-Merge-by-if-a-and-not-b/m-p/832433#M329022</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2022-09-09T04:42:21Z</dc:date>
    </item>
    <item>
      <title>Re: SQL equivalent of Merge by if a and not b</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SQL-equivalent-of-Merge-by-if-a-and-not-b/m-p/832438#M329026</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/308056"&gt;@wbsjd&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hello everyone&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Could you please help me to find the Proc SQL equivalent for the following SAS Data Step? Thanks a lot&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=""&gt;data keepc;
merge a b;
by member_i;
if a and not b;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;You've got good answers from very experienced users. I'm a bit astonished though that they haven't picked up on the code you've shared as it's syntactically not correct. The correct code would be:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data keepc;
  merge a(in=a) b(in=b);
  by member_i;
  if a and not b;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you just want to keep the rows from table a that don't have a matching key in table b then below SQL should work&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
  select *
  from a
  where not exists
  (select * from b where a.member_i=b.member_i)
  ;
quit;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 09 Sep 2022 07:56:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SQL-equivalent-of-Merge-by-if-a-and-not-b/m-p/832438#M329026</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2022-09-09T07:56:17Z</dc:date>
    </item>
    <item>
      <title>Re: SQL equivalent of Merge by if a and not b</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SQL-equivalent-of-Merge-by-if-a-and-not-b/m-p/832455#M329037</link>
      <description>&lt;P&gt;Actually without examples of the data sets A and B I would not suggest an SQL "equivalent" because MERGE treats same-named variables that appear in both data sets, other than the by variables, quite differently than an SQL join of any flavor. If you have other variables of the same name to get the the same result with SQL you would need either Coalesce(b.varname,a.varname) as Varname for Numeric variables or Coalescec(b.charvar, a.charvar) as Charvar for each pair of like named variables.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 09 Sep 2022 10:14:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SQL-equivalent-of-Merge-by-if-a-and-not-b/m-p/832455#M329037</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2022-09-09T10:14:08Z</dc:date>
    </item>
    <item>
      <title>Re: SQL equivalent of Merge by if a and not b</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SQL-equivalent-of-Merge-by-if-a-and-not-b/m-p/832456#M329038</link>
      <description>&lt;P&gt;Actually without examples of the data sets A and B I would not suggest an SQL "equivalent" because MERGE treats same-named variables that appear in both data sets, other than the by variables, quite differently than an SQL join of any flavor. If you have other variables of the same name to get the the same result with SQL you would need either Coalesce(b.varname,a.varname) as Varname for Numeric variables or Coalescec(b.charvar, a.charvar) as Charvar for each pair of like named variables.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 09 Sep 2022 10:14:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SQL-equivalent-of-Merge-by-if-a-and-not-b/m-p/832456#M329038</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2022-09-09T10:14:23Z</dc:date>
    </item>
  </channel>
</rss>

