<?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 in PROC SQL Not Working as Expected in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Left-Join-in-PROC-SQL-Not-Working-as-Expected/m-p/878889#M347249</link>
    <description>&lt;P&gt;Hard to tell but if MONYEAR is a character column and values aren't left justified then that might explain why some aren't matching. Try this to see if this could be happening:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
		create table overlap_all as
		select distinct a.*,
		b.patcounty as county
	from overlap_long a
		left join opioids_loc b
			on a.patientid = b.patientid
			and left(a.monyear) = left(b.monyear);
	quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 02 Jun 2023 04:34:21 GMT</pubDate>
    <dc:creator>SASKiwi</dc:creator>
    <dc:date>2023-06-02T04:34:21Z</dc:date>
    <item>
      <title>Left Join in PROC SQL Not Working as Expected</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Left-Join-in-PROC-SQL-Not-Working-as-Expected/m-p/878885#M347245</link>
      <description>&lt;P&gt;I'm trying to join two databases. Some rows in the left database have corresponding rows in the right database, but the output database doesn't show them. (Note: I've tried to reproduce this error with a reproducible example, but can't).&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here's a few rows of the left database:&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="left_db.png" style="width: 371px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/84636i7901E116209F0C57/image-size/large?v=v2&amp;amp;px=999" role="button" title="left_db.png" alt="left_db.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;Here are some rows from the right table:&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="right_db.png" style="width: 459px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/84637iCCEC1BD4E9CBC58B/image-size/large?v=v2&amp;amp;px=999" role="button" title="right_db.png" alt="right_db.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;Here's the code I'm using to join them:&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;	/*Add County to Overlap Table*/
	proc sql;
		create table overlap_all as
		select distinct a.*,
		b.patcounty as county
	from overlap_long a
		left join opioids_loc b
			on a.patientid = b.patientid
			and a.monyear = b.monyear;
	quit;&lt;/PRE&gt;&lt;P&gt;And here are some rows from the output table:&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="join_db.png" style="width: 446px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/84638i408C93229E64C9F0/image-size/large?v=v2&amp;amp;px=999" role="button" title="join_db.png" alt="join_db.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;Some of the values aren't showing up. For example, Row 10 in the output table (corresponding to "monyear" = 7-2021, "patientid" = 5472) should read "HUMBOLDT", but doesn't.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Does anybody have any idea of what's going on? The weird part is that some rows that should show up, but others don't.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks,&lt;/P&gt;&lt;P&gt;David&lt;/P&gt;</description>
      <pubDate>Fri, 02 Jun 2023 03:58:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Left-Join-in-PROC-SQL-Not-Working-as-Expected/m-p/878885#M347245</guid>
      <dc:creator>dbcrow</dc:creator>
      <dc:date>2023-06-02T03:58:39Z</dc:date>
    </item>
    <item>
      <title>Re: Left Join in PROC SQL Not Working as Expected</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Left-Join-in-PROC-SQL-Not-Working-as-Expected/m-p/878888#M347248</link>
      <description>&lt;P&gt;It is very hard to tell from photographs what values are in your datasets.&amp;nbsp; Are those little red icons that look kind of like the letter A supposed to indicate that the MONYEAR variables are CHARACTER variables?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Leading spaces are significant in comparing character values.&amp;nbsp; So '7-2011' and ' 7-2011' might look the same in a photograph but will not actually match.&amp;nbsp; Also if one of them has an&amp;nbsp; endash or emdash character instead of a hyphen then they will not match.&lt;/P&gt;</description>
      <pubDate>Fri, 02 Jun 2023 04:27:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Left-Join-in-PROC-SQL-Not-Working-as-Expected/m-p/878888#M347248</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-06-02T04:27:39Z</dc:date>
    </item>
    <item>
      <title>Re: Left Join in PROC SQL Not Working as Expected</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Left-Join-in-PROC-SQL-Not-Working-as-Expected/m-p/878889#M347249</link>
      <description>&lt;P&gt;Hard to tell but if MONYEAR is a character column and values aren't left justified then that might explain why some aren't matching. Try this to see if this could be happening:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
		create table overlap_all as
		select distinct a.*,
		b.patcounty as county
	from overlap_long a
		left join opioids_loc b
			on a.patientid = b.patientid
			and left(a.monyear) = left(b.monyear);
	quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 02 Jun 2023 04:34:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Left-Join-in-PROC-SQL-Not-Working-as-Expected/m-p/878889#M347249</guid>
      <dc:creator>SASKiwi</dc:creator>
      <dc:date>2023-06-02T04:34:21Z</dc:date>
    </item>
    <item>
      <title>Re: Left Join in PROC SQL Not Working as Expected</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Left-Join-in-PROC-SQL-Not-Working-as-Expected/m-p/878890#M347250</link>
      <description>&lt;P&gt;Yes, "monyear" is a character variable. The dashes are all n-dashes, but now that I look closely, only values "10-2021", "11-2021", and "12-2021" have he county values that should be there.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;There's clearly a mismatch--maybe leading spaces--between monyear in the two datasets.&amp;nbsp;Let my try trimming the leading values or formatting length of "monyear" in the left and right datasets so that they're the same.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any other suggestions?&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 02 Jun 2023 04:34:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Left-Join-in-PROC-SQL-Not-Working-as-Expected/m-p/878890#M347250</guid>
      <dc:creator>dbcrow</dc:creator>
      <dc:date>2023-06-02T04:34:47Z</dc:date>
    </item>
    <item>
      <title>Re: Left Join in PROC SQL Not Working as Expected</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Left-Join-in-PROC-SQL-Not-Working-as-Expected/m-p/878891#M347251</link>
      <description>&lt;P&gt;Worked like a charm. Thanks, yet again!, SASKiwi, and to Tom, for both of your help.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 02 Jun 2023 04:36:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Left-Join-in-PROC-SQL-Not-Working-as-Expected/m-p/878891#M347251</guid>
      <dc:creator>dbcrow</dc:creator>
      <dc:date>2023-06-02T04:36:52Z</dc:date>
    </item>
  </channel>
</rss>

