<?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: hashing  - using multiple types of join using multiple tables in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/hashing-using-multiple-types-of-join-using-multiple-tables/m-p/646185#M193264</link>
    <description>&lt;P&gt;Yes,&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data A;
  do x = 1 to 10;
    a="A";
    output;
  end;
run;

data B;
  do x = 1 to 10 by 2;
    b="B";
    output;
  end;
run;

data C;
  do x = 1 to 5;
    c="C";
    output;
  end;
run;

/* (A inner join B) left join C */

data D;
  if 0 then set a b c;
  declare hash HB(dataset:"B");
    HB.defineKey("x");
    HB.defineData("b");
    HB.defineDone();
  declare hash HC(dataset:"C");
    HC.defineKey("x");
    HC.defineData("c");
    HC.defineDone();

  do until(end);
    call missing (x,a,b,c);
    set A end = end;
    
    HC.find();

    if HB.find()=0 then output;
  end;


  stop;
run;
proc print data =D;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;All the best&lt;/P&gt;
&lt;P&gt;Bart&lt;/P&gt;</description>
    <pubDate>Fri, 08 May 2020 12:39:40 GMT</pubDate>
    <dc:creator>yabwon</dc:creator>
    <dc:date>2020-05-08T12:39:40Z</dc:date>
    <item>
      <title>hashing  - using multiple types of join using multiple tables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/hashing-using-multiple-types-of-join-using-multiple-tables/m-p/646175#M193256</link>
      <description>&lt;P&gt;Hi All,&lt;/P&gt;
&lt;P&gt;Question ,&lt;/P&gt;
&lt;P&gt;Can I perform a combination of joins (&amp;nbsp; left joins and inner joins )&amp;nbsp; in hash&amp;nbsp; using multiple tables ?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 08 May 2020 11:53:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/hashing-using-multiple-types-of-join-using-multiple-tables/m-p/646175#M193256</guid>
      <dc:creator>dennis_oz</dc:creator>
      <dc:date>2020-05-08T11:53:58Z</dc:date>
    </item>
    <item>
      <title>Re: hashing  - using multiple types of join using multiple tables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/hashing-using-multiple-types-of-join-using-multiple-tables/m-p/646185#M193264</link>
      <description>&lt;P&gt;Yes,&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data A;
  do x = 1 to 10;
    a="A";
    output;
  end;
run;

data B;
  do x = 1 to 10 by 2;
    b="B";
    output;
  end;
run;

data C;
  do x = 1 to 5;
    c="C";
    output;
  end;
run;

/* (A inner join B) left join C */

data D;
  if 0 then set a b c;
  declare hash HB(dataset:"B");
    HB.defineKey("x");
    HB.defineData("b");
    HB.defineDone();
  declare hash HC(dataset:"C");
    HC.defineKey("x");
    HC.defineData("c");
    HC.defineDone();

  do until(end);
    call missing (x,a,b,c);
    set A end = end;
    
    HC.find();

    if HB.find()=0 then output;
  end;


  stop;
run;
proc print data =D;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;All the best&lt;/P&gt;
&lt;P&gt;Bart&lt;/P&gt;</description>
      <pubDate>Fri, 08 May 2020 12:39:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/hashing-using-multiple-types-of-join-using-multiple-tables/m-p/646185#M193264</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2020-05-08T12:39:40Z</dc:date>
    </item>
    <item>
      <title>Re: hashing  - using multiple types of join using multiple tables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/hashing-using-multiple-types-of-join-using-multiple-tables/m-p/646285#M193297</link>
      <description>&lt;P&gt;Be careful though.&amp;nbsp; They are not the same as a SQL inner join when the join criteria contain an &lt;EM&gt;N&lt;/EM&gt; to &lt;EM&gt;M&lt;/EM&gt; match which should produce&amp;nbsp;&lt;EM&gt;NxM&amp;nbsp;&lt;/EM&gt;rows.&amp;nbsp; The minimal (simplest) HASH declaration is for a unique keyed data set, so the hash join scenarios are for related data with 1:1 or N:1 mappings.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;A complete replication of joining would need to deal with multidata:'yes' and cross looping.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Example:&lt;/P&gt;
