BookmarkSubscribeRSS Feed
deleted_user
Not applicable
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.
2 REPLIES 2
sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10
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_;
run;


Scott Barry
SBBWorks, Inc.
RichardH_sas
SAS Employee
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:

http://support.sas.com/documentation/cdl/en/lrdict/59540/HTML/default/a003154994.htm

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.

Best,

Richard

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

Creating Custom Steps in SAS Studio

Check out this tutorial series to learn how to build your own steps in SAS Studio.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 2227 views
  • 0 likes
  • 3 in conversation