Weeks Dummies

For questions regarding programming in the EViews programming language.

Moderators: EViews Gareth, EViews Jason, EViews Moderator, EViews Matt

tchaithonov
Posts: 168
Joined: Mon Apr 13, 2009 7:39 am
Location: New York City

Weeks Dummies

Postby tchaithonov » Thu Sep 16, 2010 3:46 pm

Hi Guys,

I am writing a short program that creates dummies for each week of the year (so, from wk1 to wk52) and it looks like the following:
if @isobject("week") = 0 then
series week = @date
endif

if @isobject("gp_weeks") = 1 then
delete gp_weeks
endif

group gp_weeks @expand(@recode(@datepart(week, "ww")=53, 1,@datepart(week, "ww")))

for !i = 1 to gp_weeks.@count
if !i < 10 then
%week = "week0" + @str(!i)
else
%week = "week" + @str(!i)
endif
%week2 = gp_weeks.@seriesname(!i)
series dum_{%week} = {%week2}
next !i


Note that there will be a week 53 for year 2007 and in the program I add 1 if it is week 53. What I usually do in Excel is to do this:
=if(weeknum($A100)=53, 1, weeknum($A100))


I compare the results from Eviews against that from Excel and found out that, starting from 1/7/2008 (week 1 according to Eviews and week 2 according to Excel), the numbers begin to diverge. Is there a way to fix this? Please let me know. Thanks.

Tchaithonv

EViews Gareth
Fe ddaethom, fe welon, fe amcangyfrifon
Posts: 13323
Joined: Tue Sep 16, 2008 5:38 pm

Re: Weeks Dummies

Postby EViews Gareth » Thu Sep 16, 2010 4:23 pm

I'm having a really hard time understanding what you're doing here.

How are you defining a week?
Follow us on Twitter @IHSEViews

tchaithonov
Posts: 168
Joined: Mon Apr 13, 2009 7:39 am
Location: New York City

Re: Weeks Dummies

Postby tchaithonov » Fri Sep 17, 2010 6:52 am

Hey Gareth,

So, the straightforward answer is that I am trying to create 52 dummies representing each of the 52 weeks of a year. But as you know, there is a 53rd week for each 7 year, and as a result, what my code should do is to move that 53rd week into week 1 (as in the 53rd week of 2007). Excel identifies the week of 1/7/2008 as week 2, but Eviews identifies it as week 1; in that case I would have two Week 1 for 2008. I wonder if there is a way to line up the Eviews result with Excel's? Please let me know.

Tchaithonov

EViews Gareth
Fe ddaethom, fe welon, fe amcangyfrifon
Posts: 13323
Joined: Tue Sep 16, 2008 5:38 pm

Re: Weeks Dummies

Postby EViews Gareth » Fri Sep 17, 2010 8:28 am

I guess the issue is that it depends on how you (or Excel) are defining a week. If you define a week as being a 7 day period starting on a Monday (or a Sunday, for that matter), then the week beginning 1/7/2008 is the first week of the year.

I'll ask Chris (our dates guru) to contemplate it though...
Follow us on Twitter @IHSEViews

EViews Chris
EViews Developer
Posts: 161
Joined: Wed Sep 17, 2008 10:39 am

Re: Weeks Dummies

Postby EViews Chris » Fri Sep 17, 2010 10:15 am

Unfortunately, there are a lot of different ways to define the 'week in year' for a day.

EViews uses a very simple rule: the first seven days of a year are week 1, the second seven days of a year are week 2, etc. etc.

You can reproduce this manually using the following:

Code: Select all

series ww = @ceil((@date - @datefloor(@date, "year") + 1)/7)


Excel appears to use a different rule, which is something like this: the week number is one plus the number of Sundays after the start of the year and before this date (depending on international settings). You can calculate this in EViews as follows:

Code: Select all

series wxl =  @ceil((@date - @datefloor(@date, "year") + @datepart(@datefloor(@date, "year"),"w")) /7)


This looks indecipherable, but it is pretty much the same expression as the one above with an adjustment for the weekday of the first day of the year.

Note that there is an international standard (defined in ISO 8601) that is different from both of these definitions. It defines the first week of the year as the week that contains the first Thursday of the year. EViews supports this (although I'm not sure we've documented it) with the date part "wi". Note that in the ISO 8601 definition, the first and last days of a year may be within a week that belongs to a different year. There is a datepart "yyyi" in EViews that returns the year that matches the ISO week definition.

EViews Chris
EViews Developer
Posts: 161
Joined: Wed Sep 17, 2008 10:39 am

Re: Weeks Dummies

Postby EViews Chris » Fri Sep 17, 2010 2:17 pm

Just to clarify on the Excel issue...

The formula I gave above will reproduce Excel's weeknum() function with the second argument set to 2 (week begins on Monday). This matches the definition used by EViews for day of the week, so it makes the calculation simpler.

To match the Excel weeknum() function with the second argument set to 1 (week begins on Sunday), you can use the same basic calculation, but you need to change the day of week numbering to Sunday=1, Monday=2, etc.

Here's the calculation in two lines:

Code: Select all

series wadj = @datepart(@datefloor(@date, "year"),"w")
series wxl1 =  @ceil((@date - @datefloor(@date, "year") + @iif(wadj<7, wadj+1, 1)) /7)


If you only provide one argument to Excel's Weeknum() function, I believe Excel sets the value of the second argument based on where you live (i.e. based on the locale settings of your computer).

tchaithonov
Posts: 168
Joined: Mon Apr 13, 2009 7:39 am
Location: New York City

Re: Weeks Dummies

Postby tchaithonov » Fri Sep 17, 2010 2:39 pm

Thanks Chris & Gareth,

I was about to say that I forgot about the "2" in weeknum in Excel, but Chris got what I meant. I will try and let you know how it goes.

Thanks again Guys,

Tchaithonov

tchaithonov
Posts: 168
Joined: Mon Apr 13, 2009 7:39 am
Location: New York City

Re: Weeks Dummies

Postby tchaithonov » Fri Sep 17, 2010 2:44 pm

To do this, the 1st week will be missing because it's counted as the last week of the previous year (week 53). I guess the unavoidable fact is that the 53rd week of 2006 (Monday on 12/25/2006, this is the real 53rd week instead of the one I said before) would become week 1 too (so two week 1s in year 2007, but whatever ...):
series wxl = @recode(@ceil((@date - @datefloor(@date, "year") + @datepart(@datefloor(@date, "year"),"w")) /7)=53,1,@ceil((@date - @datefloor(@date, "year") + @datepart(@datefloor(@date, "year"),"w")) /7))


I guess this is it. Thanks again for your help.

tchaithonov
Posts: 168
Joined: Mon Apr 13, 2009 7:39 am
Location: New York City

Re: Weeks Dummies

Postby tchaithonov » Fri Sep 17, 2010 3:17 pm

An ad-hoc way to do this (if we could generalize the "2006" in the code, that would be done) is the following:

series wxl = @recode(@datepart(@date,"yyyy")>2006 and @datepart(@date,"ww") = 53,1, @ceil((@date - @datefloor(@date, "year") + @datepart(@datefloor(@date, "year"),"w")) /7))


I guess I will spend some free time to figure out if there is a math to relate these "every 7 years" before and after 2006 issue....


Return to “Programming”

Who is online

Users browsing this forum: No registered users and 22 guests