<?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 Tables Sum function for aggreagaion using other than hash table key for sum function. in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Hash-Tables-Sum-function-for-aggreagaion-using-other-than-hash/m-p/829494#M327736</link>
    <description>Thank you very much for the reply. Great techniques.</description>
    <pubDate>Sat, 20 Aug 2022 14:17:59 GMT</pubDate>
    <dc:creator>val_nikolajevs</dc:creator>
    <dc:date>2022-08-20T14:17:59Z</dc:date>
    <item>
      <title>Hash Tables Sum function for aggreagaion using other than hash table key for sum function.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Hash-Tables-Sum-function-for-aggreagaion-using-other-than-hash/m-p/829466#M327722</link>
      <description>&lt;P&gt;Hello I am learning hash tables and and one of the task i would liko to get variable value and prorate based on month.&lt;/P&gt;
&lt;P&gt;Fro example&lt;/P&gt;
&lt;P&gt;ID 1 from jun15 to jul10 was 25 units or 1 unit per day therefore 15 units will be in June bucket and 10 in July for this period&lt;/P&gt;
&lt;P&gt;from jul10 to Aug10 it was 30 units or 30/31 per day threfore 21*(30/31) would go to July bucket and 10*(30/31) would go to the august bucket and so on.&lt;/P&gt;
&lt;P&gt;So the aggregated value for the ID one for July would 10+21*(30/31);&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Inside the step I am breaking down each ID with time range to a separate prorated rows--it works results are correct;&lt;/P&gt;
&lt;P&gt;The aggregation function gives me trouble; Tried different options and placing sum at different locations and got ether missing results and or repeats or some unrelated one.&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;data _null_;&lt;BR /&gt;retain sum 0;&lt;BR /&gt;dcl hash hkw(ordered:"A", multidata:"Y",SUMINC:"prorated");&lt;BR /&gt;hkw.defineKey("ID","month");&lt;BR /&gt;hkw.defineData("ID","month","sum","prorated");&lt;BR /&gt;hkw.defineDone();&lt;/P&gt;
&lt;P&gt;format prorated sum COMMA15.12;&lt;BR /&gt;format month Date9.;&lt;BR /&gt;Do until (LR=1);&lt;BR /&gt;set p1 end=LR;&lt;BR /&gt;rc=hkw.find();&lt;BR /&gt;do i=0 to intck('month',time_start,time_stop);&lt;BR /&gt;month = intnx('month',time_start,i,'b');&lt;BR /&gt;days = min(bill_to-1,intnx('month',month,0,'e'))-max(time_start,month)+1;&lt;BR /&gt;prorated=(var/(time_stop-time_start))*days;&lt;BR /&gt;hkw.ADD(); &lt;BR /&gt;hkw.sum(key:ID,key:month, sum:sum);&lt;BR /&gt;&lt;BR /&gt;end;&lt;BR /&gt;&lt;BR /&gt;end;&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;end;&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;hkw.output(dataset:"wh1");&lt;BR /&gt;stop;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;For some reason I can not make aggregation function to work-- Could you please let me know what I am missing? Need to aggregate by ID and month so each ID has sum by month.&lt;/P&gt;</description>
      <pubDate>Sat, 20 Aug 2022 01:14:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Hash-Tables-Sum-function-for-aggreagaion-using-other-than-hash/m-p/829466#M327722</guid>
      <dc:creator>val_nikolajevs</dc:creator>
      <dc:date>2022-08-20T01:14:32Z</dc:date>
    </item>
    <item>
      <title>Re: Hash Tables Sum function for aggreagaion using other than hash table key for sum function.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Hash-Tables-Sum-function-for-aggreagaion-using-other-than-hash/m-p/829469#M327724</link>
      <description>&lt;P&gt;To help us help you it's for questions like yours normally really useful to get sample data provided via a&amp;nbsp; fully working data step creating such data (here for your work.p1 table).&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Then show us the desired result based on this sample data and explain us the logic how to get from Have to Want.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;To post SAS code use the running man icon.&lt;BR /&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Patrick_0-1660960999168.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/74544iD2A7974142825049/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Patrick_0-1660960999168.png" alt="Patrick_0-1660960999168.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;If you have existing sample data you can share (if volume "reasonable") then either attach your SAS table to your question or alternatively generate SAS datastep code that creates the sample data&amp;nbsp;&lt;A href="https://blogs.sas.com/content/sastraining/2016/03/11/jedi-sas-tricks-data-to-data-step-macro/" target="_blank"&gt;https://blogs.sas.com/content/sastraining/2016/03/11/jedi-sas-tricks-data-to-data-step-macro/&amp;nbsp;&lt;/A&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 20 Aug 2022 02:21:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Hash-Tables-Sum-function-for-aggreagaion-using-other-than-hash/m-p/829469#M327724</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2022-08-20T02:21:17Z</dc:date>
    </item>
    <item>
      <title>Re: Hash Tables Sum function for aggreagaion using other than hash table key for sum function.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Hash-Tables-Sum-function-for-aggreagaion-using-other-than-hash/m-p/829471#M327726</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;*define data sample;
