<?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: sum of Z between date 1 and date2 in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/sum-of-Z-between-date-1-and-date2/m-p/728197#M226579</link>
    <description>&lt;P&gt;Update, for duplicated data in ttt1 and ttt2:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data ttt1;
format date1 date2 date9.;
input ID date1 :date9. date2 :date9.;
cards;
1 '06MAR2021'd '15MAR2021'd
1 '01MAR2021'd '05MAR2021'd
2 '08MAR2021'd '15MAR2021'd
;
Run;


data ttt2;
input ID date3 :date9. Z;
cards;
1 '28FEB2021'd 10
1 '02MAR2021'd 20
1 '04MAR2021'd  1
1 '04MAR2021'd 29
1 '06MAR2021'd 10
2 '08MAR2021'd 50
2 '14MAR2021'd 70
2 '18MAR2021'd 80
;
Run;

data WANT;

if 0 then set ttt1 ttt2;
declare hash H();
H.defineKey("ID","date3");
H.defineData("Z");
H.defineDone();

do until(end);
  set ttt2(rename=(z=z2)) end=end;
  if H.find() then z = z2;
    else z + z2;
  H.replace();
end;

do until(eof);
  set ttt1 end=eof;
  by ID;

  do date3 = date1 to date2;
    if H.find()=0 then sum + Z;
  end;

  if last.ID then
    do;
      output;
      sum = 0;
    end;
end;

drop date3 z:;
stop;
run;
proc print;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Bart&lt;/P&gt;</description>
    <pubDate>Mon, 22 Mar 2021 14:21:49 GMT</pubDate>
    <dc:creator>yabwon</dc:creator>
    <dc:date>2021-03-22T14:21:49Z</dc:date>
    <item>
      <title>sum of Z between date 1 and date2</title>
      <link>https://communities.sas.com/t5/SAS-Programming/sum-of-Z-between-date-1-and-date2/m-p/728168#M226574</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;I want to add a new column to data set "ttt1" that is called "Sum_Z_date1_till_date2".&lt;/P&gt;
&lt;P&gt;In this new column need to calculate sum of Z between date 1 and date 2.&lt;/P&gt;
&lt;P&gt;What is the way to do it please?&lt;/P&gt;
&lt;P&gt;Expected results is : for ID=1 get 50 and for ID=2 get 120&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data ttt1;
format date1 date2 date9.;
input ID date1 :date9. date2 :date9.;
cards;
1 '01MAR2021'd '05MAR2021'd
2 '08MAR2021'd '15MAR2021'd
;
Run;


data ttt2;
input ID date3 :date9. Z;
cards;
1 '28FEB2021'd 10
1 '02MAR2021'd 20
1 '04MAR2021'd 30
1 '06MAR2021'd 10
2 '08MAR2021'd 50
2 '14MAR2021'd 70
2 '18MAR2021'd 80
;
Run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 22 Mar 2021 13:11:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/sum-of-Z-between-date-1-and-date2/m-p/728168#M226574</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2021-03-22T13:11:57Z</dc:date>
    </item>
    <item>
      <title>Re: sum of Z between date 1 and date2</title>
      <link>https://communities.sas.com/t5/SAS-Programming/sum-of-Z-between-date-1-and-date2/m-p/728176#M226575</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data ttt1;
format date1 date2 date9.;
input ID date1 :date9. date2 :date9.;
cards;
1 01MAR2021 05MAR2021
2 08MAR2021 15MAR2021
;
Run;


data ttt2;
input ID date3 :date9. Z;
cards;
1 28FEB2021 10
1 02MAR2021 20
1 04MAR2021 30
1 06MAR2021 10
2 08MAR2021 50
2 14MAR2021 70
2 18MAR2021 80
;
Run;

proc sql;
    create table ttt3 as select
        a.*,sum(b.z*(a.date1&amp;lt;=b.date3&amp;lt;=a.date2)) as sum_z
    from ttt1 as a left join ttt2 as b on a.id=b.id 
    group by a.id
    having b.date3=max(b.date3);
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 22 Mar 2021 13:30:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/sum-of-Z-between-date-1-and-date2/m-p/728176#M226575</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2021-03-22T13:30:21Z</dc:date>
    </item>
    <item>
      <title>Re: sum of Z between date 1 and date2</title>
      <link>https://communities.sas.com/t5/SAS-Programming/sum-of-Z-between-date-1-and-date2/m-p/728184#M226576</link>
      <description>&lt;P&gt;Just for fun, with hash tables:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data ttt1;
format date1 date2 date9.;
input ID date1 :date9. date2 :date9.;
cards;
1 '01MAR2021'd '05MAR2021'd
2 '08MAR2021'd '15MAR2021'd
;
Run;


