Desktop productivity for business analysts and programmers

Sum daily values by rolling 52 weekly periods

Not applicable
Posts: 0

Sum daily values by rolling 52 weekly periods

This seems like it should be fairly easy but I am not having much luck in EG. I have a table with multiple columns and a new row/record for each day. I would like to sum the daily columns values by weeks. I would also like to have the weeks roll on a 52 week basis. Any suggestions would be welcome.
Super Contributor
Super Contributor
Posts: 3,176

Re: Sum daily values by rolling 52 weekly periods

Posted in reply to deleted_user
INTNX function can be used to derive a "week start date" by/group variable and also a "52-week period start date". Here is some sample code for generating the 52-week start date value using today's date as the INTNX argument-1:

data _null_;
date_value = today();
wk52pd_start_date = intnx('week',date_value,-52);
format date_value wk52pd_start_date date9. ;
put _all_;

Scott Barry
SBBWorks, Inc.
SAS Employee
Posts: 149

Re: Sum daily values by rolling 52 weekly periods

Posted in reply to deleted_user
From your post, I take it your data looks roughly like:

SaleDate SaleAmt
01Apr2008 $15
02Apr2008 $20
01May2008 $10

If I'm on the right track, here's a sketch of one way you could tackle this:

1. If you want totals by week, you can get that using a computed column and some summarization in the Filter and Query. Using the above data, here's what I'd do. Create a computed column -- say, Week_Number -- with Build Expression. Expression should look something like week(aprsales.SaleDate ). The WEEK function is new to SAS 9, so you won't see it in the list of EG functions but you can still type it into the expression (assuming you're running SAS 9). More documentation on the function is here:

To be on the safe side, create a second new column from an expression -- Year -- like year(aprsales.SaleDate ).

2. Once you have the computed columns Week_Number and Year, then get Week_Number, Year, and SaleAmt in the Select Data. Change Summarization on SaleAmt to SUM. In Summary Groups below the Select Data, click Edit Summary Groups and move Year and Week_Number into the Group By.

When you run the task, that should give you totals by year and week for all your data. To get the rolling 52 week behavior, that would be some sort of filter based on dates. Scott's code could be a useful technique here, along with something like a TODAY function in the filter to automate getting the appropriate 52-week window.


Ask a Question
Discussion stats
  • 2 replies
  • 3 in conversation