Data pk1;
format time_start time_stop date9.;
ID=1;
time_start='15JUN2021'd;
time_stop='10JUL2021'd;
var=100;
output;
ID=1;
time_start='10JUL2021'd;
time_stop='15AUG2021'd;
var=120;
output;
ID=1;
time_start='15AUG2021'd;
time_stop='10SEP2021'd;
var=20;
output;
ID=2;
time_start='05JUN2021'd;
time_stop='03JUL2021'd;
var=400;
output;
ID=2;
time_start='03JUL2021'd;
time_stop='12AUG2021'd;
var=343;
output;
ID=2;
time_start='12AUG2021'd;
time_stop='13SEP2021'd;
var=543;
output;
run;
*prorate based on number of days in a month;
data pk2 (drop=time_start time_stop var i days);
format time_start time_stop date9.;
format month monyy7.;
set pk1;
  do i=0 to intck('month',time_start,Time_Stop);
    month = intnx('month',time_start,i,'b');
    days = min(time_stop-1,intnx('month',month,0,'e'))-max(time_start,month)+1;
	prorated=(var/(time_stop-time_start))*days;
    output;
  end;

run;
*aggregate using proc sql;
proc sql;
create table pk3 as
select distinct ID,month, sum(prorated) as sum_var
from pk2
group by ID,month
order by ID,month;
quit; 


*transpose to create a single row for each ID with prorated values for each month;

proc transpose data=pk3 out=p3k4(DROP= _NAME_ _LABEL_);
by ID ;
id month ;
var sum_var;
run;

&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The output of the above program sample is what I am trying to acomplish using hash tables. The above works and gets exactly what I need.&lt;/P&gt;
&lt;P&gt;I am looking how to use hash table to combine data step with proration, aggregation and ordering output to feed output to transpose step.&lt;/P&gt;
&lt;P&gt;Thank you.&lt;/P&gt;</description>
      <pubDate>Sat, 20 Aug 2022 03:22:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Hash-Tables-Sum-function-for-aggreagaion-using-other-than-hash/m-p/829471#M327726</guid>
      <dc:creator>val_nikolajevs</dc:creator>
      <dc:date>2022-08-20T03:22:10Z</dc:date>
    </item>
    <item>
      <title>Re: Hash Tables Sum function for aggreagaion using other than hash table key for sum function.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Hash-Tables-Sum-function-for-aggreagaion-using-other-than-hash/m-p/829472#M327727</link>
      <description>&lt;P&gt;I might have used suminc not more than once or twice. I normally just find it too hard and not intuitive enough to use.&lt;/P&gt;
&lt;P&gt;Based on the code you share that's how I would do it if using a hash.&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;

  if _n_=1 then
    do;
      dcl hash hkw(ordered:"A", multidata:"N");
      hkw.defineKey("ID","month");
      hkw.defineData("ID","month","prorated");
      hkw.defineDone();
    end;

  format time_start time_stop date9.;
  format month monyy7.;
  set pk1 end=_last;

  do i=0 to intck('month',time_start,Time_Stop);
    month = intnx('month',time_start,i,'b');
    days = min(time_stop-1,intnx('month',month,0,'e'))-max(time_start,month)+1;
    _prorated=(var/(time_stop-time_start))*days;
    if hkw.find()=0 then prorated=sum(prorated,_prorated);
    else prorated=_prorated;
    _rc=hkw.replace();
  end;
  if _last then hkw.output(dataset:'pk3');