data ttt2;
input ID date3 :date9. Z;
cards;
1 '28FEB2021'd 10
1 '02MAR2021'd 20
1 '04MAR2021'd 30
1 '06MAR2021'd 10
2 '08MAR2021'd 50
2 '14MAR2021'd 70
2 '18MAR2021'd 80
;
Run;

data WANT;

if 0 then set ttt1 ttt2;
declare hash H(dataset: "ttt2");
H.defineKey("ID","date3");
H.defineData("Z");
H.defineDone();

do until(eof);
  set ttt1 end=eof;
  by ID;

  do date3 = date1 to date2;
    if H.find()=0 then sum + Z;
  end;

  if last.ID then
    do;
      output;
      sum = 0;
    end;
end;

drop date3 z;
stop;
run;
proc print;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Bart&lt;/P&gt;</description>
      <pubDate>Mon, 22 Mar 2021 13:51:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/sum-of-Z-between-date-1-and-date2/m-p/728184#M226576</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2021-03-22T13:51:23Z</dc:date>
    </item>
    <item>
      <title>Re: sum of Z between date 1 and date2</title>
      <link>https://communities.sas.com/t5/SAS-Programming/sum-of-Z-between-date-1-and-date2/m-p/728188#M226578</link>
      <description>&lt;P&gt;If TTT1 and TTT2 are both sorted by ID, and TTT1 has one record per id, then there is a straightforward DATA step solution:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want (drop=z date3);
  merge ttt2 ttt1;
  by id;
  if first.id then zsum=.;
  if (date1&amp;lt;=date3&amp;lt;=date2) then zsum+z;
  if last.id;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 22 Mar 2021 14:00:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/sum-of-Z-between-date-1-and-date2/m-p/728188#M226578</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2021-03-22T14:00:32Z</dc:date>
    </item>
    <item>
      <title>Re: sum of Z between date 1 and date2</title>
      <link>https://communities.sas.com/t5/SAS-Programming/sum-of-Z-between-date-1-and-date2/m-p/728197#M226579</link>
      <description>&lt;P&gt;Update, for duplicated data in ttt1 and ttt2:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data ttt1;
format date1 date2 date9.;
input ID date1 :date9. date2 :date9.;
cards;
1 '06MAR2021'd '15MAR2021'd
1 '01MAR2021'd '05MAR2021'd
2 '08MAR2021'd '15MAR2021'd
;
Run;


data ttt2;
input ID date3 :date9. Z;
cards;
1 '28FEB2021'd 10
1 '02MAR2021'd 20
1 '04MAR2021'd  1
1 '04MAR2021'd 29
1 '06MAR2021'd 10
2 '08MAR2021'd 50
2 '14MAR2021'd 70
2 '18MAR2021'd 80
;
Run;

data WANT;

if 0 then set ttt1 ttt2;
declare hash H();
H.defineKey("ID","date3");
H.defineData("Z");
H.defineDone();

do until(end);
  set ttt2(rename=(z=z2)) end=end;
  if H.find() then z = z2;
    else z + z2;
  H.replace();
end;

do until(eof);
  set ttt1 end=eof;
  by ID;

  do date3 = date1 to date2;
    if H.find()=0 then sum + Z;
  end;

  if last.ID then
    do;
      output;
      sum = 0;
    end;
end;

drop date3 z:;
stop;
run;
proc print;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Bart&lt;/P&gt;</description>
      <pubDate>Mon, 22 Mar 2021 14:21:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/sum-of-Z-between-date-1-and-date2/m-p/728197#M226579</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2021-03-22T14:21:49Z</dc:date>
    </item>
    <item>
      <title>Re: sum of Z between date 1 and date2</title>
      <link>https://communities.sas.com/t5/SAS-Programming/sum-of-Z-between-date-1-and-date2/m-p/728206#M226580</link>
      <description>&lt;P&gt;I'm a great enthusiast for the hash object, but allow me to propagate a "conditional set" apprauch to accommodate multiple date ranges per id:&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 ttt1;
format date1 date2 date9.;
input ID date1 :date9. date2 :date9.;
cards;
1 '01MAR2021'd '05MAR2021'd
1 '06MAR2021'd '15MAR2021'd
2 '08MAR2021'd '15MAR2021'd
;
Run;


data ttt2;
input ID date3 :date9. Z;
cards;
1 '28FEB2021'd 10
1 '02MAR2021'd 20
1 '04MAR2021'd  1
1 '04MAR2021'd 29
1 '06MAR2021'd 10
2 '08MAR2021'd 50
2 '14MAR2021'd 70
2 '18MAR2021'd 80
;
Run;

