<?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: HASH Left Join in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/HASH-Left-Join/m-p/478850#M123517</link>
    <description>I'm a little confused with the&lt;BR /&gt;multidata;'Y'&lt;BR /&gt;and the&lt;BR /&gt;right.find(key:y1)</description>
    <pubDate>Tue, 17 Jul 2018 21:44:00 GMT</pubDate>
    <dc:creator>BernyOsuna</dc:creator>
    <dc:date>2018-07-17T21:44:00Z</dc:date>
    <item>
      <title>HASH Left Join</title>
      <link>https://communities.sas.com/t5/SAS-Programming/HASH-Left-Join/m-p/363505#M86091</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I need help with joining two tables with hash. Both tables has one unique key and one other duplicate variable. Below are the matching variables&lt;/P&gt;&lt;P&gt;x1 - y1&lt;/P&gt;&lt;P&gt;x15 - y17&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;/* 15 variables */&lt;/P&gt;&lt;P&gt;Table a&amp;nbsp;&lt;/P&gt;&lt;P&gt;x1&lt;/P&gt;&lt;P&gt;.&lt;/P&gt;&lt;P&gt;.&lt;/P&gt;&lt;P&gt;x15&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;/* 17 variables */&lt;/P&gt;&lt;P&gt;Table b&lt;/P&gt;&lt;P&gt;y1&lt;/P&gt;&lt;P&gt;.&lt;/P&gt;&lt;P&gt;.&lt;/P&gt;&lt;P&gt;y17&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I tried with following code but I am getting all the variables from table b and only one from table a instead I want to display all the var from table a and all the var from table b&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;data left_join(drop=rc);

	declare Hash a (dataset: "o.a");
	rc=a.definekey('x1');
	rc=a.definedata();
	rc=a.definedone();

	do until(eof);
	 set o.b end=eof;
	 rc=a.find();
	 output;
	end;
	stop;
run;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 01 Jun 2017 16:35:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/HASH-Left-Join/m-p/363505#M86091</guid>
      <dc:creator>kpdoe</dc:creator>
      <dc:date>2017-06-01T16:35:15Z</dc:date>
    </item>
    <item>
      <title>Re: HASH Left Join</title>
      <link>https://communities.sas.com/t5/SAS-Programming/HASH-Left-Join/m-p/363509#M86095</link>
      <description>&lt;P&gt;I guess you meant that x15 should match with y17, as there are only fifteen variables in dataset a?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;That said, what is the relationship between the tables with regard to the key variables? Is it one-to-many, or many-to-many?&lt;/P&gt;</description>
      <pubDate>Thu, 01 Jun 2017 16:38:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/HASH-Left-Join/m-p/363509#M86095</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2017-06-01T16:38:21Z</dc:date>
    </item>
    <item>
      <title>Re: HASH Left Join</title>
      <link>https://communities.sas.com/t5/SAS-Programming/HASH-Left-Join/m-p/363512#M86096</link>
      <description>&lt;P&gt;No x15 doesnt have to match y17. x1 should mach y1. It is one to many relationship.&lt;/P&gt;</description>
      <pubDate>Thu, 01 Jun 2017 16:44:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/HASH-Left-Join/m-p/363512#M86096</guid>
      <dc:creator>kpdoe</dc:creator>
      <dc:date>2017-06-01T16:44:32Z</dc:date>
    </item>
    <item>
      <title>Re: HASH Left Join</title>
      <link>https://communities.sas.com/t5/SAS-Programming/HASH-Left-Join/m-p/363518#M86099</link>
      <description>&lt;P&gt;Check this paper &lt;A href=" http://support.sas.com/resources/papers/proceedings16/10200-2016.pdf" target="_self"&gt;Using the SAS® Hash Object with Duplicate Key Entries&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;There is section "2. One-to-Many, Many-to-Many Left Joins" might have the answer you are looking for.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Hope this helps,&lt;/P&gt;
&lt;P&gt;Ahmed&lt;/P&gt;</description>
      <pubDate>Thu, 01 Jun 2017 17:13:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/HASH-Left-Join/m-p/363518#M86099</guid>
      <dc:creator>AhmedAl_Attar</dc:creator>
      <dc:date>2017-06-01T17:13:45Z</dc:date>
    </item>
    <item>
      <title>Re: HASH Left Join</title>
      <link>https://communities.sas.com/t5/SAS-Programming/HASH-Left-Join/m-p/363522#M86100</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The problem with&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data left_join(drop=rc);

	declare Hash a (dataset: "o.a");
	rc=a.definekey('x1');
	rc=a.definedata();
	rc=a.definedone();

	do until(eof);
	 set o.b end=eof;
	 rc=a.find();
	 output;
	end;
	stop;
run;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;Your a.definedata probably should be a.definedata(all:'Y').&amp;nbsp; That's probably why you are not getting all the A vars&lt;/LI&gt;
&lt;LI&gt;You're apparently simulating B left join A on b.y1=a.x1.&amp;nbsp; Your a.find() default to X1 as the lookup key, but you want Y1 to be the lookup key:&lt;BR /&gt;&amp;nbsp;&amp;nbsp; try a.find(key:y1).&lt;/LI&gt;
&lt;LI&gt;In your loop, when a B is NOT found in A, all the vars in A should be set to missing, but you will not get that.&amp;nbsp; Instead you will get all the A vars in the most recent successful FIND method.&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  if _n_=1 then do;
    if 0 then set a;
    declare hash right (dataset:'a');
       right.definekey('x1');
       right.definedata(all:'Y');
       right.definedone();
       right.add();   /* Add a dummy record with all missing vals*/
  end;

  set b;  /* left */ 

  rc=right.find(key:.);&lt;BR /&gt;  rc=right.find(key:y1);
 run;

