<?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 Is there a faster way to join using an 'or' statement? in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/Is-there-a-faster-way-to-join-using-an-or-statement/m-p/685641#M79562</link>
    <description>&lt;P&gt;I have two tables that I want to join, call them ID_table and merge_table.&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data Id_table;
   input Name $ Parent_name $  var1 ;
   datalines;
ABC  ABC_Co 28.6
HLE  ABC_Co 42.1
Ggl_inc google 18.8
APPLE APPL_Inc 123.4
;
run;

data merge_table;
   input Comp_Name $ var2 ;
   datalines;
ABC_Co 110
Ggl_inc 105.3
APPLE 112.1
XYZ 101
;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I want sas to merge on either: Name = comp_name, or Parent_name = comp_name.&lt;/P&gt;&lt;P&gt;If I do this:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;PROC SQL;
	CREATE TABLE merge_test AS
	SELECT l.*, r.*
	FROM Id_table AS l LEFT JOIN merge_table AS r
	ON	 (l.Name = r.Comp_Name or l.Parent_name = r.Comp_Name);
QUIT;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;It works. However, my actual data set has 582k obs and 300k obs in both tables, and when I run the above command it seems to take forever and SAS times out.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Is there a more efficient way to program this?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Furthermore, I still want to keep unmerged matches in my data set, but SAS doesn't seem to be doing so in the above test sample (the values for comp_name = XYZ from merge_table are dropped). Is there&amp;nbsp; a way it is included?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;thanks!&lt;/P&gt;</description>
    <pubDate>Tue, 22 Sep 2020 09:58:21 GMT</pubDate>
    <dc:creator>UniversitySas</dc:creator>
    <dc:date>2020-09-22T09:58:21Z</dc:date>
    <item>
      <title>Is there a faster way to join using an 'or' statement?</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Is-there-a-faster-way-to-join-using-an-or-statement/m-p/685641#M79562</link>
      <description>&lt;P&gt;I have two tables that I want to join, call them ID_table and merge_table.&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data Id_table;
   input Name $ Parent_name $  var1 ;
   datalines;
ABC  ABC_Co 28.6
HLE  ABC_Co 42.1
Ggl_inc google 18.8
APPLE APPL_Inc 123.4
;
run;

data merge_table;
   input Comp_Name $ var2 ;
   datalines;
ABC_Co 110
Ggl_inc 105.3
APPLE 112.1
XYZ 101
;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I want sas to merge on either: Name = comp_name, or Parent_name = comp_name.&lt;/P&gt;&lt;P&gt;If I do this:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;PROC SQL;
	CREATE TABLE merge_test AS
	SELECT l.*, r.*
	FROM Id_table AS l LEFT JOIN merge_table AS r
	ON	 (l.Name = r.Comp_Name or l.Parent_name = r.Comp_Name);
QUIT;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;It works. However, my actual data set has 582k obs and 300k obs in both tables, and when I run the above command it seems to take forever and SAS times out.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Is there a more efficient way to program this?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Furthermore, I still want to keep unmerged matches in my data set, but SAS doesn't seem to be doing so in the above test sample (the values for comp_name = XYZ from merge_table are dropped). Is there&amp;nbsp; a way it is included?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;thanks!&lt;/P&gt;</description>
      <pubDate>Tue, 22 Sep 2020 09:58:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Is-there-a-faster-way-to-join-using-an-or-statement/m-p/685641#M79562</guid>
      <dc:creator>UniversitySas</dc:creator>
      <dc:date>2020-09-22T09:58:21Z</dc:date>
    </item>
    <item>
      <title>Re: Is there a faster way to join using an 'or' statement?</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Is-there-a-faster-way-to-join-using-an-or-statement/m-p/685645#M79563</link>
      <description>&lt;P&gt;Maxim 2: Read the Log.&lt;/P&gt;
&lt;P&gt;The SQL procedure notifies you that it has to do cartesian joins, which is often a bad sign, performancewise.&lt;/P&gt;
&lt;P&gt;Split your join into two, and concatenate the results:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table merge_test as
  select l.*, r.*
  from Id_table as l left join merge_table as r
  on l.Name = r.Comp_Name
  where not missing(r.comp_name)
union
  select l.*, r.*
  from Id_table as l left join merge_table as r
  on l.Parent_name = r.Comp_Name
  where not missing(r.comp_name)
;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 22 Sep 2020 10:10:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Is-there-a-faster-way-to-join-using-an-or-statement/m-p/685645#M79563</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-09-22T10:10:11Z</dc:date>
    </item>
  </channel>
</rss>

