<?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 JOIN LEFT too many rows in SAS Studio</title>
    <link>https://communities.sas.com/t5/SAS-Studio/JOIN-LEFT-too-many-rows/m-p/501755#M6337</link>
    <description>&lt;P&gt;Hi SAS Community,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a data set&amp;nbsp;("WORK.PTMatchUS") and would like to extend it (in a new table "WORK.Merged") with two columns (NewCOMNAM and NewCUSIP) from&amp;nbsp;a different data set (CRSP.STOCKNAMES)&amp;nbsp;as soon as&amp;nbsp;there is a match in the variable&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;STRONG&gt;CUSIP&lt;/STRONG&gt;.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE class=" language-sas"&gt;&lt;CODE class="  language-sas"&gt;&lt;SPAN class="token procnames"&gt;PROC&lt;/SPAN&gt; &lt;SPAN class="token procnames"&gt;SQL&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
	CREATE &lt;SPAN class="token statement"&gt;TABLE&lt;/SPAN&gt; WORK&lt;SPAN class="token punctuation"&gt;.&lt;/SPAN&gt;Merged AS &lt;SPAN class="token statement"&gt;SELECT&lt;/SPAN&gt; Old&lt;SPAN class="token punctuation"&gt;.&lt;/SPAN&gt;&lt;SPAN class="token operator"&gt;*&lt;/SPAN&gt; &lt;SPAN class="token punctuation"&gt;,&lt;/SPAN&gt; New&lt;SPAN class="token punctuation"&gt;.&lt;/SPAN&gt;COMNAM AS NewCOMNAM &lt;SPAN class="token punctuation"&gt;,&lt;/SPAN&gt; New&lt;SPAN class="token punctuation"&gt;.&lt;/SPAN&gt;CUSIP AS NewCUSIP 
	&lt;SPAN class="token keyword"&gt;FROM&lt;/SPAN&gt; WORK&lt;SPAN class="token punctuation"&gt;.&lt;/SPAN&gt;PTMatchUS as Old 
	&lt;SPAN class="token function"&gt;Left&lt;/SPAN&gt; JOIN CRSP&lt;SPAN class="token punctuation"&gt;.&lt;/SPAN&gt;STOCKNAMES as New ON Old&lt;SPAN class="token punctuation"&gt;.&lt;/SPAN&gt;CUSIP&lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt;New&lt;SPAN class="token punctuation"&gt;.&lt;/SPAN&gt;CUSIP&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
&lt;SPAN class="token procnames"&gt;QUIT&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;STRONG&gt;Example&lt;/STRONG&gt;:&amp;nbsp;&lt;/P&gt;&lt;P&gt;Data set A:&lt;/P&gt;&lt;P&gt;101 // Daniel&lt;/P&gt;&lt;P&gt;211 // Steve&lt;/P&gt;&lt;P&gt;31 // Joe&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;Data set B:&lt;/P&gt;&lt;P&gt;111 // 175&lt;/P&gt;&lt;P&gt;211 // 180&lt;/P&gt;&lt;P&gt;51 // 182&lt;/P&gt;&lt;P&gt;23&amp;nbsp;// 165&lt;/P&gt;&lt;P&gt;31&amp;nbsp;// 177&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;New table should be then:&lt;/P&gt;&lt;P&gt;101 // Daniel // &lt;STRONG&gt;N/A&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;211 // Steve // &lt;STRONG&gt;180&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;31 // Joe // &lt;STRONG&gt;177&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Problem: I get much more rows in the new created table than&amp;nbsp;it should...My new table should have the same amount of rows as the primary data set "Work.PTMatchUS" but just extended to the two new columns (NewCOMNAM and NewCUSIP) with the information from the other dataset (CRSP.STOCKNAMES)... in other words just like a basic VLOOKUP in Excel.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any ideas how to change my code in a way that it works?&lt;/P&gt;&lt;P&gt;Thanks in advance.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Jorge&lt;/P&gt;</description>
    <pubDate>Fri, 05 Oct 2018 06:29:32 GMT</pubDate>
    <dc:creator>jozuleta</dc:creator>
    <dc:date>2018-10-05T06:29:32Z</dc:date>
    <item>
      <title>JOIN LEFT too many rows</title>
      <link>https://communities.sas.com/t5/SAS-Studio/JOIN-LEFT-too-many-rows/m-p/501755#M6337</link>
      <description>&lt;P&gt;Hi SAS Community,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a data set&amp;nbsp;("WORK.PTMatchUS") and would like to extend it (in a new table "WORK.Merged") with two columns (NewCOMNAM and NewCUSIP) from&amp;nbsp;a different data set (CRSP.STOCKNAMES)&amp;nbsp;as soon as&amp;nbsp;there is a match in the variable&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;STRONG&gt;CUSIP&lt;/STRONG&gt;.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE class=" language-sas"&gt;&lt;CODE class="  language-sas"&gt;&lt;SPAN class="token procnames"&gt;PROC&lt;/SPAN&gt; &lt;SPAN class="token procnames"&gt;SQL&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
	CREATE &lt;SPAN class="token statement"&gt;TABLE&lt;/SPAN&gt; WORK&lt;SPAN class="token punctuation"&gt;.&lt;/SPAN&gt;Merged AS &lt;SPAN class="token statement"&gt;SELECT&lt;/SPAN&gt; Old&lt;SPAN class="token punctuation"&gt;.&lt;/SPAN&gt;&lt;SPAN class="token operator"&gt;*&lt;/SPAN&gt; &lt;SPAN class="token punctuation"&gt;,&lt;/SPAN&gt; New&lt;SPAN class="token punctuation"&gt;.&lt;/SPAN&gt;COMNAM AS NewCOMNAM &lt;SPAN class="token punctuation"&gt;,&lt;/SPAN&gt; New&lt;SPAN class="token punctuation"&gt;.&lt;/SPAN&gt;CUSIP AS NewCUSIP 
	&lt;SPAN class="token keyword"&gt;FROM&lt;/SPAN&gt; WORK&lt;SPAN class="token punctuation"&gt;.&lt;/SPAN&gt;PTMatchUS as Old 
	&lt;SPAN class="token function"&gt;Left&lt;/SPAN&gt; JOIN CRSP&lt;SPAN class="token punctuation"&gt;.&lt;/SPAN&gt;STOCKNAMES as New ON Old&lt;SPAN class="token punctuation"&gt;.&lt;/SPAN&gt;CUSIP&lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt;New&lt;SPAN class="token punctuation"&gt;.&lt;/SPAN&gt;CUSIP&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