&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Notes:&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;I added a row of missing value to the hash object based on dataset A.&lt;/LI&gt;
&lt;LI&gt;I use that row (rc=right.find(key:.) to set all A vars to missing prior to searching for A.X1=B.Y1.&amp;nbsp; If that search is not successful it won't inadvertantly inherit A vars from a prior successfull search.&lt;/LI&gt;
&lt;LI&gt;Editted addition: This assumes that X1 is unique in dataset A.&lt;/LI&gt;
&lt;/OL&gt;</description>
      <pubDate>Thu, 01 Jun 2017 17:30:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/HASH-Left-Join/m-p/363522#M86100</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2017-06-01T17:30:26Z</dc:date>
    </item>
    <item>
      <title>Re: HASH Left Join</title>
      <link>https://communities.sas.com/t5/SAS-Programming/HASH-Left-Join/m-p/363548#M86106</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/146786"&gt;@kpdoe&lt;/a&gt; wrote:&lt;BR /&gt;
&lt;P&gt;No x15 doesnt have to match y17. x1 should mach y1. It is one to many relationship.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Then my preferred method is&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=a;
by x1;
run;

proc sort data=b;
by y1;
run;

data want;
merge
  a (in=a)
  b (in=b rename=(y1=x1))
;
by x1;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You can add a subsetting if that selects if you want an inner, left or right join.&lt;/P&gt;</description>
      <pubDate>Thu, 01 Jun 2017 18:04:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/HASH-Left-Join/m-p/363548#M86106</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2017-06-01T18:04:21Z</dc:date>
    </item>
    <item>
      <title>Re: HASH Left Join</title>
      <link>https://communities.sas.com/t5/SAS-Programming/HASH-Left-Join/m-p/363551#M86107</link>
      <description>&lt;P&gt;Ah yes,&amp;nbsp; the OP said it's many-to-one but my program assumed unique X1 values in A.&amp;nbsp; Here's the code for duplicated X1 values:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  if _n_=1 then do;
    if 0 then set a;
    declare hash right (dataset:'a',multidata:'Y');
       right.definekey('x1');
       right.definedata(all:'Y');
       right.definedone();
       right.add();   /* Add a dummy record with all missing vals*/
  end;

  set b;  /* left */ 

  rc=right.find(key:y1); &lt;BR /&gt;  if rc^=0 then rc=right.find(key:.);
  do while (rc=0);
    output;
    rc=right.find_next();
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Edited at 6/1/2017 14:58 UTC-5:00&lt;/P&gt;</description>
      <pubDate>Thu, 01 Jun 2017 18:58:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/HASH-Left-Join/m-p/363551#M86107</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2017-06-01T18:58:48Z</dc:date>
    </item>
    <item>
      <title>Re: HASH Left Join</title>
      <link>https://communities.sas.com/t5/SAS-Programming/HASH-Left-Join/m-p/363554#M86108</link>
      <description>&lt;P&gt;Just one caveat: this can do left, right or inner, but not many-to-many.&lt;/P&gt;</description>
      <pubDate>Thu, 01 Jun 2017 18:08:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/HASH-Left-Join/m-p/363554#M86108</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2017-06-01T18:08:36Z</dc:date>
    </item>
    <item>
      <title>Re: HASH Left Join</title>
      <link>https://communities.sas.com/t5/SAS-Programming/HASH-Left-Join/m-p/363574#M86110</link>
      <description>&lt;P&gt;Thank you very much. It worked!&lt;/P&gt;</description>
      <pubDate>Thu, 01 Jun 2017 19:26:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/HASH-Left-Join/m-p/363574#M86110</guid>
      <dc:creator>kpdoe</dc:creator>
      <dc:date>2017-06-01T19:26:34Z</dc:date>
    </item>
    <item>
      <title>Re: HASH Left Join</title>
      <link>https://communities.sas.com/t5/SAS-Programming/HASH-Left-Join/m-p/478850#M123517</link>
      <description>I'm a little confused with the&lt;BR /&gt;multidata;'Y'&lt;BR /&gt;and the&lt;BR /&gt;right.find(key:y1)</description>
      <pubDate>Tue, 17 Jul 2018 21:44:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/HASH-Left-Join/m-p/478850#M123517</guid>
      <dc:creator>BernyOsuna</dc:creator>
      <dc:date>2018-07-17T21:44:00Z</dc:date>
    </item>
    <item>
      <title>Re: HASH Left Join</title>
      <link>https://communities.sas.com/t5/SAS-Programming/HASH-Left-Join/m-p/478888#M123536</link>
      <description>&lt;P&gt;Mark,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;You're right. There's no need to make assumptions about data and code differently for many-to-one and many-to-many. MULTIDATA:"Y" covers all bases. If there're many, the loop gets all of them; and if there's one, the very first FIND_NEXT call fails, which is what the doctor ordered.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Best&lt;/P&gt;&lt;P&gt;Paul&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 18 Jul 2018 02:21:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/HASH-Left-Join/m-p/478888#M123536</guid>
      <dc:creator>hashman</dc:creator>
      <dc:date>2018-07-18T02:21:39Z</dc:date>
    </item>
  </channel>
</rss>

