<?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 full and then a left join with the same table in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/HASH-full-and-then-a-left-join-with-the-same-table/m-p/699716#M214050</link>
    <description>&lt;P&gt;The Set Statement read TableA sequentially, so the order from TableA will be intact in the output data set &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
    <pubDate>Wed, 18 Nov 2020 09:26:43 GMT</pubDate>
    <dc:creator>PeterClemmensen</dc:creator>
    <dc:date>2020-11-18T09:26:43Z</dc:date>
    <item>
      <title>HASH full and then a left join with the same table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/HASH-full-and-then-a-left-join-with-the-same-table/m-p/699464#M213970</link>
      <description>&lt;P&gt;&lt;BR /&gt;DATA TABLEA;&lt;BR /&gt;input Name $1. ID $5. smoker_code Year;&lt;BR /&gt;Cards;&lt;BR /&gt;A ID1 0 2012&lt;BR /&gt;B ID2 1 2010&lt;BR /&gt;;&lt;BR /&gt;Run;&lt;/P&gt;&lt;P&gt;DATA TABLEB;&lt;BR /&gt;input Name $1. ID $5. want Year;&lt;BR /&gt;Cards;&lt;BR /&gt;A ID1 0.5 2010&lt;BR /&gt;A ID1 0.6 2011&lt;BR /&gt;A ID1 0.7 2012&lt;BR /&gt;B ID2 0.8 2008&lt;BR /&gt;B ID2 0.8 2009&lt;BR /&gt;B ID2 0.8 2010&lt;BR /&gt;B ID2 0.9 2011&lt;BR /&gt;;&lt;BR /&gt;Run;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;Proc sql;&lt;BR /&gt;Create table ABC_SQL_full as&lt;BR /&gt;select A.*&lt;BR /&gt;,B.want as want1&lt;BR /&gt;,B.year as Year_past&lt;BR /&gt;,C.want as Want2&lt;/P&gt;&lt;P&gt;from (select *,MONOTONIC() as ORD from TABLEA) as a&lt;/P&gt;&lt;P&gt;full join TABLEB as b&lt;BR /&gt;on A.ID=B.ID&lt;BR /&gt;and A.Name=B.name&lt;/P&gt;&lt;P&gt;left join TABLEB as c&lt;BR /&gt;on A.ID=c.ID&lt;BR /&gt;and a.Name=c.name&lt;BR /&gt;and b.year=c.year&lt;/P&gt;&lt;P&gt;where b.year le a.year&lt;BR /&gt;order by ORD,year_past;&lt;BR /&gt;quit;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;/* how to perform the same SQL join results with HASH method&lt;/P&gt;&lt;P&gt;1.HASH full join with tableB&lt;BR /&gt;2.and HASH left join with same tableB with condition b.year=c.year in the defined key&amp;nbsp;&lt;BR /&gt;3.and having the same where and order condition*/&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;/* here it's an left join in hash that i can use for the second join ... */&lt;BR /&gt;data ABC_HASH_full;&lt;BR /&gt;if 0 then set TABLEB ;&lt;/P&gt;&lt;P&gt;set TABLEA ;&lt;/P&gt;&lt;P&gt;if _n_=1 then&lt;BR /&gt;do;&lt;BR /&gt;declare hash hhh(dataset: "TABLEB", multidata:'y');&lt;BR /&gt;hhh.DefineKey('ID','Name');&lt;BR /&gt;hhh.DefineData('want');&lt;BR /&gt;hhh.DefineDone();&lt;BR /&gt;end;&lt;/P&gt;&lt;P&gt;if hhh.find(key:ID,Key:Name)=0 then do;&lt;BR /&gt;output ABC_SQL_full;&lt;BR /&gt;end;&lt;BR /&gt;run;&lt;/P&gt;</description>
      <pubDate>Tue, 17 Nov 2020 12:30:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/HASH-full-and-then-a-left-join-with-the-same-table/m-p/699464#M213970</guid>
      <dc:creator>bebess</dc:creator>
      <dc:date>2020-11-17T12:30:43Z</dc:date>
    </item>
    <item>
      <title>Re: HASH full and then a left join with the same table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/HASH-full-and-then-a-left-join-with-the-same-table/m-p/699469#M213973</link>
      <description>&lt;P&gt;See if you can use this as a template&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data hash_full;
   if _N_ = 1 then do;
      dcl hash h(dataset : "TABLEB(rename=Year=Year_Past", multidata : "Y");
      h.definekey("Name", "ID");
      h.definedata("want", "Year_Past");
      h.definedone();
   end;

   set TABLEA;
   Year_Past = .;want = .;

   do while (h.do_over() = 0);
      if Year_Past &amp;lt;= Year then output;
   end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 17 Nov 2020 12:49:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/HASH-full-and-then-a-left-join-with-the-same-table/m-p/699469#M213973</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2020-11-17T12:49:51Z</dc:date>
    </item>
    <item>
      <title>Re: HASH full and then a left join with the same table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/HASH-full-and-then-a-left-join-with-the-same-table/m-p/699712#M214047</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/55544"&gt;@bebess&lt;/a&gt;&amp;nbsp;is this the answer you're after?&lt;/P&gt;</description>
      <pubDate>Wed, 18 Nov 2020 09:19:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/HASH-full-and-then-a-left-join-with-the-same-table/m-p/699712#M214047</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2020-11-18T09:19:02Z</dc:date>
    </item>
    <item>
      <title>Re: HASH full and then a left join with the same table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/HASH-full-and-then-a-left-join-with-the-same-table/m-p/699713#M214048</link>
      <description>Thank you it seems to work as i want. the only question i have is how to sort the output table like in my SQL example by ORD ( keep order of TABLEA table ) ?&lt;BR /&gt;Maybe it's not possible in that case to do it during the HASH, maybe i have to add another data step / sql step ....</description>
      <pubDate>Wed, 18 Nov 2020 09:20:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/HASH-full-and-then-a-left-join-with-the-same-table/m-p/699713#M214048</guid>
      <dc:creator>bebess</dc:creator>
      <dc:date>2020-11-18T09:20:41Z</dc:date>
    </item>
    <item>
      <title>Re: HASH full and then a left join with the same table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/HASH-full-and-then-a-left-join-with-the-same-table/m-p/699715#M214049</link>
      <description>Yes definitely great &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/31304"&gt;@PeterClemmensen&lt;/a&gt;</description>
      <pubDate>Wed, 18 Nov 2020 09:24:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/HASH-full-and-then-a-left-join-with-the-same-table/m-p/699715#M214049</guid>
      <dc:creator>bebess</dc:creator>
      <dc:date>2020-11-18T09:24:04Z</dc:date>
    </item>
    <item>
      <title>Re: HASH full and then a left join with the same table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/HASH-full-and-then-a-left-join-with-the-same-table/m-p/699716#M214050</link>
      <description>&lt;P&gt;The Set Statement read TableA sequentially, so the order from TableA will be intact in the output data set &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 18 Nov 2020 09:26:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/HASH-full-and-then-a-left-join-with-the-same-table/m-p/699716#M214050</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2020-11-18T09:26:43Z</dc:date>
    </item>
  </channel>
</rss>

