<?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: Total sum before and after dates in a dataset in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Total-sum-before-and-after-dates-in-a-dataset/m-p/937290#M368282</link>
    <description>I just learnt something new!</description>
    <pubDate>Fri, 26 Jul 2024 17:15:51 GMT</pubDate>
    <dc:creator>Zatere</dc:creator>
    <dc:date>2024-07-26T17:15:51Z</dc:date>
    <item>
      <title>Total sum before and after dates in a dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Total-sum-before-and-after-dates-in-a-dataset/m-p/937198#M368248</link>
      <description>&lt;P&gt;Hi there,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have a question and hopefully I will make sense.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have two tables:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have1;
input ID $ DT :yymmdd10.;
format DT yymmddd10.;
datalines;
A 2024-07-04
A 2024-07-25
;
run;

data have2;
input ID $ DT :yymmdd10. AMT;
format DT yymmddd10.;
datalines;
A 2024-07-03 10
A 2024-07-04 20
A 2024-07-04 30
A 2024-07-11 40
A 2024-07-11 50
A 2024-07-16 70
A 2024-07-16 80
A 2024-07-25 90
A 2024-07-26 100
;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I want to combine them in such a way that in the first table I can add the total sum of the second table for&amp;nbsp;&lt;SPAN&gt;before-or-equal and after the dates in the first table.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Essentially, I want the following:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
input ID $ DT :yymmdd10. Before_upto_DT After_DT;
format DT yymmddd10.;
datalines;
A 2024-07-04 60 430
A 2024-07-25 390 100
; 
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;My attempt was to join them using PROC SQL but obviously this is not working.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Any help please.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks in advance.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 26 Jul 2024 08:33:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Total-sum-before-and-after-dates-in-a-dataset/m-p/937198#M368248</guid>
      <dc:creator>Zatere</dc:creator>
      <dc:date>2024-07-26T08:33:42Z</dc:date>
    </item>
    <item>
      <title>Re: Total sum before and after dates in a dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Total-sum-before-and-after-dates-in-a-dataset/m-p/937201#M368250</link>
      <description>&lt;P&gt;Yes. You pick up the right tool (SQL) for small data.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have1;
input ID $ DT :yymmdd10.;
format DT yymmddd10.;
datalines;
A 2024-07-04
A 2024-07-25
;
run;

data have2;
input ID $ DT :yymmdd10. AMT;
format DT yymmddd10.;
datalines;
A 2024-07-03 10
A 2024-07-04 20
A 2024-07-04 30
A 2024-07-11 40
A 2024-07-11 50
A 2024-07-16 70
A 2024-07-16 80
A 2024-07-25 90
A 2024-07-26 100
;
run;

proc sql;
create table want as
select *,
 (select sum(AMT) from have2 where ID=a.ID and DT&amp;lt;=a.DT) as Before_upto_DT ,
 (select sum(AMT) from have2 where ID=a.ID and DT&amp;gt; a.DT) as After_DT
 from have1 as a;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 26 Jul 2024 08:41:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Total-sum-before-and-after-dates-in-a-dataset/m-p/937201#M368250</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2024-07-26T08:41:32Z</dc:date>
    </item>
    <item>
      <title>Re: Total sum before and after dates in a dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Total-sum-before-and-after-dates-in-a-dataset/m-p/937203#M368252</link>
      <description>Hi Ksharp thanks for your reply. It is working indeed. However, as you have mentioned this solution would work for small data. Both tables that I have are quite big so it takes a long time to finish.</description>
      <pubDate>Fri, 26 Jul 2024 08:55:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Total-sum-before-and-after-dates-in-a-dataset/m-p/937203#M368252</guid>
      <dc:creator>Zatere</dc:creator>
      <dc:date>2024-07-26T08:55:26Z</dc:date>
    </item>
    <item>
      <title>Re: Total sum before and after dates in a dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Total-sum-before-and-after-dates-in-a-dataset/m-p/937212#M368253</link>
      <description>&lt;P&gt;How big are these two tables ?&lt;/P&gt;
&lt;P&gt;You could try Hash Table.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have1;
input ID $ DT :yymmdd10.;
format DT yymmddd10.;
datalines;
A 2024-07-04
A 2024-07-25
;
run;

data have2;
input ID $ DT :yymmdd10. AMT;
format DT yymmddd10.;
datalines;
A 2024-07-03 10
A 2024-07-04 20
A 2024-07-04 30
A 2024-07-11 40
A 2024-07-11 50
A 2024-07-16 70
A 2024-07-16 80
A 2024-07-25 90
A 2024-07-26 100
;
run;

data want;
 if _n_=1 then do;
  if 0 then set have2;
  declare hash h(dataset:'have2',multidata:'y',hashexp:20);
  h.definekey('ID','DT');
  h.definedata('AMT');
  h.definedone();
 end;
set have1;
Before_upto_DT=0;
do i=DT-100*356 to DT;
 rc=h.find(key:ID,key:i);
 do while(rc=0);
   Before_upto_DT+AMT;
   rc=h.find_next(key:ID,key:i);
 end;
end;

After_DT=0;
do i=DT+1 to DT+100*356 ;
 rc=h.find(key:ID,key:i);
 do while(rc=0);
   After_DT+AMT;
   rc=h.find_next(key:ID,key:i);
 end;
end;

