Page **1** of **1**

### Week number code

Posted: **Fri Mar 22, 2019 11:03 am**

by **BT454**

Good afternoon,

I'm attempting to write some code to compare weekly frequency data, over the span of several years (i.e .comparing the third week of data for year x, to the third week in year y, to the third week in year z). I thought about just using lags of (-52), but some years have 53 weeks.

Is there a way to do this?

Thanks,

Bob

### Re: Week number code

Posted: **Fri Mar 22, 2019 11:16 am**

by **EViews Gareth**

Welcome to the horrors of working with weekly data.

If you can describe mathematically how you'd like to do the comparisons, we can probably tell you how to do it in EViews.

### Re: Week number code

Posted: **Fri Mar 22, 2019 11:28 am**

by **BT454**

Thank you very much in advance for the help.

For starters, I need to create 5 year average series for each week number of the year.

I.e. lagging five year average of series x, in week 10 of 2019 = (x_week10 _2018 + x_week10_2017 + x_week10_2016 + x_week10_2015 + x_week10_2014)/5

Does that help clarify?

I thought about creating a series that assigns the week number to the week ending date using excel, and importing it to eviews, but I'm not sure how I'd use it to cross reference against other series.

Thanks again,

Bob

### Re: Week number code

Posted: **Fri Mar 22, 2019 11:36 am**

by **EViews Gareth**

But how do you define a week number?

What is week 10 in 2019? What is week 10 in 2018? Etc...

### Re: Week number code

Posted: **Fri Mar 22, 2019 11:58 am**

by **BT454**

Good question.

This actually differs depending on what the data is, but for this example we'd focus on week ending Saturday (some other data is week ending Friday).

i.e. week 10 in 2019 ends Saturday March, 9, so Sunday March 3 - Saturday March 9 would all be days included in week 10 of 2019.

In 2018 week 10 would start on Sunday, March 4 and run through Saturday, March 10.

Does this help?

Thanks,

Bob

### Re: Week number code

Posted: **Fri Mar 22, 2019 3:09 pm**

by **EViews Matt**

Hello,

Since you have weekly data (dated I assume), for practical purpose is not the simple definition that week 1 is the first observation of the year, week 2 is the second observation of the year, etc., sufficient? This definition matches how EViews displays the date for each week by default (the earliest date within the week represented by the observation, implicitly interpreting "... observation of the year" to mean "... observation beginning in the year").

### Re: Week number code

Posted: **Sat Mar 23, 2019 6:35 am**

by **BT454**

yes, that is correct. Week one is the first week, week two is the second week, so on and so forth. Where those weeks start and end are at times important (although it may not matter right now, but for instance NOAA HDD/CDD weekly data has a week ending on Saturday, EIA weekly storage data has a week ending on Friday morning, but as long as I can pull that weekly data into eviews and have it be able to recognize which week of the year it is (1-52, or 53 in certain instances) that is at least enough to get up and running.

Thank you very much,

Bob

### Re: Week number code

Posted: **Mon Mar 25, 2019 10:39 am**

by **EViews Matt**

How about this:

Code: Select all

`series weekid = @ceiling(@datepart(@date, "ddd") / 7)`

series oneyearlag = -@ceiling(@datepart(@dateadd(@date, -weekid, "w"), "ddd") / 7)

series twoyearlag = oneyearlag + oneyearlag(oneyearlag)

series threeyearlag = twoyearlag + oneyearlag(twoyearlag)

series fouryearlag = threeyearlag + oneyearlag(threeyearlag)

You can then calculate a five-year average for all* weeks as:

Code: Select all

`series y = (x + x(oneyearlag) + x(twoyearlag) + x(threeyearlag) + x(fouryearlag)) / 5`

*The one caveat is that the above doesn't work for the 53rd week of the year, but that would need to be a special case anyway.

### Re: Week number code

Posted: **Mon Mar 25, 2019 2:14 pm**

by **BT454**

Thanks!

It looks like this did the job quite well.

Bob

### Re: Week number code

Posted: **Tue Mar 26, 2019 1:27 pm**

by **BT454**

I was wrong about that. Never mind my previous post, and thanks again!