<?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: Proc SQL Left Join with multiple conditions when no matching record found in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Proc-SQL-Left-Join-with-multiple-conditions-when-no-matching/m-p/948698#M371166</link>
    <description>&lt;P&gt;Thanks for the reply &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/10892"&gt;@PaigeMiller&lt;/a&gt;&amp;nbsp;. I had a look online about CASE WHEN in the ON clause but I could not find anything relevant with the LEFT JOIN, so I am not sure how this may work. Any ideas?&lt;BR /&gt;&lt;BR /&gt;Another solution that I thought of was to do three LEFT JOINs in the same PROC SQL and then do a DATA step with COALESCE to get what I want (btw &lt;EM&gt;loc&lt;/EM&gt; variables are numeric). But it would be great to know if PROC SQL can perform this without the DATA step. Thanks&lt;/P&gt;</description>
    <pubDate>Wed, 23 Oct 2024 08:30:53 GMT</pubDate>
    <dc:creator>chrmav</dc:creator>
    <dc:date>2024-10-23T08:30:53Z</dc:date>
    <item>
      <title>Proc SQL Left Join with multiple conditions when no matching record found</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Proc-SQL-Left-Join-with-multiple-conditions-when-no-matching/m-p/948626#M371135</link>
      <description>&lt;P&gt;Hello Team,&lt;/P&gt;&lt;P&gt;I wondered if users had any experience with "multiple conditional" LEFT JOINs in a PROC SQL?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;What I currently have is a PROC SQL with a LEFT JOIN operation that retrieves the variable &lt;EM&gt;reference&lt;/EM&gt; based on &lt;EM&gt;id,&lt;/EM&gt; &lt;EM&gt;date&lt;/EM&gt;, and &lt;EM&gt;loc1&lt;/EM&gt;, which you can see in the code below (I am using SAS 9.4.):&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;&lt;FONT color="#000080"&gt;proc sql;&lt;/FONT&gt;
