<?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: Rooling sum for one column between first datetime and datetime of that record (row) in SAS in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Rooling-sum-for-one-column-between-first-datetime-and-datetime/m-p/417091#M280320</link>
    <description>&lt;P&gt;Does this do what you are looking for?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=have;
    BY id opt t_purchase t_spent;
run;

DATA want;
    
    DO UNTIL(LAST.id);
        SET work.HAVE;
        BY id opt t_purchase t_spent;
        IF (FIRST.id) THEN sum_usage=0;
        sum_usage + usage;
        OUTPUT;
    END;
RUN;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Ahmed&lt;/P&gt;</description>
    <pubDate>Wed, 29 Nov 2017 15:35:57 GMT</pubDate>
    <dc:creator>AhmedAl_Attar</dc:creator>
    <dc:date>2017-11-29T15:35:57Z</dc:date>
    <item>
      <title>Rooling sum for one column between first datetime and datetime of that record (row) in SAS</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Rooling-sum-for-one-column-between-first-datetime-and-datetime/m-p/417063#M280317</link>
      <description>&lt;P&gt;&lt;SPAN&gt;I have one problem and I think there is not much to correct to work right. I have table:&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input id  opt  $   t_purchase   : datetime32. t_spent :datetime32.  bonus  usage ;
format  time   datetime32. ;
cards; 
1    a1  10NOV2017:12:02:00  10NOV2017:14:05:00   100     3 
1    a1  10NOV2017:12:02:00  10NOV2017:15:07:33   100     0  
1    a1  10NOV2017:12:02:00  10NOV2017:13:24:50   100     6 
2    a1  10NOV2017:13:54:00  10NOV2017:14:02:58   100     3 
1    a1  10NOV2017:12:02:00  10NOV2017:20:22:07   100    12    
2    a1  10NOV2017:13:54:00  10NOV2017:13:57:12   100     7 &lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;So, I need to sum all usage values from time_purchase (for one id, opt combination (group by id, opt) there is just one unique t_purchase) until t_spent. Also, I have about million of rows, so hash table would be the best solution. I've tried with:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;data want;
 if _n_=1 then do;
  if 0 then set have(rename=(usage=_usage));
  declare hash h(dataset:'have(rename=(usage=_usage))',hashexp:20);
  h.definekey('id','opt', 't_purchase', 't_spent');
  h.definedata('_usage');
  h.definedone();
 end;
set have;
sum_usage=0;
do i=intck('second', t_purchase, t_spent) to t_spent ;
 if h.find(key:user,key:id_option,key:i)=0 then sum_usage-_usage;