&lt;SPAN class="token procnames"&gt;QUIT&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;STRONG&gt;Example&lt;/STRONG&gt;:&amp;nbsp;&lt;/P&gt;&lt;P&gt;Data set A:&lt;/P&gt;&lt;P&gt;101 // Daniel&lt;/P&gt;&lt;P&gt;211 // Steve&lt;/P&gt;&lt;P&gt;31 // Joe&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;Data set B:&lt;/P&gt;&lt;P&gt;111 // 175&lt;/P&gt;&lt;P&gt;211 // 180&lt;/P&gt;&lt;P&gt;51 // 182&lt;/P&gt;&lt;P&gt;23&amp;nbsp;// 165&lt;/P&gt;&lt;P&gt;31&amp;nbsp;// 177&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;New table should be then:&lt;/P&gt;&lt;P&gt;101 // Daniel // &lt;STRONG&gt;N/A&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;211 // Steve // &lt;STRONG&gt;180&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;31 // Joe // &lt;STRONG&gt;177&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Problem: I get much more rows in the new created table than&amp;nbsp;it should...My new table should have the same amount of rows as the primary data set "Work.PTMatchUS" but just extended to the two new columns (NewCOMNAM and NewCUSIP) with the information from the other dataset (CRSP.STOCKNAMES)... in other words just like a basic VLOOKUP in Excel.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any ideas how to change my code in a way that it works?&lt;/P&gt;&lt;P&gt;Thanks in advance.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Jorge&lt;/P&gt;</description>
      <pubDate>Fri, 05 Oct 2018 06:29:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Studio/JOIN-LEFT-too-many-rows/m-p/501755#M6337</guid>
      <dc:creator>jozuleta</dc:creator>
      <dc:date>2018-10-05T06:29:32Z</dc:date>
    </item>
    <item>
      <title>Re: JOIN LEFT too many rows</title>
      <link>https://communities.sas.com/t5/SAS-Studio/JOIN-LEFT-too-many-rows/m-p/501860#M6338</link>
      <description>&lt;P&gt;Hi,&amp;nbsp;&lt;BR /&gt;I ran your code, with some minor changes and it appears to work for the example data given. See last PROC SQL step in code below.&lt;BR /&gt;I also wrote my own SQL query (first SQL step below) before checking yours. Both are creating 3 rows in the output datasets.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I suggest you&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;Create some&amp;nbsp;test code and run that (e.g. code below).&amp;nbsp;&lt;/LI&gt;
&lt;LI&gt;Create 2 smaller test datasets from your PTMatchUS &amp;amp; Stocknames&amp;nbsp;and run the code against that and check the results&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;I suspect there is something about the 2 datasets that impacts the results, that you are unaware of&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data PTMatchUS ;
	infile cards ;
	input cusip $ name $ ;
cards ;
101 Daniel
211 Steve
31 Joe
;
data stocknames ;
	infile cards ;
	input cusip $  newcusip $ ;
cards ;
111 175
211 180
51 182
23 165
31 177
;

proc sql ;
	create table 
		merged1 as
	select 
		old.*,
		new.newcusip as newcusip 
	from
		PTMatchUS as old
	left join 
		stocknames as new
	on 
		old.cusip=new.cusip 
	;
quit ;

PROC SQL;
	CREATE TABLE WORK.Merged AS SELECT Old.* , New.newcusip AS Newcusip 
	FROM WORK.PTMatchUS as Old 
	Left JOIN STOCKNAMES as New ON Old.CUSIP=New.CUSIP;
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>Fri, 05 Oct 2018 12:47:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Studio/JOIN-LEFT-too-many-rows/m-p/501860#M6338</guid>
      <dc:creator>AMSAS</dc:creator>
      <dc:date>2018-10-05T12:47:18Z</dc:date>
    </item>
    <item>
      <title>Re: JOIN LEFT too many rows</title>
      <link>https://communities.sas.com/t5/SAS-Studio/JOIN-LEFT-too-many-rows/m-p/502259#M6353</link>
      <description>&lt;P&gt;Hi AMSAS,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;thanks for your help. Apparently my code was right but my description was not clearly enough. My Data set contains several rows with also the same ID (CUSIP) and this is why I got more rows in the resulting table. I could fix the problem by deleting duplicates in the data. Thanks anyway for you help!&lt;/P&gt;</description>
      <pubDate>Sun, 07 Oct 2018 15:37:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Studio/JOIN-LEFT-too-many-rows/m-p/502259#M6353</guid>
      <dc:creator>jozuleta</dc:creator>
      <dc:date>2018-10-07T15:37:47Z</dc:date>
    </item>
  </channel>
</rss>

