<?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: Doing Left Join using  Hash Object. in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Doing-Left-Join-using-Hash-Object/m-p/908774#M358571</link>
    <description>&lt;P&gt;Given your code I'm making the following assumptions of what you have and want&lt;/P&gt;
&lt;P&gt;1. Your left table where you want to keep all rows is the one in the set statement -&amp;nbsp;Tviot_1&lt;/P&gt;
&lt;P&gt;2. The relationship of your left table to all hash tables is one to zero or one - given you didn't use hash option multidata:'y' that's what it must be&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You don't need to call hash methods as part of an expression.&lt;/P&gt;
&lt;P&gt;If you define the variables in the hash to the PDV via IF 0 then set &amp;lt;lookup tables&amp;gt;; then you must set these variables to missing at the beginning of the data step as else the value from the last successful lookup will get retained.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Below amended code will return a left join.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data tviot_2;
  if _n_ = 1 then
    do;
      if 0 then set tviot_1 tom_tkufat_bituach_taarif tashlum_24 schum_tvia_magash;
      declare hash ttb(dataset: "tom_tkufat_bituach_taarif");
      ttb.definekey("ms_pol");
      ttb.definedata("tr_hatchlat_bituach","tr_produktzia","tr_tom_bituach");
      ttb.definedone();

      declare hash tshl24(dataset: "tashlum_24");
      tshl24.definekey("ms_tvia");
      tshl24.definedata("mone_tashlumim","ms_tashlum_24","trd_tashlum_24_kavua");
      tshl24.definedone();

      declare hash mgsh(dataset: "schum_tvia_magash");
      mgsh.definekey("ms_tvia");
      mgsh.definedata("schum_tvia_magash","tkufat_bituach");
      mgsh.definedone();
    end;
  call missing(of _all_);

  set tviot_1;

  _rc=ttb.find();
  _rc=tshl24.find();
  _rc=mgsh.find();

  /* if you want an IF condition that's always true: Just add 1 to the expression (1: True, 0:False*/
  /* if 1 or ttb.find() or tshl24.find() or mgsh.find(); */

  drop _rc;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 19 Dec 2023 09:51:54 GMT</pubDate>
    <dc:creator>Patrick</dc:creator>
    <dc:date>2023-12-19T09:51:54Z</dc:date>
    <item>
      <title>Doing Left Join using  Hash Object.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Doing-Left-Join-using-Hash-Object/m-p/908773#M358570</link>
      <description>&lt;P&gt;Hi!&lt;/P&gt;
&lt;P&gt;I recently discovered the use of Hash Object. This is fast and easy to use instrument.&lt;/P&gt;
&lt;P&gt;But I'm straggling to find out how to Left Join two tables.&lt;/P&gt;
&lt;P&gt;Previous topics provide solution to the specific problems, but doesn't provide clear explanation what and how they doing this.&lt;/P&gt;
&lt;P&gt;Here is my attempt to make left join but it doesn't work well - there are values in places that supposed to have NULL.&lt;/P&gt;
&lt;P&gt;Please, help me to modify the code to make the correct Left join with missing values.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data Tviot_2;
If 0 Then Set Tviot_1
			  Tom_Tkufat_Bituach_Taarif
			  Tashlum_24
			  Schum_Tvia_Magash;

If _N_ = 1 Then Do;
	Declare Hash TTB(Dataset: "Tom_Tkufat_Bituach_Taarif");
	TTB.DefineKey("MS_POL");
	TTB.DefineData("TR_HATCHLAT_BITUACH","TR_PRODUKTZIA","TR_TOM_BITUACH");
	TTB.DefineDone();

	Declare Hash TSHL24(Dataset: "Tashlum_24");
	TSHL24.DefineKey("MS_TVIA");
	TSHL24.DefineData("MONE_TASHLUMIM","MS_TASHLUM_24","TRD_TASHLUM_24_KAVUA");
	TSHL24.DefineDone();

	Declare Hash MGSH(Dataset: "Schum_Tvia_Magash");
	MGSH.DefineKey("MS_TVIA");
	MGSH.DefineData("Schum_Tvia_Magash","TKUFAT_BITUACH");
	MGSH.DefineDone();
End;

Set Tviot_1;
	If TTB.Find()    = 0
		OR TSHL24.Find() = 0
		OR MGSH.Find()   = 0
	Then Output;
	Else Output;

Run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 19 Dec 2023 09:08:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Doing-Left-Join-using-Hash-Object/m-p/908773#M358570</guid>
      <dc:creator>IgorR</dc:creator>
      <dc:date>2023-12-19T09:08:34Z</dc:date>
    </item>
    <item>
      <title>Re: Doing Left Join using  Hash Object.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Doing-Left-Join-using-Hash-Object/m-p/908774#M358571</link>
      <description>&lt;P&gt;Given your code I'm making the following assumptions of what you have and want&lt;/P&gt;