&lt;PRE&gt;data A;
  do key = 1 to 9;
    retain A_value 100;
    A_value + 1;
    do _n_ = 1 to ceil(key/3);
      A_value + 0.01;
      output;
    end;
    A_value = int(A_value);
  end;
run;

data B;
  do key = 1 to 10 by 2;
    B_value = 200 + key;
    output;
  end;
run;

data C;
  do key = 1 to 5;
    retain C_value 300;
    C_value + 1;
    C_value + 0.01; output;
    C_value + 0.01; output;
    C_value = int(C_value);
  end;
run;

proc transpose data=a out=a_print(drop=_name_) prefix=A_value; by key; run;
proc transpose data=b out=b_print(drop=_name_) prefix=B_value; by key; run;
proc transpose data=c out=c_print(drop=_name_) prefix=C_value; by key; run;

proc print noobs data=a_print;
proc print noobs data=b_print;
proc print noobs data=c_print;

proc sql;
  create table reference as
  select 
    a.key, a_value, b_value, c_value
  from 
    a 
  inner join 
    b 
  on 
    a.key=b.key
  left join 
    c
  on 
    c.key = a.key
  order by 
    a.key, a_value, b_value, c_value
  ;

/* (A inner join B) left join C */

/* expected row count:
 * 1:1x1x2 + 3:1x1x2 + 5:2x1x2 + 7:3x1 + 9:3x1 = 14
 */

data D;
  if 0 then set a b c;

  declare hash HB(dataset:"B", multidata:'yes', ordered:'A');
    HB.defineKey("key");
    HB.defineData("b_value");
    HB.defineDone();
  declare hash HC(dataset:"C", multidata:'yes', ordered: 'A');
    HC.defineKey("key");
    HC.defineData("c_value");
    HC.defineDone();

  do until(exhausted);
    set A end = exhausted;
    
    _rc_b = HB.find();
    if _rc_b ne 0 then continue;

    do _bix_ = 1 by 1 until (_bix_ &amp;gt;= 1000);  /* simulate SQL LOOPS= option */
      _rc_c = HC.find();

      if _rc_c ne 0 then do;
        C_value = .;
        output;
      end;
      else
      do _cix_ = 1 by 1 until (_cix_ &amp;gt;= 1000);
        output;
        HC.has_next(result: _rc_cnext);
        if _rc_cnext = 0 then leave;
        HC.find_next();
      end;

      HB.has_next(result: _rc_bnext);
      if _rc_bnext = 0 then leave;
      HB.find_next();
    end;
  end;

  drop _:;

  stop;
run;
&lt;/PRE&gt;</description>
      <pubDate>Fri, 08 May 2020 18:21:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/hashing-using-multiple-types-of-join-using-multiple-tables/m-p/646285#M193297</guid>
      <dc:creator>RichardDeVen</dc:creator>
      <dc:date>2020-05-08T18:21:35Z</dc:date>
    </item>
    <item>
      <title>Re: hashing  - using multiple types of join using multiple tables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/hashing-using-multiple-types-of-join-using-multiple-tables/m-p/646546#M193446</link>
      <description>Richard, &lt;BR /&gt;&lt;BR /&gt;100% agree, my example was just "simple 1-to-1 case", with more advanced cases user always have to look out. Thanks for improving my version! &lt;BR /&gt;&lt;BR /&gt;Bart</description>
      <pubDate>Sun, 10 May 2020 18:27:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/hashing-using-multiple-types-of-join-using-multiple-tables/m-p/646546#M193446</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2020-05-10T18:27:22Z</dc:date>
    </item>
  </channel>
</rss>