end;
drop _usage i;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;The fifth line from the bottom is not correct for sure,&lt;CODE&gt;(do i=intck('second', t_purchase, t_spent)&lt;/CODE&gt;&lt;SPAN&gt;&amp;nbsp;but have no idea how to approach this, how to select all records between two&amp;nbsp;datetime&amp;nbsp;values. So, the main problem is how to set up time interval to calculate this. I have already one function in this hash table&amp;nbsp;but with the slightly different time interval, so it would be pretty good to write this one too, but it's not necessary.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Desired output:&lt;/SPAN&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;id  opt    t_purchase            t_spent       bonus   usage sum_usage
1    a1  10NOV2017:12:02:00  10NOV2017:14:05:00   100     3        9
1    a1  10NOV2017:12:02:00  10NOV2017:15:07:33   100     0        9
1    a1  10NOV2017:12:02:00  10NOV2017:13:24:50   100     6        6
2    a1  10NOV2017:13:54:00  10NOV2017:14:02:58   100     3        10
1    a1  10NOV2017:12:02:00  10NOV2017:20:22:07   100    12        21 
2    a1  10NOV2017:13:54:00  10NOV2017:13:57:12   100     7 .      7&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 29 Nov 2017 14:32:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Rooling-sum-for-one-column-between-first-datetime-and-datetime/m-p/417063#M280317</guid>
      <dc:creator>jovic92</dc:creator>
      <dc:date>2017-11-29T14:32:06Z</dc:date>
    </item>
    <item>
      <title>Re: Rooling sum for one column between first datetime and datetime of that record (row) in SAS</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Rooling-sum-for-one-column-between-first-datetime-and-datetime/m-p/417087#M280318</link>
      <description>&lt;P&gt;What do you expect the "fifth line from the bottom" to be?&amp;nbsp; You do mean&amp;nbsp;the second record, with t_spent=10NOV2017:15:07:33, right?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Both record 1 (usage=3), and&amp;nbsp;record 3 (usage=6) fall within the time-spanned in record 2.&amp;nbsp; That's a total of sum_usage=9 (since record 2 has usage=0).&amp;nbsp; Records 4 and 6 have a different id/opt combination, and record 5 is outside the record 2 time span, so their usage is not part of record 2 sum_usage.&lt;/P&gt;</description>
      <pubDate>Wed, 29 Nov 2017 15:30:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Rooling-sum-for-one-column-between-first-datetime-and-datetime/m-p/417087#M280318</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2017-11-29T15:30:34Z</dc:date>
    </item>
    <item>
      <title>Re: Rooling sum for one column between first datetime and datetime of that record (row) in SAS</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Rooling-sum-for-one-column-between-first-datetime-and-datetime/m-p/417088#M280319</link>
      <description>I thought fifth line from bottom in code, not table. Intck function can't be used, i guess this way in this solution, but I have no idea how to solve this.</description>
      <pubDate>Wed, 29 Nov 2017 15:33:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Rooling-sum-for-one-column-between-first-datetime-and-datetime/m-p/417088#M280319</guid>
      <dc:creator>jovic92</dc:creator>
      <dc:date>2017-11-29T15:33:01Z</dc:date>
    </item>
    <item>
      <title>Re: Rooling sum for one column between first datetime and datetime of that record (row) in SAS</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Rooling-sum-for-one-column-between-first-datetime-and-datetime/m-p/417091#M280320</link>
      <description>&lt;P&gt;Does this do what you are looking for?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=have;
    BY id opt t_purchase t_spent;
run;

DATA want;
    
    DO UNTIL(LAST.id);
        SET work.HAVE;
        BY id opt t_purchase t_spent;
        IF (FIRST.id) THEN sum_usage=0;
        sum_usage + usage;
        OUTPUT;
    END;
RUN;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Ahmed&lt;/P&gt;</description>
      <pubDate>Wed, 29 Nov 2017 15:35:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Rooling-sum-for-one-column-between-first-datetime-and-datetime/m-p/417091#M280320</guid>
      <dc:creator>AhmedAl_Attar</dc:creator>
      <dc:date>2017-11-29T15:35:57Z</dc:date>
    </item>
    <item>
      <title>Re: Rooling sum for one column between first datetime and datetime of that record (row) in SAS</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Rooling-sum-for-one-column-between-first-datetime-and-datetime/m-p/417231#M280321</link>
      <description>&lt;P&gt;This can be a bit more compact.&amp;nbsp; In particular you don't need the "do until ..." group, and you don't need the explicit OUTPUT statement:&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;proc sort data=have;
    BY id opt t_purchase t_spent;
run;

DATA want;
  SET work.HAVE;
  BY id opt t_purchase;
  IF (FIRST.purchase) THEN sum_usage=0;
  sum_usage + usage;
RUN;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 29 Nov 2017 20:44:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Rooling-sum-for-one-column-between-first-datetime-and-datetime/m-p/417231#M280321</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2017-11-29T20:44:27Z</dc:date>
    </item>
    <item>
      <title>Re: Rooling sum for one column between first datetime and datetime of that record (row) in SAS</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Rooling-sum-for-one-column-between-first-datetime-and-datetime/m-p/417341#M280322</link>
      <description>&lt;P&gt;If you can sort the input data, this should do it:&lt;/P&gt;&lt;PRE&gt;proc sort data=have;
  by id opt t_purchase t_spent;
run;
data want;
  set have;
  by id opt t_purchase t_spent;
  if first.t_purchase then
    sum_usage=usage;
  else
    sum_usage+usage;
run;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 30 Nov 2017 09:24:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Rooling-sum-for-one-column-between-first-datetime-and-datetime/m-p/417341#M280322</guid>
      <dc:creator>s_lassen</dc:creator>
      <dc:date>2017-11-30T09:24:39Z</dc:date>
    </item>
    <item>
      <title>Re: Rooling sum for one column between first datetime and datetime of that record (row) in SAS</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Rooling-sum-for-one-column-between-first-datetime-and-datetime/m-p/417408#M280323</link>
      <description>&lt;P&gt;OK. If I understand your question.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input id  opt  $   t_purchase   : datetime32. t_spent :datetime32.  bonus  usage ;
format  t_purchase  t_spent datetime32. ;
cards; 
1    a1  10NOV2017:12:02:00  10NOV2017:14:05:00   100     3 
1    a1  10NOV2017:12:02:00  10NOV2017:15:07:33   100     0  
1    a1  10NOV2017:12:02:00  10NOV2017:13:24:50   100     6 
2    a1  10NOV2017:13:54:00  10NOV2017:14:02:58   100     3 
1    a1  10NOV2017:12:02:00  10NOV2017:20:22:07   100    12    
2    a1  10NOV2017:13:54:00  10NOV2017:13:57:12   100     7 
;
run;

data want;
 if _n_=1 then do;
  if 0 then set have(rename=(usage=_usage));
  declare hash h(dataset:'have(rename=(usage=_usage))',hashexp:20);
  h.definekey('id','opt', 't_purchase', 't_spent');
  h.definedata('_usage');
  h.definedone();
 end;
set have;
sum_usage=0;
do i=t_purchase to t_spent ;
 if h.find(key:id,key:opt,key:t_purchase,key:i)=0 then sum_usage+_usage;
end;
drop _usage i;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 30 Nov 2017 14:03:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Rooling-sum-for-one-column-between-first-datetime-and-datetime/m-p/417408#M280323</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2017-11-30T14:03:25Z</dc:date>
    </item>
  </channel>
</rss>