&lt;P&gt;1. Your left table where you want to keep all rows is the one in the set statement -&amp;nbsp;Tviot_1&lt;/P&gt;
&lt;P&gt;2. The relationship of your left table to all hash tables is one to zero or one - given you didn't use hash option multidata:'y' that's what it must be&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You don't need to call hash methods as part of an expression.&lt;/P&gt;
&lt;P&gt;If you define the variables in the hash to the PDV via IF 0 then set &amp;lt;lookup tables&amp;gt;; then you must set these variables to missing at the beginning of the data step as else the value from the last successful lookup will get retained.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Below amended code will return a left join.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data tviot_2;
  if _n_ = 1 then
    do;
      if 0 then set tviot_1 tom_tkufat_bituach_taarif tashlum_24 schum_tvia_magash;
      declare hash ttb(dataset: "tom_tkufat_bituach_taarif");
      ttb.definekey("ms_pol");
      ttb.definedata("tr_hatchlat_bituach","tr_produktzia","tr_tom_bituach");
      ttb.definedone();

      declare hash tshl24(dataset: "tashlum_24");
      tshl24.definekey("ms_tvia");
      tshl24.definedata("mone_tashlumim","ms_tashlum_24","trd_tashlum_24_kavua");
      tshl24.definedone();

      declare hash mgsh(dataset: "schum_tvia_magash");
      mgsh.definekey("ms_tvia");
      mgsh.definedata("schum_tvia_magash","tkufat_bituach");
      mgsh.definedone();
    end;
  call missing(of _all_);

  set tviot_1;

  _rc=ttb.find();
  _rc=tshl24.find();
  _rc=mgsh.find();

  /* if you want an IF condition that's always true: Just add 1 to the expression (1: True, 0:False*/
  /* if 1 or ttb.find() or tshl24.find() or mgsh.find(); */

  drop _rc;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 19 Dec 2023 09:51:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Doing-Left-Join-using-Hash-Object/m-p/908774#M358571</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2023-12-19T09:51:54Z</dc:date>
    </item>
    <item>
      <title>Re: Doing Left Join using  Hash Object.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Doing-Left-Join-using-Hash-Object/m-p/908776#M358573</link>
      <description>&lt;P&gt;The problem is that the variables fetched from the hash tables are retained.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can solve the problem by setting the data variables missing, e.g.:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data Tviot_2;
If 0 Then Set Tviot_1
			  Tom_Tkufat_Bituach_Taarif
			  Tashlum_24
			  Schum_Tvia_Magash;

If _N_ = 1 Then Do;
	Declare Hash TTB(Dataset: "Tom_Tkufat_Bituach_Taarif");
	TTB.DefineKey("MS_POL");
	TTB.DefineData("TR_HATCHLAT_BITUACH","TR_PRODUKTZIA","TR_TOM_BITUACH");
	TTB.DefineDone();

	Declare Hash TSHL24(Dataset: "Tashlum_24");
	TSHL24.DefineKey("MS_TVIA");
	TSHL24.DefineData("MONE_TASHLUMIM","MS_TASHLUM_24","TRD_TASHLUM_24_KAVUA");
	TSHL24.DefineDone();

	Declare Hash MGSH(Dataset: "Schum_Tvia_Magash");
	MGSH.DefineKey("MS_TVIA");
	MGSH.DefineData("Schum_Tvia_Magash","TKUFAT_BITUACH");
	MGSH.DefineDone();
End;

Set Tviot_1;
call missing(of TR_HATCHLAT_BITUACH--TKUFAT_BITUACH);
	If TTB.Find()    = 0
		OR TSHL24.Find() = 0
		OR MGSH.Find()   = 0
	Then Output;
	Else Output;

Run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;Edit note: added "of" in CALL MSÍSSING.&lt;/P&gt;</description>
      <pubDate>Tue, 19 Dec 2023 09:39:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Doing-Left-Join-using-Hash-Object/m-p/908776#M358573</guid>
      <dc:creator>s_lassen</dc:creator>
      <dc:date>2023-12-19T09:39:58Z</dc:date>
    </item>
    <item>
      <title>Re: Doing Left Join using  Hash Object.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Doing-Left-Join-using-Hash-Object/m-p/908829#M358579</link>
      <description>&lt;P&gt;Variables sourced from input datasets are always "retain"ed.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So you need to either clear them to missing before you read the "LEFT" table.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if 0 then set LEFT RIGHT1 RIGHT2 ...;
call missing(of _all_);
set left;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Or clear them to missing when the FIND() method fails.&amp;nbsp; Since FIND() returns a TRUE value when the value is NOT found the code would look like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if RIGHT1.find() then call missing(TR_HATCHLAT_BITUACH,TR_PRODUKTZIA,TR_TOM_BITUACH);
if RIGHT2.find() then call missing(....);
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;So the generalized pattern might look like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  if 0 then set left
    right1(keep=r1data1 r1data2)
    right2(keep=r1data2 r2data2)
  ;
  set left;
  if _n_=1 then do;
    declare hash right1(dataset:'right1');
    right1.definekey('key1');
    right1.definedata('r1data1','r1data2');
    right1.definedone();
    declare hash right2(dataset:'right2');
    right2.definekey('key2');
    right2.definedata('r2data1','r2data2');
    right2.definedone();
  end;
  if right1.find() then call missing(r1data1,r1data2);
  if right2.find() then call missing(r2data1,r2data2);
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 19 Dec 2023 15:25:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Doing-Left-Join-using-Hash-Object/m-p/908829#M358579</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-12-19T15:25:23Z</dc:date>
    </item>
  </channel>
</rss>

