<?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 Hash and Merge in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Hash-and-Merge/m-p/805903#M317479</link>
    <description>&lt;P&gt;Hi ,&lt;/P&gt;
&lt;P&gt;&amp;nbsp; Am confused with how the Hash and Merge works for this below table . For me the output of the Merge is correct . but&amp;nbsp; Merge requires the data set to be sorted before the Merge to happen and its time consuming .Is there any option i can have the same output of Merge in hash join as well?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data product;
input ID $ File_Nm $ PRODUCT $;

datalines;
1001  File1 1234
1001  File1 4567
1001  File1 7890
1001  File1 0123
1001  File1 3456
1001  File1 6789
;

data YEAR2021_WW01;
input ID $ File_Nm $ Amount $;
 
datalines;
1001  File1 100
1001  File1 200
1001  File1 300
;
  
data want_hash;
 if 0 then set product ;
 
 
  declare hash p (dataset:"product");
  p.definekey("ID","File_Nm");
  p.definedata(all:'Y'); 
  p.definedone();
   do until(eof);
   set YEAR2021_WW01 end=eof;
 
if p.find() = 0 then output;
end;
stop;
run;&lt;BR /&gt;&lt;BR /&gt;/*Merge Join*/&lt;BR /&gt;DATA WORK.want_merge;&lt;BR /&gt;&lt;BR /&gt;MERGE WORK.product(IN=A) YEAR2021_WW01(IN=B);&lt;BR /&gt;BY ID File_Nm;&lt;BR /&gt;IF A;&lt;BR /&gt;RUN;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Mon, 04 Apr 2022 17:41:02 GMT</pubDate>
    <dc:creator>Saranya_Sub</dc:creator>
    <dc:date>2022-04-04T17:41:02Z</dc:date>
    <item>
      <title>Hash and Merge</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Hash-and-Merge/m-p/805903#M317479</link>
      <description>&lt;P&gt;Hi ,&lt;/P&gt;
&lt;P&gt;&amp;nbsp; Am confused with how the Hash and Merge works for this below table . For me the output of the Merge is correct . but&amp;nbsp; Merge requires the data set to be sorted before the Merge to happen and its time consuming .Is there any option i can have the same output of Merge in hash join as well?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data product;
input ID $ File_Nm $ PRODUCT $;

datalines;
1001  File1 1234
1001  File1 4567
1001  File1 7890
1001  File1 0123
1001  File1 3456
1001  File1 6789
;

data YEAR2021_WW01;
input ID $ File_Nm $ Amount $;
 
datalines;
1001  File1 100
1001  File1 200
1001  File1 300
;
  
data want_hash;
 if 0 then set product ;
 
 
  declare hash p (dataset:"product");
  p.definekey("ID","File_Nm");
  p.definedata(all:'Y'); 
  p.definedone();
   do until(eof);
   set YEAR2021_WW01 end=eof;
 
if p.find() = 0 then output;
end;
stop;
run;&lt;BR /&gt;&lt;BR /&gt;/*Merge Join*/&lt;BR /&gt;DATA WORK.want_merge;&lt;BR /&gt;&lt;BR /&gt;MERGE WORK.product(IN=A) YEAR2021_WW01(IN=B);&lt;BR /&gt;BY ID File_Nm;&lt;BR /&gt;IF A;&lt;BR /&gt;RUN;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 04 Apr 2022 17:41:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Hash-and-Merge/m-p/805903#M317479</guid>
      <dc:creator>Saranya_Sub</dc:creator>
      <dc:date>2022-04-04T17:41:02Z</dc:date>
    </item>
    <item>
      <title>Re: Hash and Merge</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Hash-and-Merge/m-p/805909#M317483</link>
      <description>&lt;P&gt;Seems like you switch the two data sets around.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Try this&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want_hash;
   if 0 then set YEAR2021_WW01 ;
   declare hash p (dataset : "YEAR2021_WW01");
   p.definekey("ID", "File_Nm");
   p.definedata(all:'Y'); 
   p.definedone();

   do until(eof);
      set product end=eof;
      if p.find() = 0 then output;
   end;
   stop;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 04 Apr 2022 18:35:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Hash-and-Merge/m-p/805909#M317483</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2022-04-04T18:35:11Z</dc:date>
    </item>
    <item>
      <title>Re: Hash and Merge</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Hash-and-Merge/m-p/805910#M317484</link>
      <description>&lt;P&gt;What is the question?&lt;/P&gt;