run;

proc print data=pk3;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The most relevant thing you missed is to execute the hash definition only once in the data step and not for every single iteration (if _n_=1 then....).&lt;/P&gt;</description>
      <pubDate>Sat, 20 Aug 2022 04:46:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Hash-Tables-Sum-function-for-aggreagaion-using-other-than-hash/m-p/829472#M327727</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2022-08-20T04:46:44Z</dc:date>
    </item>
    <item>
      <title>Re: Hash Tables Sum function for aggreagaion using other than hash table key for sum function.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Hash-Tables-Sum-function-for-aggreagaion-using-other-than-hash/m-p/829477#M327729</link>
      <description>&lt;P&gt;You don't need the proc transpose.&amp;nbsp; You can do what you in a data step, with a two-dimensional array (rows for year, and columns for months - assuming you need more than one year.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Say you know your earliest data is in MAR2020 and the latest data is in SEP2021:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set pk1;
  by id;
  array ym {2020:2021,1:12}
   _dummy  _dummy  MAR2020 APR2020 MAY2020 JUN2020 JUL2020 AUG2020 SEP2020 OCT2020 NOV2020 DEC2020
   JAN2021 FEB2021 MAR2021 APR2021 MAY2021 JUN2021 JUL2021 AUG2021 SEP2021 _dummy  _dummy  _dummy;

  if first.id then call missing (of ym{*});

  _ndays=time_stop - time_start;
  do dateval=time_start by 0 until (dateval&amp;gt;time_stop);
    nxt_dateval=intnx('month',dateval,1,'beg');
    _ndays_this_month=min(nxt_dateval,time_stop)-dateval;
    ym{year(dateval),month(dateval)} + var * _ndays_this_month/_ndays;
    dateval=nxt_dateval;
  end;
  keep ID  mar2020 -- sep2021;
  if last.id;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 20 Aug 2022 09:14:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Hash-Tables-Sum-function-for-aggreagaion-using-other-than-hash/m-p/829477#M327729</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2022-08-20T09:14:07Z</dc:date>
    </item>
    <item>
      <title>Re: Hash Tables Sum function for aggreagaion using other than hash table key for sum function.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Hash-Tables-Sum-function-for-aggreagaion-using-other-than-hash/m-p/829485#M327730</link>
      <description>&lt;P&gt;Hello&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/313690"&gt;@val_nikolajevs&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You've got great responses from &lt;A href="https://communities.sas.com/t5/user/viewprofilepage/user-id/12447" target="_blank" rel="noopener"&gt;Patrick&lt;/A&gt; and &lt;A href="https://communities.sas.com/t5/user/viewprofilepage/user-id/31461" target="_blank" rel="noopener"&gt;mkeintz&lt;/A&gt;. Let me just add a few words about the &lt;A href="https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/lecompobjref/n0bzcn2c8368wnn160kom4th35p2.htm" target="_blank" rel="noopener"&gt;SUM method&lt;/A&gt;, as this was the primary subject of your question. This method is really a bit tricky, but still relatively simple compared to the &lt;A href="https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/lecompobjref/p06dsrxj7f6w4hn1q0qt9p06adup.htm" target="_blank" rel="noopener"&gt;SUMDUP method&lt;/A&gt;.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Basically, by adding the argument tag &lt;FONT face="courier new,courier"&gt;suminc:"prorated"&lt;/FONT&gt;&amp;nbsp;to the declaration of hash object HKW you instruct SAS to set up a sum of values of variable PRORATED &lt;EM&gt;for each item&lt;/EM&gt; of HKW. (These sums are initialized to zero automatically, they are never missing, so there's no need to initialize them manually.) If you also specify &lt;FONT face="courier new,courier"&gt;multidata:"Y"&lt;/FONT&gt;, this means, in general,&amp;nbsp;&lt;EM&gt;multiple&lt;/EM&gt; sums per key value combination. But your key items are ID and MONTH and you also want to aggregate on that level. So, omit the&amp;nbsp;&lt;FONT face="courier new,courier"&gt;multidata:"Y"&lt;/FONT&gt; argument tag to get only one sum per ID-MONTH combination.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It seems unusual to me that your sum variable (named SUM) is also a data item of HKW. Typically, the final sum values are only "harvested" at the end, but if SUM is a data item, it will be involved in all intermediate operations of the hash object. Therefore, I would rather keep SUM out of the data items and write the output dataset with a traditional OUTPUT &lt;EM&gt;statement&lt;/EM&gt; (instead of using the OUTPUT &lt;EM&gt;method&lt;/EM&gt; of HKW, requiring SUM to be a data item).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can use the &lt;A href="https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/lecompobjref/n1918zoztwp0uzn1jgzmg6duycfb.htm" target="_blank" rel="noopener"&gt;REF method&lt;/A&gt; to trigger the incrementation of the sums when a new PRORATED value is available. A&amp;nbsp;&lt;A href="https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/lecompobjref/n0165vsqfp6opan1a7a9eewcptdn.htm" target="_blank" rel="noopener"&gt;hash iterator object&lt;/A&gt;&amp;nbsp;can drive the retrieval of the accumulated sum values at the end of the DATA step. All this is shown in the code below. (With the code structure using the DOW loop and the STOP statement, copied from your original code, there's no need to wrap the hash object declaration into "&lt;FONT face="courier new,courier"&gt;if _n_=1 then do; ... end;&lt;/FONT&gt;" because it's executed only once anyway: &lt;FONT face="courier new,courier"&gt;_n_&lt;/FONT&gt; is constantly 1 throughout this DATA step.)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want(keep=id month sum);