&lt;FONT color="#3366FF"&gt;create table&lt;/FONT&gt; table1 &lt;FONT color="#3366FF"&gt;as&lt;/FONT&gt;
&lt;FONT color="#3366FF"&gt;select&lt;/FONT&gt; &lt;FONT color="#339966"&gt;a.&lt;/FONT&gt;*,b.reference
&lt;FONT color="#3366FF"&gt;from&lt;/FONT&gt; event1 &lt;FONT color="#3366FF"&gt;as&lt;/FONT&gt; a
&lt;FONT color="#3366FF"&gt;left join&lt;/FONT&gt; master1 &lt;FONT color="#3366FF"&gt;as&lt;/FONT&gt; b &lt;FONT color="#3366FF"&gt;on&lt;/FONT&gt; a.id = b.id &lt;FONT color="#3366FF"&gt;and&lt;/FONT&gt; a.date between b.date1 &lt;FONT color="#3366FF"&gt;and&lt;/FONT&gt; b.date2 &lt;FONT color="#3366FF"&gt;and&lt;/FONT&gt; a.loc1=b.loc1 &lt;FONT color="#3366FF"&gt;and&lt;/FONT&gt; a.loc1^=&lt;FONT color="#339966"&gt;.&lt;/FONT&gt;;
&lt;FONT color="#339966"&gt;/* left join master1 as b on a.id = b.id and a.date between b.date1 and b.date2 and a.loc1=b.loc2 and a.loc1^=.;*/
/* left join master1 as b on a.id = b.id and a.date between b.date1 and b.date2 and a.loc3=b.loc3 and a.loc3^=.;*/&lt;/FONT&gt;
&lt;FONT color="#000080"&gt;quit;&lt;/FONT&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;What I would like to do (in the same PROC SQL statement) is to have options for alternative locations (as &lt;EM&gt;id&lt;/EM&gt; and &lt;EM&gt;date&lt;/EM&gt; cannot change in my context) when there are no matching records in the right table (i.e., master1). For example, if there are no records for some rows, I would like to retrieve matches on a.loc1 = b.loc2, and if there is still not a match, then to retrieve matches based on a.loc3 = b.loc3 (see the comments in the SAS code above for the two additional LEFT JOIN operations that I would like to incorporate - in same way - in the PROC SQL). However, I would need to instruct SAS to use the second (or third) LEFT JOIN only for the rows where the current LEFT JOIN operation found no matching records in the right table. So when the first LEFT JOIN finds no matching records for some (or all) rows, move on to the second one, and finally to the third one, assuming the second one has some no matching records either.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Additionally, it would be great if the SAS code can create three additional variables (i.e., additional to the variable &lt;EM&gt;reference&lt;/EM&gt;) where I can tell which LEFT JOIN operation the SAS code used for the matching record, say a dummy variable equal to 1 for var1 (when &lt;EM&gt;loc1&lt;/EM&gt; was used, zero otherwise),&amp;nbsp; equal to 1 for var2 (when &lt;EM&gt;loc2&lt;/EM&gt; was used, zero otherwise), and equal to 1 for var3 (when &lt;EM&gt;loc3&lt;/EM&gt; was used, zero otherwise).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I would appreciate your help on this one and any advice (or example) as to how I can adjust the code in the above example, please.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 22 Oct 2024 18:48:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Proc-SQL-Left-Join-with-multiple-conditions-when-no-matching/m-p/948626#M371135</guid>
      <dc:creator>chrmav</dc:creator>
      <dc:date>2024-10-22T18:48:12Z</dc:date>
    </item>
    <item>
      <title>Re: Proc SQL Left Join with multiple conditions when no matching record found</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Proc-SQL-Left-Join-with-multiple-conditions-when-no-matching/m-p/948627#M371136</link>
      <description>&lt;P&gt;CASE WHEN works in the ON clause&lt;/P&gt;</description>
      <pubDate>Tue, 22 Oct 2024 17:15:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Proc-SQL-Left-Join-with-multiple-conditions-when-no-matching/m-p/948627#M371136</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2024-10-22T17:15:24Z</dc:date>
    </item>
    <item>
      <title>Re: Proc SQL Left Join with multiple conditions when no matching record found</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Proc-SQL-Left-Join-with-multiple-conditions-when-no-matching/m-p/948698#M371166</link>
      <description>&lt;P&gt;Thanks for the reply &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/10892"&gt;@PaigeMiller&lt;/a&gt;&amp;nbsp;. I had a look online about CASE WHEN in the ON clause but I could not find anything relevant with the LEFT JOIN, so I am not sure how this may work. Any ideas?&lt;BR /&gt;&lt;BR /&gt;Another solution that I thought of was to do three LEFT JOINs in the same PROC SQL and then do a DATA step with COALESCE to get what I want (btw &lt;EM&gt;loc&lt;/EM&gt; variables are numeric). But it would be great to know if PROC SQL can perform this without the DATA step. Thanks&lt;/P&gt;</description>
      <pubDate>Wed, 23 Oct 2024 08:30:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Proc-SQL-Left-Join-with-multiple-conditions-when-no-matching/m-p/948698#M371166</guid>
      <dc:creator>chrmav</dc:creator>
      <dc:date>2024-10-23T08:30:53Z</dc:date>
    </item>
    <item>
      <title>Re: Proc SQL Left Join with multiple conditions when no matching record found</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Proc-SQL-Left-Join-with-multiple-conditions-when-no-matching/m-p/948702#M371168</link>
      <description>You could try using OR in the join criteria. You might end up with duplicates that way, but I know too little of your data to give more specific advice.&lt;BR /&gt;Is it possible to post test data as datalines in a data step?</description>
      <pubDate>Wed, 23 Oct 2024 09:25:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Proc-SQL-Left-Join-with-multiple-conditions-when-no-matching/m-p/948702#M371168</guid>
      <dc:creator>LinusH</dc:creator>
      <dc:date>2024-10-23T09:25:33Z</dc:date>
    </item>
    <item>
      <title>Re: Proc SQL Left Join with multiple conditions when no matching record found</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Proc-SQL-Left-Join-with-multiple-conditions-when-no-matching/m-p/948719#M371172</link>
      <description>&lt;P&gt;Hello&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/470636"&gt;@chrmav&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I think you can combine &lt;A href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13674" target="_blank" rel="noopener"&gt;LinusH&lt;/A&gt;'s and &lt;A href="https://communities.sas.com/t5/user/viewprofilepage/user-id/10892" target="_blank" rel="noopener"&gt;PaigeMiller&lt;/A&gt;'s ideas: Use an OR condition in the ON clause and a corresponding CASE expression in the SELECT statement to identify the matching criterion (e.g., in a variable CRIT with values 1, 2, 3 [for the first, second and third criterion, resp.] and missing [if none of the three criteria was satisfied]). Then you can use that variable CRIT in a HAVING clause to eliminate the duplicate matches mentioned by LinusH, prioritizing CRIT=1 over CRIT=2 and both over CRIT3. The pertinent GROUP BY clause would use a combination of variables (or a single variable) identifying the observations of dataset EVENT1 uniquely. In the code example below I use the combination of ID, DATE, LOC1 and LOC3, but you may have better ideas, knowing the data. If there is no unique key, you should create one (e.g., the observation number) in a preliminary DATA step (view). Obviously, variable CRIT combines the information of the three indicator variables you have described.&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.reference, case when .^=a.loc1=b.loc1 then 1
                              when .^=a.loc1=b.loc2 then 2
                              when .^=a.loc3=b.loc3 then 3
                              else .
                         end as crit
from event1 as a
left join master1 as b on a.id = b.id and a.date between b.date1 and b.date2
                           and (.^=a.loc1=b.loc1 or .^=a.loc1=b.loc2 or .^=a.loc3=b.loc3)
group by a.id, a.date, a.loc1, a.loc3  /* or rather: group by a.&amp;lt;unique key variable(s) in EVENT1&amp;gt; */
having crit=min(crit);
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 23 Oct 2024 11:45:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Proc-SQL-Left-Join-with-multiple-conditions-when-no-matching/m-p/948719#M371172</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2024-10-23T11:45:08Z</dc:date>
    </item>
    <item>
      <title>Re: Proc SQL Left Join with multiple conditions when no matching record found</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Proc-SQL-Left-Join-with-multiple-conditions-when-no-matching/m-p/948908#M371228</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/32733"&gt;@FreelanceReinh&lt;/a&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I ran the code you suggested and it appears to accomplish exactly what I need. Additionally, it is quite versatile, as I can easily adjust the matching order by specifying the priority in the CASE expression - indicating which option should be first (1), second (2), or third (3).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you so much for you time and effort on this.&lt;/P&gt;</description>
      <pubDate>Thu, 24 Oct 2024 14:37:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Proc-SQL-Left-Join-with-multiple-conditions-when-no-matching/m-p/948908#M371228</guid>
      <dc:creator>chrmav</dc:creator>
      <dc:date>2024-10-24T14:37:09Z</dc:date>
    </item>
  </channel>
</rss>