&lt;P&gt;Are you asking if you can get sorted results from the HASH method without sorting the&amp;nbsp;YEAR2021_WW01 dataset?&lt;/P&gt;
&lt;P&gt;If so then the answer is no.&lt;/P&gt;</description>
      <pubDate>Mon, 04 Apr 2022 18:38:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Hash-and-Merge/m-p/805910#M317484</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-04-04T18:38:36Z</dc:date>
    </item>
    <item>
      <title>Re: Hash and Merge</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Hash-and-Merge/m-p/805958#M317504</link>
      <description>&lt;P&gt;&lt;EM&gt;&amp;gt;&amp;nbsp;&amp;nbsp;Merge requires the data set to be sorted before the Merge to happen and its time consuming .&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But then the merge takes no time at all. And the output is sorted, ready for the next merge (be sure to set the &lt;EM&gt;sorted&lt;/EM&gt; flag as SAS is &lt;A href="https://communities.sas.com/t5/SASware-Ballot-Ideas/create-a-VALIDATESORT-data-set-option-to-validate-the-sort-order/idi-p/288038" target="_self"&gt;too lazy to do that&lt;/A&gt;).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Bottom line: If you want a sorted output, you need to sort at some point.&lt;/P&gt;
&lt;P&gt;You can use the hash table for this if you want, but it is unlikely that the hash table is going to be the silver bullet you seem to think it is.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 04 Apr 2022 22:41:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Hash-and-Merge/m-p/805958#M317504</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2022-04-04T22:41:26Z</dc:date>
    </item>
    <item>
      <title>Re: Hash and Merge</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Hash-and-Merge/m-p/805970#M317510</link>
      <description>&lt;P&gt;The reason you are not successful in using hash object to replicate the merge is primarily due to the fact that MERGE is a "match_merge" process.&amp;nbsp; &amp;nbsp;When you have a many-to-many match as you do in your example, then merge combines the first match from each dataset, then the second match from each dataset, etc.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;When one of the datasets is shorter (as year2021_ww01 is here), then the last observation of that shorter sequence is matched with every "excess" obs in the longer sequence.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;BTW, your merge suggests that the hash object should be for year2021_ww01, not product.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But even then your code would fail, because the hash object defaults to storing one dataitem (i.e. one "row") per key combination, so you don't even keep the entire sequence of obs from year2021_ww01 for any repeated ID/FILE_NM.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So you need to&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;accommodate more than one obs per key combination&amp;nbsp; - use the &lt;EM&gt;&lt;STRONG&gt;multidata:"Y"&lt;/STRONG&gt;&lt;/EM&gt; option.&lt;/LI&gt;
&lt;LI&gt;Match the first obs of the PRODUCT dataset with the first matching dataitem in the hash object, match the second with the second, etc., This is done by usnig H.FIND() method to find the first, then delete that dataitem (&lt;EM&gt;&lt;STRONG&gt;h.removeddup&lt;/STRONG&gt;&lt;/EM&gt;), so that the next dataitem will become the first to satisfy the next h.find().&lt;/LI&gt;
&lt;LI&gt;Carry forth the last matching dataitem in the hash object to match any "extra" obs in PRODUCT.&amp;nbsp; So don't &lt;EM&gt;&lt;STRONG&gt;h.removedup&lt;/STRONG&gt; &lt;/EM&gt;the last dataitem for the duplicate key&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 (drop=_:);
  set product year2021_ww01 (obs=0);
  if _n_=1 then do;
    declare hash h (dataset:'year2021_ww01',multidata:'Y');
      h.definekey('id','file_nm');
      h.definedata(all:'Y');
      h.definedone();
  end;
  if h.find()=0 then do;
    h.has_next(result:_duplicate_available);
    if _duplicate_available then h.removedup();
  end;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;A couple of NOTES:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;The above does not need either dataset to be sorted.&amp;nbsp; Which, as far as I can tell, is the only real advantage of using hash vs MERGE in this particular application.&lt;BR /&gt;&lt;BR /&gt;&lt;/LI&gt;
&lt;LI&gt;If there were key combinations in YEAR2021_WW01 that have a longer series than in PRODUCTS, then the above will NOT replicate the merge statement - because the merge statement would repeat the last matching obs in PRODUCTS to meet the longer series in YEAR2021_WW01, while this program would ignore extra obs in the YEAR2021_WW01 object.&amp;nbsp; That would take a bit more code.&lt;BR /&gt;&lt;BR /&gt;&lt;/LI&gt;
&lt;/OL&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>Tue, 05 Apr 2022 02:27:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Hash-and-Merge/m-p/805970#M317510</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2022-04-05T02:27:18Z</dc:date>
    </item>
  </channel>
</rss>