dcl hash hkw(ordered:'a', suminc:'prorated');
hkw.defineKey('ID','month');
hkw.defineDone();
dcl hiter hi('hkw');

do until (LR=1);
  set pk1 end=LR;
  do i=0 to intck('month',time_start,time_stop);
    month = intnx('month',time_start,i,'b');
    days = min(time_stop-1,intnx('month',month,0,'e'))-max(time_start,month)+1;
    prorated=(var/(time_stop-time_start))*days;
    hkw.ref();
  end;
end;

do while(hi.next()=0);
  hkw.sum(sum:sum);
  output;
end;

stop;
format month monyy7. sum comma18.8;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 20 Aug 2022 11:21:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Hash-Tables-Sum-function-for-aggreagaion-using-other-than-hash/m-p/829485#M327730</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2022-08-20T11:21:11Z</dc:date>
    </item>
    <item>
      <title>Re: Hash Tables Sum function for aggreagaion using other than hash table key for sum function.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Hash-Tables-Sum-function-for-aggreagaion-using-other-than-hash/m-p/829492#M327734</link>
      <description>Thank you very much for detailed explanation--that really helps to grasp how this works.</description>
      <pubDate>Sat, 20 Aug 2022 14:16:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Hash-Tables-Sum-function-for-aggreagaion-using-other-than-hash/m-p/829492#M327734</guid>
      <dc:creator>val_nikolajevs</dc:creator>
      <dc:date>2022-08-20T14:16:05Z</dc:date>
    </item>
    <item>
      <title>Re: Hash Tables Sum function for aggreagaion using other than hash table key for sum function.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Hash-Tables-Sum-function-for-aggreagaion-using-other-than-hash/m-p/829493#M327735</link>
      <description>Hi Patrick--thank you very much that what i was looking for.</description>
      <pubDate>Sat, 20 Aug 2022 14:17:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Hash-Tables-Sum-function-for-aggreagaion-using-other-than-hash/m-p/829493#M327735</guid>
      <dc:creator>val_nikolajevs</dc:creator>
      <dc:date>2022-08-20T14:17:05Z</dc:date>
    </item>
    <item>
      <title>Re: Hash Tables Sum function for aggreagaion using other than hash table key for sum function.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Hash-Tables-Sum-function-for-aggreagaion-using-other-than-hash/m-p/829494#M327736</link>
      <description>Thank you very much for the reply. Great techniques.</description>
      <pubDate>Sat, 20 Aug 2022 14:17:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Hash-Tables-Sum-function-for-aggreagaion-using-other-than-hash/m-p/829494#M327736</guid>
      <dc:creator>val_nikolajevs</dc:creator>
      <dc:date>2022-08-20T14:17:59Z</dc:date>
    </item>
  </channel>
</rss>

