turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

Find a Community

- Home
- /
- BI
- /
- Enterprise Guide
- /
- Moving Average

Topic Options

- RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

03-27-2017 06:52 AM - edited 03-27-2017 06:58 AM

Hello,

I am attempting to create a moving average of a time frame of certain days in a week and for 1 week either side of my day of interrogation.

For example, I have a measure for this wednesday. I want to take an average of the wednesday before, this wednesday and next wednesday ONLY if it meets a certain criteria.

I have data for all weekdays which also need to be averaged.

Is the best way forward to do an IF ELSE statement to filter for certain weekdays then embed some sort of average calculation in the same statement?

My table has the dates and the measures are such and I need to calculate the average like so:

Date Measure Average

Monday 1st 10 13.3

Tuesday 2nd 15 13.3

Wednesday 3rd 10 11.6

Thursday 4th 20 20

Friday 5th 10 18.3

Saturday 6th 15 11.6

Sunday 7th 10 13.3

Monday 8th 20 13.3

Tuesday 9th 15 13.3

Wednesday 10th 10 11.6

Thursday 11th 20 20

Friday 12th 20 18.3

Saturday 13th 10 11.6

Sunday 14th 15 13.3

Monday 15th 10 13.3

Tuesday 16th 10 13.3

Wednesday 17th 15 11.6

Thursday 18th 20 20

Friday 19th 25 18.3

Saturday 20th 10 11.6

Sunday 21st 15 13.3

I am used to the query builder or I can write out the code, I just need some guidance please on the method.

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to udaypatel1

03-27-2017 07:48 AM

You have too many vague questions at once. Try explaining one of the calculations first.

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to Reeza

03-27-2017 08:09 AM

Hi, thanks for your response.

Imagine I have 3 weeks of data, Mon-Sun.

I want to average individual days. Mondays, Tuesdays, Wednesdays etc.

Now I also want to be able to selectively exclude certain weekdays if they do not meet a certain criteria. I will focus on averaging all the days first though, then work on the exclusion criteria.

Should I split the weekdays into individual data sets, order them, apply a lag function and average it?

Or is there another quicker method to do this?

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to udaypatel1

03-27-2017 08:47 AM

Use a query builder or summary task.

In query builder add all columns to select list, create two computed columns, one that calculates the weekday (weekday function) and one that calculates average of the variable you want. Use the calculated weekday column in the GROUP BY section.

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to udaypatel1

03-27-2017 08:48 AM

PS by definition this is not a moving average.

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to Reeza

03-27-2017 09:55 AM

Hi Reeza,

Thank you for your answer. The moving average is still required as I need to be able to select just 3 weeks at a time (current week and weeks either side).

I have years of dates which I need to perform this on. I can do the average pretty easily as you had suggested, it was more a case of selecting only a handful of neighbouring weeks.

If you could think of anything that could help Im willing to try it.

I am attempting to use the example in this paper: http://www2.sas.com/proceedings/forum2008/093-2008.pdf

Thanks!

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to udaypatel1

03-27-2017 02:44 PM

Look at proc expand then.

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to Reeza

03-27-2017 03:03 PM

There's a great blog post by @Rick_SAS: How to compute a moving average with SAS.

If you want to include just certain days, or classify by day of the week -- then you might need to calc some additional classifier variables based on the date field that you've included.

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to ChrisHemedinger

04-11-2017 09:03 AM

Hi,

Thanks for that. I have tried to apply the same using SAS EG but I do not think it is entirely possible.

I think the proc does not work in EG. Is there a workaround for this proc in SAS EG?

Kind Regards,

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to udaypatel1

04-11-2017 09:06 AM

You need a SAS/ETS license for PROC EXPAND and it is available in EG, if you have the license.

If you don't then try the temporary array method:

GitHub is where people build software. More than 28 million people use GitHub to discover, fork, and contribute to over 85 million projects.

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to Reeza

04-12-2017 06:52 AM

Hi Reeza,

thank you for the suggestion.

I have attempted to use the script as posted:

data want;

array p{0:3} _temporary_;

set have; by object;

if first.object then call missing(of p{*});

p{mod(_n_,4)} = price;

lowest = min(of p{*});

highest = max(of p{*});

run;

In the 3rd line, would I be replacing 'object' with a column in my data set which is numerically ordered such as a date?