<?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:multiple rows per ID in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/sum-of-Z-between-date-1-and-date2-multiple-rows-per-ID/m-p/728236#M226588</link>
    <description>&lt;P&gt;I think my solution in the other thread works in this case. Please try it.&lt;/P&gt;</description>
    <pubDate>Mon, 22 Mar 2021 17:38:54 GMT</pubDate>
    <dc:creator>PaigeMiller</dc:creator>
    <dc:date>2021-03-22T17:38:54Z</dc:date>
    <item>
      <title>sum of Z between date 1 and date2:multiple rows per ID</title>
      <link>https://communities.sas.com/t5/SAS-Programming/sum-of-Z-between-date-1-and-date2-multiple-rows-per-ID/m-p/728216#M226583</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;EM&gt;Continue&lt;/EM&gt;&lt;SPAN&gt;&amp;nbsp;from the&amp;nbsp;&lt;/SPAN&gt;&lt;EM&gt;previous question, now there can be multiple rows for each customer in data set ttt1.&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;&lt;EM&gt;Which solution will work now?&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;&lt;EM&gt;Remind that the task is to&amp;nbsp;&lt;/EM&gt;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;Expected results is :&lt;/P&gt;
&lt;P&gt;for ID=1 &lt;CODE class=" language-sas"&gt;01MAR2021 05MAR2021 &lt;/CODE&gt;: 50&lt;/P&gt;
&lt;P&gt;for ID=1 &lt;CODE class=" language-sas"&gt;27FEB2021 28FEB2021&lt;/CODE&gt;&lt;CODE class=" language-sas"&gt;&lt;/CODE&gt;: 10&lt;/P&gt;
&lt;P&gt;for ID=2 &lt;CODE class=" language-sas"&gt; 08MAR2021 15MAR2021L&amp;nbsp;120&lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;What do you think about the following solution?&lt;/P&gt;
&lt;P&gt;will it work well?&lt;/P&gt;
&lt;P&gt;Wil it be usefull code?(less time of running)&lt;/P&gt;
&lt;P&gt;It is based on many to many merge using&amp;nbsp;&lt;SPAN&gt;Cartesian product (&lt;STRONG&gt;creates all possible combinations of matching observations)&lt;/STRONG&gt;&lt;/SPAN&gt;&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;
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;

/*Cartesian product:Many to many merge*/
proc sql;
create table ttt3 as
select *
from ttt1 a
join ttt2 b
on a.ID=b.ID;
quit;

Data ttt4;
set ttt3;
Indicator=(Date1&amp;lt;=Date3&amp;lt;=Date2);
Run;

proc sql;
create table ttt5 as
select ID,date1,date2,
sum(z*Indicator)
from ttt3
group by Id,Date1,Date2;
quit;

proc sort data=ttt1;by id date1 date2;run;
proc sort data=ttt5;by id date1 date2;run;
Data wanted;
merge ttt1(in=a) ttt5(in=b);
By id date1 date2;
IF a;
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>Mon, 22 Mar 2021 16:34:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/sum-of-Z-between-date-1-and-date2-multiple-rows-per-ID/m-p/728216#M226583</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2021-03-22T16:34:47Z</dc:date>
    </item>
    <item>
      <title>Re: sum of Z between date 1 and date2:multiple rows per ID</title>
      <link>https://communities.sas.com/t5/SAS-Programming/sum-of-Z-between-date-1-and-date2-multiple-rows-per-ID/m-p/728236#M226588</link>
      <description>&lt;P&gt;I think my solution in the other thread works in this case. Please try it.&lt;/P&gt;</description>
      <pubDate>Mon, 22 Mar 2021 17:38:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/sum-of-Z-between-date-1-and-date2-multiple-rows-per-ID/m-p/728236#M226588</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2021-03-22T17:38:54Z</dc:date>
    </item>
  </channel>
</rss>

