<?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 Convert left join to hash data step in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Convert-left-join-to-hash-data-step/m-p/863146#M340972</link>
    <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Good morning everyone.&lt;BR /&gt;I'm trying to convert a proc sql join to a hashed data step, but I don't get the same result because of:&lt;BR /&gt;- duplicates that the hash does not consider;&lt;BR /&gt;- of an "ON" condition that I can't put into the data hash step.&lt;BR /&gt;Here is the example:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data ds1;
input k1 ts_start ;
datalines;
1 4
;

data ds2;
input k1 ts_start ts_end;
datalines;
1 1 9
1 2 8
1 6 8
;


proc sql noprint;
create table ds_tot
   as select A.k1,
             B.ts_start,
			 B.ts_end
   from  ds1 A
   left join   ds2 B
   on  A.K1=B.K1 and 
       A.ts_start between B.ts_start and B.ts_end;
quit;


data ds_tot_hash;
if 0 then set ds1 ds2;
	 declare hash h_merge(dataset:"ds2",   multidata:'y' );
	 rc = h_merge.DefineKey("k1");
	 rc = h_merge.DefineData("ts_start", "ts_end");
	 rc = h_merge.DefineDone();
drop rc;
do while (not eof);
   set ds1 (keep=k1) end=eof;
   if h_merge.find() &amp;gt;= 0;  
   output;
end;
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;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 09 Mar 2023 10:04:19 GMT</pubDate>
    <dc:creator>mariopellegrini</dc:creator>
    <dc:date>2023-03-09T10:04:19Z</dc:date>
    <item>
      <title>Convert left join to hash data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Convert-left-join-to-hash-data-step/m-p/863146#M340972</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Good morning everyone.&lt;BR /&gt;I'm trying to convert a proc sql join to a hashed data step, but I don't get the same result because of:&lt;BR /&gt;- duplicates that the hash does not consider;&lt;BR /&gt;- of an "ON" condition that I can't put into the data hash step.&lt;BR /&gt;Here is the example:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data ds1;
input k1 ts_start ;
datalines;
1 4
;

data ds2;
input k1 ts_start ts_end;
datalines;
1 1 9
1 2 8
1 6 8
;


proc sql noprint;
create table ds_tot
   as select A.k1,
             B.ts_start,
			 B.ts_end
   from  ds1 A
   left join   ds2 B
   on  A.K1=B.K1 and 
       A.ts_start between B.ts_start and B.ts_end;
quit;


data ds_tot_hash;
if 0 then set ds1 ds2;
	 declare hash h_merge(dataset:"ds2",   multidata:'y' );
	 rc = h_merge.DefineKey("k1");
	 rc = h_merge.DefineData("ts_start", "ts_end");
	 rc = h_merge.DefineDone();
drop rc;
do while (not eof);
   set ds1 (keep=k1) end=eof;
   if h_merge.find() &amp;gt;= 0;  
   output;
end;
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;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 09 Mar 2023 10:04:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Convert-left-join-to-hash-data-step/m-p/863146#M340972</guid>
      <dc:creator>mariopellegrini</dc:creator>
      <dc:date>2023-03-09T10:04:19Z</dc:date>
    </item>
    <item>
      <title>Re: Convert left join to hash data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Convert-left-join-to-hash-data-step/m-p/863151#M340975</link>
      <description>&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 ds_tot_hash(drop = ts);

   if _N_ = 1 then do;
      dcl hash h(dataset : 'ds2', multidata : 'Y');
      h.definekey('k1');
      h.definedata(all : 'Y');
      h.definedone();
   end;

   set ds1(rename = ts_start = ts);

   if 0 then set ds2;

   do while (h.do_over() = 0);
      if ts_start &amp;lt;= ts &amp;lt;= ts_end then output;
   end;

run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;U&gt;&lt;STRONG&gt;Result:&lt;/STRONG&gt;&lt;/U&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;k1 ts_start  ts_end
1  1         9
1  2         8&lt;/PRE&gt;</description>
      <pubDate>Thu, 09 Mar 2023 10:40:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Convert-left-join-to-hash-data-step/m-p/863151#M340975</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2023-03-09T10:40:21Z</dc:date>
    </item>
    <item>
      <title>Re: Convert left join to hash data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Convert-left-join-to-hash-data-step/m-p/863153#M340976</link>
      <description>&lt;P&gt;I think this should work:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data ds_tot_hash;                                                                                                                       
  if 0 then set ds1 ds2;                                                                                                                
  if _N_=1 then do;                                                                                                                     
       declare hash h_merge(dataset:"ds2",   multidata:'y' );                                                                           
       rc = h_merge.DefineKey("k1");                                                                                                    
       rc = h_merge.DefineData("ts_start", "ts_end");                                                                                   
       rc = h_merge.DefineDone();                                                                                                       
   end;                                                                                                                                 
   found=0;                                                                                                                             
   set ds1 (keep=k1 ts_start rename=(ts_start=a_start)) end=eof;                                                                        
   rc=h_merge.find();                                                                                                                   
   do while(rc=0);                                                                                                                      
     if ts_start&amp;lt;=a_start&amp;lt;=ts_end then do;                                                                                              
       found=1;                                                                                                                         
       output;                                                                                                                          
       end;                                                                                                                             
     rc=h_merge.find_next();                                                                                                            
     end;                                                                                                                               
   if found=0 then do;                                                                                                                  
     call missing(ts_start,ts_end);                                                                                                     
     output;                                                                                                                            
     end;                                                                                                                               
  keep k1 ts_start ts_end;                                                                                                              
run;                      
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;We have to include the FOUND variable, to check if something has been output, in order to simulate the left join.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Only question: is this really worth the trouble?&lt;/P&gt;</description>
      <pubDate>Thu, 09 Mar 2023 10:49:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Convert-left-join-to-hash-data-step/m-p/863153#M340976</guid>
      <dc:creator>s_lassen</dc:creator>
      <dc:date>2023-03-09T10:49:07Z</dc:date>
    </item>
  </channel>
</rss>

