<?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 object vs proc sql in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/hash-object-vs-proc-sql/m-p/306768#M65629</link>
    <description>&lt;P&gt;I would like to calculate a sum, and add a field through the use of hash object&lt;BR /&gt;and get the same result (Table Calculated) obtained from the following proc sql:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data input;
input k1 k2 num;
datalines;
1 1  10
1 1 20
1 1 30
2 1 25
2 2 40
2 3 80
run;
proc sql;
create table calculated as 
select *, 
	sum(num) as sum_num,
	 case  when k1=1 then .
		  when k1=2 then 1
	 end as new_field
from input
group by  k1,k2;
quit;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Mon, 24 Oct 2016 13:13:37 GMT</pubDate>
    <dc:creator>mariopellegrini</dc:creator>
    <dc:date>2016-10-24T13:13:37Z</dc:date>
    <item>
      <title>hash object vs proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/hash-object-vs-proc-sql/m-p/306768#M65629</link>
      <description>&lt;P&gt;I would like to calculate a sum, and add a field through the use of hash object&lt;BR /&gt;and get the same result (Table Calculated) obtained from the following proc sql:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data input;
input k1 k2 num;
datalines;
1 1  10
1 1 20
1 1 30
2 1 25
2 2 40
2 3 80
run;
proc sql;
create table calculated as 
select *, 
	sum(num) as sum_num,
	 case  when k1=1 then .
		  when k1=2 then 1
	 end as new_field
from input
group by  k1,k2;
quit;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 24 Oct 2016 13:13:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/hash-object-vs-proc-sql/m-p/306768#M65629</guid>
      <dc:creator>mariopellegrini</dc:creator>
      <dc:date>2016-10-24T13:13:37Z</dc:date>
    </item>
    <item>
      <title>Re: hash object vs proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/hash-object-vs-proc-sql/m-p/306783#M65634</link>
      <description>&lt;P&gt;If you are looking for a non-SQL solution (maybe because of performance problems?), consider this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data help (keep=k1 k2 sum_num);
set input;
retain sum_num;
by k1 k2;
if first.k2 then sum_num = 0;
sum_num + num;
if last.k2 then output;
run;

data calculated;
merge
  input
  help
;
by k1 k2;
if k1 = 2 then new_field = 1;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;As opposed to the SQL, this also preserves the original order.&lt;/P&gt;</description>
      <pubDate>Mon, 24 Oct 2016 13:59:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/hash-object-vs-proc-sql/m-p/306783#M65634</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2016-10-24T13:59:30Z</dc:date>
    </item>
    <item>
      <title>Re: hash object vs proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/hash-object-vs-proc-sql/m-p/306793#M65641</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%MACRO sum(k1,k2);
   PROC SQL;
      UPDATE calculated2
      SET sum_num=(select sum(num) FROM calculated WHERE k1 eq &amp;amp;k1. AND k2 eq &amp;amp;k2. GROUP BY k1,k2)
      WHERE k1 eq &amp;amp;k1. AND k2 eq &amp;amp;k2. 
      ;
   QUIT;
%MEND;

DATA calculated2;
   length new_field sum_num 8;
   drop rc; 
   if _n_=1 then do; 
      call missing(new_field);
      declare hash NF();
      NF.definekey('k1');
      NF.definedata('new_field');
      NF.definedone(); 
      NF.add(key:1, data:.);
      NF.add(key:2, data:1);
   end;
   set input;
   call execute('%'||'sum('||put(k1,best.)||','||put(k2,best.)||');');
   rc=NF.find();
RUN;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Nice problem, thanks for asking &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;&lt;P&gt;Cheers,&lt;/P&gt;&lt;P&gt;Oligolas&lt;/P&gt;</description>
      <pubDate>Mon, 24 Oct 2016 14:05:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/hash-object-vs-proc-sql/m-p/306793#M65641</guid>
      <dc:creator>Oligolas</dc:creator>
      <dc:date>2016-10-24T14:05:50Z</dc:date>
    </item>
    <item>
      <title>Re: hash object vs proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/hash-object-vs-proc-sql/m-p/306844#M65660</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks!&lt;/P&gt;
&lt;P&gt;And if there was a more restrictive condition on the "case when"?&lt;/P&gt;
&lt;P&gt;For example:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;proc sql;&lt;BR /&gt;create table calculated as &lt;BR /&gt;select *, &lt;BR /&gt; sum(num) as sum_num,&lt;BR /&gt; case when k1=1 then .&lt;BR /&gt; when k1=2 &lt;STRONG&gt;and calculated sum_num &amp;lt; 50&lt;/STRONG&gt; then 1&lt;BR /&gt; end as new_field&lt;BR /&gt;from input&lt;BR /&gt;group by k1,k2;&lt;BR /&gt;quit;&lt;/P&gt;</description>
      <pubDate>Mon, 24 Oct 2016 15:13:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/hash-object-vs-proc-sql/m-p/306844#M65660</guid>
      <dc:creator>mariopellegrini</dc:creator>
      <dc:date>2016-10-24T15:13:21Z</dc:date>
    </item>
    <item>
      <title>Re: hash object vs proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/hash-object-vs-proc-sql/m-p/306870#M65676</link>
      <description>&lt;P&gt;In order to perform a match, hash needs clear keys. IMHO it can not deal with mnemonics operators like '&amp;lt;'&lt;/P&gt;&lt;P&gt;So you would have to calculate the sum_num and set the corresponding new_field first. Afterwards you can perform your merge with hash.&lt;/P&gt;&lt;P&gt;I would use &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562"&gt;@Kurt_Bremser﻿&lt;/a&gt;'s code to perform the first step, while defining the new_field value as requested and load the table into the hash for match merging. - of course only if I expect the time requested for programming to be compensated by run time &lt;span class="lia-unicode-emoji" title=":winking_face:"&gt;😉&lt;/span&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA help (keep=k1 k2 sum_num new_field);
   set input;
   retain sum_num;
   by k1 k2;
   if first.k2 then sum_num = 0;
   sum_num + num;

   if last.k2 then do;
      if k1 eq 1 then new_field=.;
      else if k1 eq 2 and sum_num &amp;lt; 50 then new_field=1;
      output;
   end;
RUN;

DATA calculated2;
   length new_field sum_num 8;
   drop rc; 
   if _n_=1 then do; 
      call missing(new_field, sum_num);
      declare hash NF(dataset:'help');
      NF.definekey('k1', 'k2');
      NF.definedata(all:'YES');
      NF.definedone();
   end;
   set input;
   rc=NF.find(key:k1,key:k2);
RUN;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 24 Oct 2016 16:25:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/hash-object-vs-proc-sql/m-p/306870#M65676</guid>
      <dc:creator>Oligolas</dc:creator>
      <dc:date>2016-10-24T16:25:40Z</dc:date>
    </item>
  </channel>
</rss>

