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

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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