data want (drop=z date3);
  set ttt1 (keep=id date1 rename=(date1=date3) in=start)
      ttt2 
      ttt1 (rename=(date2=date3) in=end);
  by id date3;
  if start=1 then do;
    set ttt1;  /* Get DATE1 and DATE2, automatically retained*/
    zsum=.;
  end;
  else if (date1&amp;lt;=date3&amp;lt;=date2) then zsum+z;
  if end=1;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;This assumes that TTT1 is (1) sorted by ID/DATE1, and (2) has no intersecting date ranges.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The SET statement forces the DATE1 (renamed to DATE3) to appear in chronological order with all the DATE3 values in TTT2, followed by DATE2 (from TTT1 and renamed to DATE3).&amp;nbsp; &amp;nbsp;The &lt;EM&gt;&lt;STRONG&gt;conditional set&lt;/STRONG&gt;&lt;/EM&gt; statement (in the "if start" do group) reads in (unrenamed) DATE1 and DATE2.&amp;nbsp; These variables will be retained until the next conditional set statement, (i.e. throughout all the qualifying records in TTT2), thereby enabling the "if (date1&amp;lt;=date3&amp;lt;=date2) ..." test for accumulating ZSUM.&amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 22 Mar 2021 14:49:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/sum-of-Z-between-date-1-and-date2/m-p/728206#M226580</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2021-03-22T14:49:19Z</dc:date>
    </item>
    <item>
      <title>Re: sum of Z between date 1 and date2</title>
      <link>https://communities.sas.com/t5/SAS-Programming/sum-of-Z-between-date-1-and-date2/m-p/728211#M226581</link>
      <description>&lt;P&gt;Great code Mark! Beautiful use-case of interleaving.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In defence of hash approach, it requires only that ttt1 be sorted by ID, data ranges can overlap. And I just realised that hash aggregates on the ID level, not ID/date (but that's easy to fix by doing:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/*if last.ID then*/&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Bart&lt;/P&gt;</description>
      <pubDate>Mon, 22 Mar 2021 15:04:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/sum-of-Z-between-date-1-and-date2/m-p/728211#M226581</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2021-03-22T15:04:21Z</dc:date>
    </item>
    <item>
      <title>Re: sum of Z between date 1 and date2</title>
      <link>https://communities.sas.com/t5/SAS-Programming/sum-of-Z-between-date-1-and-date2/m-p/728226#M226584</link>
      <description>Why is it needed to write the statement&lt;BR /&gt; having b.date3=max(b.date3);?</description>
      <pubDate>Mon, 22 Mar 2021 16:41:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/sum-of-Z-between-date-1-and-date2/m-p/728226#M226584</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2021-03-22T16:41:49Z</dc:date>
    </item>
    <item>
      <title>Re: sum of Z between date 1 and date2</title>
      <link>https://communities.sas.com/t5/SAS-Programming/sum-of-Z-between-date-1-and-date2/m-p/728235#M226587</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159549"&gt;@Ronein&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;Why is it needed to write the statement&lt;BR /&gt;having b.date3=max(b.date3);?&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;So you get only one output record from SQL for each row of the table ttt1&lt;/P&gt;</description>
      <pubDate>Mon, 22 Mar 2021 17:37:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/sum-of-Z-between-date-1-and-date2/m-p/728235#M226587</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2021-03-22T17:37:51Z</dc:date>
    </item>
    <item>
      <title>Re: sum of Z between date 1 and date2</title>
      <link>https://communities.sas.com/t5/SAS-Programming/sum-of-Z-between-date-1-and-date2/m-p/728353#M226630</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;I run your code but results is not correct.&lt;/P&gt;
&lt;P&gt;Please compare results&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt; data ttt1;
format date1 date2 date9.;
input ID date1 :date9. date2 :date9.;
cards;
1 01MAR2021 05MAR2021
2 08MAR2021 15MAR2021
1 27FEB2021 28FEB2021
;
Run;
data ttt2;
format  date3 date9.;
input ID date3 :date9. Z;
cards;
1 28FEB2021 10
1 02MAR2021 20
1 04MAR2021 30
1 06MAR2021 10
2 08MAR2021 50
2 14MAR2021 70
2 18MAR2021 80
;
Run;

/**My solution*/
/*Cartesian product:Many to many merge*/
proc sql;
create table ttt3 as
select *,
      case when Date1&amp;lt;=Date3&amp;lt;=Date2 then 1 else 0 end as Indicator,
	  calculated Indicator*Z as Z_new
from ttt1 a
left join ttt2 b
on a.ID=b.ID
order by ID,date1,date2
;
quit;
proc sql;
create table wanted as
select ID,date1,date2,
sum(Z_new) as Z_new
from ttt3
group by Id,Date1,Date2;
quit;
 
/**PaigeMiller solution*/
proc sql;
    create table ttt3 as select
        a.*,sum(b.z*(a.date1&amp;lt;=b.date3&amp;lt;=a.date2)) as sum_z
    from ttt1 as a left join ttt2 as b on a.id=b.id 
    group by a.id
    having b.date3=max(b.date3);
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 23 Mar 2021 07:03:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/sum-of-Z-between-date-1-and-date2/m-p/728353#M226630</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2021-03-23T07:03:34Z</dc:date>
    </item>
    <item>
      <title>Re: sum of Z between date 1 and date2</title>
      <link>https://communities.sas.com/t5/SAS-Programming/sum-of-Z-between-date-1-and-date2/m-p/728391#M226645</link>
      <description>&lt;P&gt;This is easily fixed. The problem with a Cartesian join is that if you have large data sets, you will need a huge amount of time to process the Cartesian join.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data ttt1;
format date1 date2 date9.;
input ID date1 :date9. date2 :date9.;
n=_n_;
cards;
1 01MAR2021 05MAR2021
2 08MAR2021 15MAR2021
1 27FEB2021 28FEB2021
;
Run;
data ttt2;
format  date3 date9.;
input ID date3 :date9. Z;
cards;
1 28FEB2021 10
1 02MAR2021 20
1 04MAR2021 30
1 06MAR2021 10
2 08MAR2021 50
2 14MAR2021 70
2 18MAR2021 80
;
Run;

proc sql;
    create table ttt3 as select
        a.*,sum(b.z*(a.date1&amp;lt;=b.date3&amp;lt;=a.date2)) as sum_z
    from ttt1 as a left join ttt2 as b on a.id=b.id
    group by a.id,a.n 
    having b.date3=max(b.date3)
    order by a.n;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 23 Mar 2021 10:15:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/sum-of-Z-between-date-1-and-date2/m-p/728391#M226645</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2021-03-23T10:15:39Z</dc:date>
    </item>
    <item>
      <title>Re: sum of Z between date 1 and date2</title>
      <link>https://communities.sas.com/t5/SAS-Programming/sum-of-Z-between-date-1-and-date2/m-p/728398#M226648</link>
      <description>&lt;P&gt;I would move the date condition into the ON of the LEFT JOIN:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table want as
  select
    t1.id,
    t1.date1,
    t1.date2,
    sum(t2.z) as sum_z
  from ttt1 t1 left join ttt2 t2
  on t1.id=t2.id and t1.date1 le t2.date3 le t1.date2
  group by t1.id, t1.date1, t1.date2
;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 23 Mar 2021 10:52:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/sum-of-Z-between-date-1-and-date2/m-p/728398#M226648</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2021-03-23T10:52:40Z</dc:date>
    </item>
    <item>
      <title>Re: sum of Z between date 1 and date2</title>
      <link>https://communities.sas.com/t5/SAS-Programming/sum-of-Z-between-date-1-and-date2/m-p/728406#M226653</link>
      <description>&lt;PRE&gt;data ttt1;
format date1 date2 date9.;
input ID date1 :date9. date2 :date9.;
format date1 date2 date9.;
cards;
1 '01MAR2021'd '05MAR2021'd
2 '08MAR2021'd '15MAR2021'd
;
Run;


data ttt2;
input ID date3 :date9. Z;
format date3 date9.;
cards;
1 '28FEB2021'd 10
1 '02MAR2021'd 20
1 '04MAR2021'd 30
1 '06MAR2021'd 10
2 '08MAR2021'd 50
2 '14MAR2021'd 70
2 '18MAR2021'd 80
;
Run;

proc summary data=ttt2 nway;
class id date3;
var z;
output out=sum(drop=_:) sum=;
run;
data want;
 if _n_=1 then do;
   if 0 then set sum;
   declare hash h(dataset:'sum',hashexp:20);
   h.definekey('id','date3');
   h.definedata('z');
   h.definedone();
 end;
set ttt1;
sum=0;
do temp=date1 to date2;
 if h.find(key:id,key:temp)=0 then sum+z;
end;
drop temp z date3;
run;
&lt;/PRE&gt;</description>
      <pubDate>Tue, 23 Mar 2021 11:35:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/sum-of-Z-between-date-1-and-date2/m-p/728406#M226653</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2021-03-23T11:35:13Z</dc:date>
    </item>
    <item>
      <title>Re: sum of Z between date 1 and date2</title>
      <link>https://communities.sas.com/t5/SAS-Programming/sum-of-Z-between-date-1-and-date2/m-p/728535#M226686</link>
      <description>&lt;P&gt;&amp;nbsp;Thanks&lt;/P&gt;</description>
      <pubDate>Tue, 23 Mar 2021 19:54:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/sum-of-Z-between-date-1-and-date2/m-p/728535#M226686</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2021-03-23T19:54:17Z</dc:date>
    </item>
  </channel>
</rss>

