<?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: Selecting sum of values from the last week of date from a date in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/Selecting-sum-of-values-from-the-last-week-of-date-from-a-date/m-p/593998#M15605</link>
    <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Question: are you asking for 7-day rolling sums?&amp;nbsp; I notice that you have some missing dates in the time frame, but that can still be accommodated in a data step.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I'm not going to transcribe test data you provide in a picture instead of as text, so this program is untested.&amp;nbsp; It assumes your data set (named HAVE below) is sorted by date, starts at Jan 1, 2019, and can have missing SEND_DATES:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data dummy_dates;  
  total_opens=0;
  do send_date='01jan2019'd to today();
run;

data want;
  merge dummy_dates have;
  by send_date;

  array opens {0:6} _temporary_ (7*0);
  retain opens_rolling_n 0;

  i=mod(date,7);

  opens_rolling_n=opens_rolling_n-opens{i}+total_opens;
  opens{i}=coalesce(total_opens,0);
run;
  
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The technique here is to make a dummy dataset of all possible dates in your time frame, and assign TOTAL_OPEN=0 for each of those dates.&amp;nbsp; Then merge this dataset (DUMMY_DATES) with HAVE by SENT_DATE.&amp;nbsp; Any time a send_date is missing in HAVE, it is added with total_opens=0.&amp;nbsp; When a send_date is present in both datasets, the value in HAVE prevails (because it is the rightmost dataset in the MERGE statement).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The program has an array for the 7 most recent values of total_open.&amp;nbsp; A running OPENS_ROLLING_N is updated by adding in the current and subtracting out the 7-day old value (found in the array).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you want to avoid having less than 7 day total in the first 6 observations, then:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want (drop=_:);
  merge dummy_dates have;
  by send_date;

  array opens {0:6} _temporary_ (7*0);
  retain _opens 0;

  i=mod(date,7);

  _opens=_opens-opens{i}+total_opens;
  opens{i}=coalesce(total_opens,0);

  if _n_&amp;gt;6 then opens_rolling_n=_opens;
run;
  &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 04 Oct 2019 01:08:32 GMT</pubDate>
    <dc:creator>mkeintz</dc:creator>
    <dc:date>2019-10-04T01:08:32Z</dc:date>
    <item>
      <title>Selecting sum of values from the last week of date from a date</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Selecting-sum-of-values-from-the-last-week-of-date-from-a-date/m-p/593973#M15597</link>
      <description>&lt;P&gt;Hi all,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I've rolled up email data at a date level for the year and I want to add a couple columns showing from each date, how many recipients or emails there were in the past week and I would like to write this out as a selection. The sample data set is attached, and you you look at it, I'd be looking to add a column where on 01/10/19 the value showing the total communications in the past 7 days would equal 6 total communications in the past week.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks for your help and let me know if you need further clarification.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="capture.jpg" style="width: 557px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/32954i4A61886B4EF8DA6C/image-size/large?v=v2&amp;amp;px=999" role="button" title="capture.jpg" alt="capture.jpg" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 03 Oct 2019 22:29:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Selecting-sum-of-values-from-the-last-week-of-date-from-a-date/m-p/593973#M15597</guid>
      <dc:creator>Dogo23</dc:creator>
      <dc:date>2019-10-03T22:29:43Z</dc:date>
    </item>
    <item>
      <title>Re: Selecting sum of values from the last week of date from a date</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Selecting-sum-of-values-from-the-last-week-of-date-from-a-date/m-p/593976#M15598</link>
      <description>Can you show what you expect as output please?&lt;BR /&gt;Please post data as text, not an image. &lt;BR /&gt;&lt;BR /&gt;Otherwise, you're likely looking for PROC MEANS with a format on data and possibly a WHERE statement to filter your data dynamically.</description>
      <pubDate>Thu, 03 Oct 2019 22:37:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Selecting-sum-of-values-from-the-last-week-of-date-from-a-date/m-p/593976#M15598</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2019-10-03T22:37:40Z</dc:date>
    </item>
    <item>
      <title>Re: Selecting sum of values from the last week of date from a date</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Selecting-sum-of-values-from-the-last-week-of-date-from-a-date/m-p/593994#M15604</link>
      <description>&lt;P&gt;The way I would do it is using a proc sql (which a lot of people suggest not to) and do a self join.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;if b.date in between a.date-7 (use intnx) and a.date then sum(comms) and group by a.date.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 04 Oct 2019 00:36:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Selecting-sum-of-values-from-the-last-week-of-date-from-a-date/m-p/593994#M15604</guid>
      <dc:creator>Ramakanthkrovi</dc:creator>
      <dc:date>2019-10-04T00:36:14Z</dc:date>
    </item>
    <item>
      <title>Re: Selecting sum of values from the last week of date from a date</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Selecting-sum-of-values-from-the-last-week-of-date-from-a-date/m-p/593998#M15605</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Question: are you asking for 7-day rolling sums?&amp;nbsp; I notice that you have some missing dates in the time frame, but that can still be accommodated in a data step.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I'm not going to transcribe test data you provide in a picture instead of as text, so this program is untested.&amp;nbsp; It assumes your data set (named HAVE below) is sorted by date, starts at Jan 1, 2019, and can have missing SEND_DATES:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data dummy_dates;  
  total_opens=0;
  do send_date='01jan2019'd to today();
run;

data want;
  merge dummy_dates have;
  by send_date;

  array opens {0:6} _temporary_ (7*0);
  retain opens_rolling_n 0;

  i=mod(date,7);

  opens_rolling_n=opens_rolling_n-opens{i}+total_opens;
  opens{i}=coalesce(total_opens,0);
run;
  
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The technique here is to make a dummy dataset of all possible dates in your time frame, and assign TOTAL_OPEN=0 for each of those dates.&amp;nbsp; Then merge this dataset (DUMMY_DATES) with HAVE by SENT_DATE.&amp;nbsp; Any time a send_date is missing in HAVE, it is added with total_opens=0.&amp;nbsp; When a send_date is present in both datasets, the value in HAVE prevails (because it is the rightmost dataset in the MERGE statement).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The program has an array for the 7 most recent values of total_open.&amp;nbsp; A running OPENS_ROLLING_N is updated by adding in the current and subtracting out the 7-day old value (found in the array).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you want to avoid having less than 7 day total in the first 6 observations, then:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want (drop=_:);
  merge dummy_dates have;
  by send_date;

  array opens {0:6} _temporary_ (7*0);
  retain _opens 0;

  i=mod(date,7);

  _opens=_opens-opens{i}+total_opens;
  opens{i}=coalesce(total_opens,0);

  if _n_&amp;gt;6 then opens_rolling_n=_opens;
run;
  &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 04 Oct 2019 01:08:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Selecting-sum-of-values-from-the-last-week-of-date-from-a-date/m-p/593998#M15605</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2019-10-04T01:08:32Z</dc:date>
    </item>
  </channel>
</rss>