drop rc i;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 26 Jul 2024 09:37:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Total-sum-before-and-after-dates-in-a-dataset/m-p/937212#M368253</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2024-07-26T09:37:23Z</dc:date>
    </item>
    <item>
      <title>Re: Total sum before and after dates in a dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Total-sum-before-and-after-dates-in-a-dataset/m-p/937222#M368255</link>
      <description>&lt;P&gt;For large datasets, I recommend defining a date-indexed array which you load for every ID with the data from have2.&lt;/P&gt;
&lt;P&gt;Then you work through the entries in have1 for that ID and calculate in DO loops.&lt;/P&gt;
&lt;P&gt;How large are your datasets? Is there at least one entry in have1 for every ID in have2?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 26 Jul 2024 10:46:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Total-sum-before-and-after-dates-in-a-dataset/m-p/937222#M368255</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2024-07-26T10:46:48Z</dc:date>
    </item>
    <item>
      <title>Re: Total sum before and after dates in a dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Total-sum-before-and-after-dates-in-a-dataset/m-p/937225#M368256</link>
      <description>Both are quite big tables. Especially the have2 goes up to many millions of rows. Do you perhaps have an example of this approach?</description>
      <pubDate>Fri, 26 Jul 2024 10:51:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Total-sum-before-and-after-dates-in-a-dataset/m-p/937225#M368256</guid>
      <dc:creator>Zatere</dc:creator>
      <dc:date>2024-07-26T10:51:57Z</dc:date>
    </item>
    <item>
      <title>Re: Total sum before and after dates in a dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Total-sum-before-and-after-dates-in-a-dataset/m-p/937257#M368266</link>
      <description>&lt;P&gt;Please answer my second question. The answer is crucial to how the approach must be coded.&lt;/P&gt;</description>
      <pubDate>Fri, 26 Jul 2024 14:33:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Total-sum-before-and-after-dates-in-a-dataset/m-p/937257#M368266</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2024-07-26T14:33:32Z</dc:date>
    </item>
    <item>
      <title>Re: Total sum before and after dates in a dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Total-sum-before-and-after-dates-in-a-dataset/m-p/937260#M368268</link>
      <description>Sorry about that. Yes there is at least one entry in have1 for every ID in have2.</description>
      <pubDate>Fri, 26 Jul 2024 14:40:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Total-sum-before-and-after-dates-in-a-dataset/m-p/937260#M368268</guid>
      <dc:creator>Zatere</dc:creator>
      <dc:date>2024-07-26T14:40:29Z</dc:date>
    </item>
    <item>
      <title>Re: Total sum before and after dates in a dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Total-sum-before-and-after-dates-in-a-dataset/m-p/937281#M368277</link>
      <description>&lt;P&gt;So we can try it with synchronized groups, using a date-indexed array:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let start = %sysfunc(inputn(1900-01-01,yymmdd10.));
%let end = %sysfunc(inputn(2999-12-31,yymmdd10.));

data want;
array vals {&amp;amp;start.:&amp;amp;end.} _temporary_;
do i = &amp;amp;start. to &amp;amp;end.;
  vals{i} = 0;
end;
do until (last.id);
  set have2;
  by id;
  vals{dt} = vals{dt} + amt;
end;
do until (last.id);
  set have1;
  by id;
  Before_upto_DT = 0;
  After_DT = 0;
  do i = &amp;amp;start. to dt;
    Before_upto_DT = Before_upto_DT + vals{i};
  end;
  do i = dt + 1 to &amp;amp;end.;
    After_DT =  After_DT + vals{i};
  end;
  output;
end;
drop i amt;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 26 Jul 2024 16:14:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Total-sum-before-and-after-dates-in-a-dataset/m-p/937281#M368277</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2024-07-26T16:14:16Z</dc:date>
    </item>
    <item>
      <title>Re: Total sum before and after dates in a dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Total-sum-before-and-after-dates-in-a-dataset/m-p/937287#M368280</link>
      <description>It is working so thank you. Actually there are IDs in have2 which are not in have1. I just added a small step to create the have2b which has only IDs that are in have1. How can I modify your approach to address this as well please?</description>
      <pubDate>Fri, 26 Jul 2024 16:52:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Total-sum-before-and-after-dates-in-a-dataset/m-p/937287#M368280</guid>
      <dc:creator>Zatere</dc:creator>
      <dc:date>2024-07-26T16:52:53Z</dc:date>
    </item>
    <item>
      <title>Re: Total sum before and after dates in a dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Total-sum-before-and-after-dates-in-a-dataset/m-p/937289#M368281</link>
      <description>&lt;P&gt;Make a view:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have2b / view=have2b;
merge
  have1 (in=h1 keep=id)
  have2
;
by id;
if h1;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The view is executed during the "want" DATA step, so it should not much affect the overall performance.&lt;/P&gt;</description>
      <pubDate>Fri, 26 Jul 2024 17:09:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Total-sum-before-and-after-dates-in-a-dataset/m-p/937289#M368281</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2024-07-26T17:09:25Z</dc:date>
    </item>
    <item>
      <title>Re: Total sum before and after dates in a dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Total-sum-before-and-after-dates-in-a-dataset/m-p/937290#M368282</link>
      <description>I just learnt something new!</description>
      <pubDate>Fri, 26 Jul 2024 17:15:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Total-sum-before-and-after-dates-in-a-dataset/m-p/937290#M368282</guid>
      <dc:creator>Zatere</dc:creator>
      <dc:date>2024-07-26T17:15:51Z</dc:date>
    </item>
  </channel>
</rss>

