<?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 values based on date range created off of the min date in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Sum-of-values-based-on-date-range-created-off-of-the-min-date/m-p/348113#M80566</link>
    <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input Customer $ Home     Car     Boat     DateSold : mmddyy10.;
format datesold mmddyy10.;
cards;
123A         0             1         0          3/30/2017
123A         1             0         0          4/2/2017
456B         0             1         0          3/28/2017
456B         1             0         0          3/28/2017
456B         0             0         1          4/7/2017
;
run;


proc sql;
select customer,
(select count(*) from have where customer=a.customer and 
 datesold between a.datesold and a.datesold+7) as total
 from 
(
select distinct customer,datesold 
 from have
  group by customer
   having datesold=min(datesold)
) as a;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Fri, 07 Apr 2017 14:14:49 GMT</pubDate>
    <dc:creator>Ksharp</dc:creator>
    <dc:date>2017-04-07T14:14:49Z</dc:date>
    <item>
      <title>Sum of values based on date range created off of the min date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sum-of-values-based-on-date-range-created-off-of-the-min-date/m-p/348080#M80554</link>
      <description>&lt;P&gt;I am trying to sum sales&amp;nbsp;for customers&amp;nbsp;when the date range falls between the min date of the first sale and any sales that happened within 7 days from the min date. &amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;For example:&lt;/P&gt;&lt;P&gt;Customer &amp;nbsp;Home &amp;nbsp; &amp;nbsp; Car &amp;nbsp; &amp;nbsp; Boat &amp;nbsp; &amp;nbsp; DateSold&lt;/P&gt;&lt;P&gt;123A &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 0 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 0 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;3/30/2017&lt;/P&gt;&lt;P&gt;123A &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 0 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 0 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;4/2/2017&lt;/P&gt;&lt;P&gt;456B &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 0 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 0 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;3/28/2017&lt;/P&gt;&lt;P&gt;456B &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 0 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 0 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;3/28/2017&lt;/P&gt;&lt;P&gt;456B &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 0 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 0 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;4/7/2017&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I want the&amp;nbsp;end result to look like:&lt;/P&gt;&lt;P&gt;Customer &amp;nbsp; &amp;nbsp; Total&lt;/P&gt;&lt;P&gt;123A &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 2 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;456B &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 2 &amp;nbsp; &amp;nbsp; **I do not want to count the Boat on 4/7 because it was sold more than 7 days from the min date for this customer&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have tried:&lt;/P&gt;&lt;P&gt;proc sql;&lt;BR /&gt;create table TEST as&lt;BR /&gt;select&lt;BR /&gt;Customer,&lt;BR /&gt;MIN(DateBound) as MinDateBound,&lt;BR /&gt;SUM(boat + home + car) as PoliciesSold&lt;BR /&gt;from&lt;BR /&gt;Database_Test&lt;BR /&gt;where&lt;BR /&gt;PolicyStatus_ID in (1,2) and DateBound &amp;gt; '27MAR2017'd&lt;BR /&gt;group by&lt;BR /&gt;Customer_ID&lt;BR /&gt;order by Customer&lt;BR /&gt;;&lt;BR /&gt;quit;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This query works great ... except for the 'only include if within 7 days filter'.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 07 Apr 2017 13:26:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sum-of-values-based-on-date-range-created-off-of-the-min-date/m-p/348080#M80554</guid>
      <dc:creator>Roddy</dc:creator>
      <dc:date>2017-04-07T13:26:40Z</dc:date>
    </item>
    <item>
      <title>Re: Sum of values based on date range created off of the min date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sum-of-values-based-on-date-range-created-off-of-the-min-date/m-p/348085#M80557</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You have not posted any test data in the form of a datastep, therefore this is just a guesss:&lt;/P&gt;
&lt;PRE&gt;data want;
  set have;
  retain fst_date total;
  by customer;
  if first.customer then do;
    fst_date=date_sold;
    total=0;
  end;
  if date_sold &amp;lt;= fst_date + 7 then total=total+sum(home,car,boot);
  if last.customer then output;
run; &lt;/PRE&gt;</description>
      <pubDate>Fri, 07 Apr 2017 13:30:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sum-of-values-based-on-date-range-created-off-of-the-min-date/m-p/348085#M80557</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2017-04-07T13:30:54Z</dc:date>
    </item>
    <item>
      <title>Re: Sum of values based on date range created off of the min date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sum-of-values-based-on-date-range-created-off-of-the-min-date/m-p/348088#M80558</link>
      <description>&lt;P&gt;If you want two passes of the data calculate the min per customer and then use&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;where date between minDate and minDate+6&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;or you could try the following in your current query, minus your original logic for dates.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;having date between min(date) and min(date) + 6&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;BETWEEN includes both ends of an interval which is why I've used 6 in above samples.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 07 Apr 2017 13:32:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sum-of-values-based-on-date-range-created-off-of-the-min-date/m-p/348088#M80558</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2017-04-07T13:32:21Z</dc:date>
    </item>
    <item>
      <title>Re: Sum of values based on date range created off of the min date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sum-of-values-based-on-date-range-created-off-of-the-min-date/m-p/348113#M80566</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input Customer $ Home     Car     Boat     DateSold : mmddyy10.;
format datesold mmddyy10.;
cards;
123A         0             1         0          3/30/2017
123A         1             0         0          4/2/2017
456B         0             1         0          3/28/2017
456B         1             0         0          3/28/2017
456B         0             0         1          4/7/2017
;
run;


proc sql;
select customer,
(select count(*) from have where customer=a.customer and 
 datesold between a.datesold and a.datesold+7) as total
 from 
(
select distinct customer,datesold 
 from have
  group by customer
   having datesold=min(datesold)
) as a;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 07 Apr 2017 14:14:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sum-of-values-based-on-date-range-created-off-of-the-min-date/m-p/348113#M80566</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2017-04-07T14:14:49Z</dc:date>
    </item>
  </channel>
</rss>

