<?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: Left Join not Correct When using Where Clause in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Left-Join-not-Correct-When-using-Where-Clause/m-p/596986#M171984</link>
    <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/10429"&gt;@Doug____&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;I have a left table with 153 unduplicated observations and a right table with 15000+ observations.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If I use a where clause on the left table it only returns 153 records. If I remove the where clause and subset the data prior to query processing it returns the correct result (201 records). Why does this happen? Should it not return the same number of records? It acts like an inner join if the where clause is present regardless of the join used.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;These questions are impossible to answer without having the exact data sets (and code to do the subsetting) that you are using.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Perhaps you could make a smaller example that illustrates the problem, say 10 to 20 records, and provide that to us following these instructions &lt;STRONG&gt;(do not skip this step):&lt;/STRONG&gt;&amp;nbsp;&lt;A href="https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-data-AKA-generate/ta-p/258712" target="_blank"&gt;https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-data-AKA-generate/ta-p/258712&lt;/A&gt;&lt;/P&gt;</description>
    <pubDate>Wed, 16 Oct 2019 16:59:54 GMT</pubDate>
    <dc:creator>PaigeMiller</dc:creator>
    <dc:date>2019-10-16T16:59:54Z</dc:date>
    <item>
      <title>Left Join not Correct When using Where Clause</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Left-Join-not-Correct-When-using-Where-Clause/m-p/596978#M171980</link>
      <description>&lt;P&gt;I have a left table with 153 unduplicated observations and a right table with 15000+ observations.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;If I use a where clause on the left table it only returns 153 records. If I remove the where clause and subset the data prior to query processing it returns the correct result (201 records). Why does this happen? Should it not return the same number of records? It acts like an inner join if the where clause is present regardless of the join used.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This returns 201 records (correct);&lt;BR /&gt;proc sql;&lt;BR /&gt;create table checkit as&lt;BR /&gt;select&lt;BR /&gt;a.an,&lt;BR /&gt;b.an as cfa,&lt;BR /&gt;b.load_date,&lt;BR /&gt;b.amount,&lt;BR /&gt;b.data_source,&lt;BR /&gt;b.fa_code&lt;BR /&gt;from&lt;BR /&gt;list a&lt;BR /&gt;left join&lt;BR /&gt;rfcorex b&amp;lt;&amp;lt;&amp;lt;-subset prior to query processing&lt;BR /&gt;on&lt;BR /&gt;a.an= b.an&lt;BR /&gt;;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This returns 153 records (inner join behavior&amp;nbsp; - not correct)&lt;/P&gt;&lt;P&gt;proc sql;&lt;BR /&gt;create table checkit as&lt;BR /&gt;select&lt;BR /&gt;a.an,&lt;BR /&gt;b.an as cfa,&lt;BR /&gt;b.load_date,&lt;BR /&gt;b.amount,&lt;BR /&gt;b.data_source,&lt;BR /&gt;b.fa_code&lt;BR /&gt;from&lt;BR /&gt;list a&lt;BR /&gt;left join&lt;BR /&gt;rfcore b&amp;lt;&amp;lt;&amp;lt;-no subset prior to query processing&lt;BR /&gt;on&lt;BR /&gt;a.an= b.an&lt;/P&gt;&lt;P&gt;where b.data_source contains "XXXXX"&lt;/P&gt;</description>
      <pubDate>Wed, 16 Oct 2019 16:56:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Left-Join-not-Correct-When-using-Where-Clause/m-p/596978#M171980</guid>
      <dc:creator>Doug____</dc:creator>
      <dc:date>2019-10-16T16:56:03Z</dc:date>
    </item>
    <item>
      <title>Re: Left Join not Correct When using Where Clause</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Left-Join-not-Correct-When-using-Where-Clause/m-p/596986#M171984</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/10429"&gt;@Doug____&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;I have a left table with 153 unduplicated observations and a right table with 15000+ observations.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If I use a where clause on the left table it only returns 153 records. If I remove the where clause and subset the data prior to query processing it returns the correct result (201 records). Why does this happen? Should it not return the same number of records? It acts like an inner join if the where clause is present regardless of the join used.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;These questions are impossible to answer without having the exact data sets (and code to do the subsetting) that you are using.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Perhaps you could make a smaller example that illustrates the problem, say 10 to 20 records, and provide that to us following these instructions &lt;STRONG&gt;(do not skip this step):&lt;/STRONG&gt;&amp;nbsp;&lt;A href="https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-data-AKA-generate/ta-p/258712" target="_blank"&gt;https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-data-AKA-generate/ta-p/258712&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 16 Oct 2019 16:59:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Left-Join-not-Correct-When-using-Where-Clause/m-p/596986#M171984</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2019-10-16T16:59:54Z</dc:date>
    </item>
    <item>
      <title>Re: Left Join not Correct When using Where Clause</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Left-Join-not-Correct-When-using-Where-Clause/m-p/596988#M171985</link>
      <description>&lt;P&gt;WHERE filters on the way in.&lt;/P&gt;
&lt;P&gt;If you want to filter after the join use HAVING.&lt;/P&gt;</description>
      <pubDate>Wed, 16 Oct 2019 17:03:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Left-Join-not-Correct-When-using-Where-Clause/m-p/596988#M171985</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-10-16T17:03:18Z</dc:date>
    </item>
    <item>
      <title>Re: Left Join not Correct When using Where Clause</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Left-Join-not-Correct-When-using-Where-Clause/m-p/596989#M171986</link>
      <description>&lt;P&gt;The Where clause you have is applied after the join in the second case so ONLY includes any records where the clause is true.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Left join in the first case can create multiple records from match&amp;nbsp; AND all of the records from the A data set are included regardless of the value of any of the B variables, such as not present in the B set at all based on the ON criteria.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data work.one;
   input x y;
datalines;
1 1
2 2
3 3
;

data work.two;
   input x y;
datalines;
5  3
;

proc sql;
   create table work.merged as
   select a.x , b.y
   from work.one as a
       left join
       work.two as b
       on a.x=b.x
   ;
quit;
/* result 3 records, all from work.one*/
proc sql;
   create table work.merged2 as
   select a.x , b.y
   from work.one as a
       left join
       work.two as b
       on a.x=b.x
   where b.y=3         
   ;
quit;
/* result 0 records because none of x match and the where would only
   output matches where the work.two y value is 3*/

&lt;/PRE&gt;</description>
      <pubDate>Wed, 16 Oct 2019 17:03:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Left-Join-not-Correct-When-using-Where-Clause/m-p/596989#M171986</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2019-10-16T17:03:39Z</dc:date>
    </item>
    <item>
      <title>Re: Left Join not Correct When using Where Clause</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Left-Join-not-Correct-When-using-Where-Clause/m-p/599800#M173210</link>
      <description>&lt;P&gt;I took the where clause off and just did the join first then selected the records I wanted which seems to work.&lt;/P&gt;</description>
      <pubDate>Mon, 28 Oct 2019 14:08:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Left-Join-not-Correct-When-using-Where-Clause/m-p/599800#M173210</guid>
      <dc:creator>Doug____</dc:creator>
      <dc:date>2019-10-28T14:08:44Z</dc:date>
    </item>
  </channel>
</rss>

