Date Arithmetic (irregular workfiles)

For questions regarding the import, export and manipulation of data in EViews, including graphing and basic statistics.

Moderators: EViews Gareth, EViews Jason, EViews Steve, EViews Moderator

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

Date Arithmetic (irregular workfiles)

Postby EViews Chris » Thu Feb 19, 2009 7:57 pm

This post is a continuation of an earlier post, found here:
viewtopic.php?f=3&t=474

Here are some examples of date arithmetic in irregular workfiles.

Again, I'll assume that we are working in a daily workfile, although this time I'm assuming some days may not be included in the workfile (typically because of holidays).

I'll start with some simple examples, then tackle a quite difficult example.

First, we can easily calculate the number of days between this observation and the previous observation in the workfile:

Code: Select all

show @date-@date(-1)


Next, we can create a dummy variable for the first day of each month included in the workfile

Code: Select all

show @datefloor(@date, "m")<>@datefloor(@date(-1), "m")


Note that this version will return an NA for the first observation in the workfile. An alternate approach that always returns a 1 for the first observation in the workfile is:

Code: Select all

show @day=@minsby(@day,@datefloor(@date(-1), "m"))


The same approach can be used to create a dummy variable for the last day of each month. Either:

Code: Select all

show @datefloor(@date, "m")<>@datefloor(@date(+1), "m")


or

Code: Select all

show @day=@maxsby(@day,@datefloor(@date(-1), "m"))


Another calculation that can be useful is the number of observations in the workfile within the month containing this observation:

Code: Select all

show @sumsby(1, @datefloor(@date, "m"))


We can also calculate the number of observations from the beginning of the month:

Code: Select all

show @obsid-@minsby(@obsid, @datefloor(@date,"m"))


Or the number of observations until the end of the month:

Code: Select all

show @maxsby(@obsid, @datefloor(@date,"m"))-@obsid



Now, to deal with a fairly difficult example, let's say that we needed to calculate the number of trading days remaining before the next option expiration date, where options expire on the third Friday of every month which can be a holiday.

The first step is to calculate the weekday number of the first day of the current month:

Code: Select all

show @datepart(@datefloor(@date,"m"),"w")


We can use this (in a somewhat cryptic way) to calculate the day number of the first Friday in the month:

Code: Select all

show (8-@mod(@datepart(@datefloor(@date,"m"),"w")+2,7))


It is trivial to shift this forwards to the third Friday in the month:

Code: Select all

show (8-@mod(@datepart(@datefloor(@date,"m"),"w")+2,7))+14


Now, we can create a dummy variable for the last day included in the workfile on or before the third Friday in the month:

Code: Select all

series lastday = (@day<=(8-@mod(@datepart(@datefloor(@date,"m"),"w")+2,7))+14  and @day(+1)> (8-@mod(@datepart(@datefloor(@date,"m"),"w")+2,7))+14)


(Although note that this assumes that there is at least one day in the workfile *after* the third Friday of the month. If not the second condition would also be satisfied if @month<>@month(+1))

Finally, we can calculate the number of observations remaining in the workfile before the next option expiration day:

Code: Select all

show @maxsby(@obsid,@cumsum(lastday))-@obsid

Ricardo98
Posts: 14
Joined: Tue Jun 20, 2017 12:49 pm

Re: Date Arithmetic (irregular workfiles)

Postby Ricardo98 » Tue Jun 27, 2017 11:41 am

Thanks a lot for this material Chris!

Following into this topic:

Say if I want to create a dummy variable =1 when a date is bank holidays (see 26th of December). I would type:

series bholydays= (@month=12 and @day=26)

But ... how would I tell Eviews to make it =0 when that day of the week is either 6 or 7 (weekend) and to be =1 the following Monday?

Thanks a lot in advance.

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

Re: Date Arithmetic (irregular workfiles)

Postby EViews Gareth » Tue Jun 27, 2017 11:45 am

Code: Select all

series bholydats = (@month=12 and @day=26 and @weekday<6) or (@month=12 and @day=27 and @weekday=1) or (@month=12 and @day=28 and @weekday=1)


As an aside, you should look at the @holiday and @event functions.
http://www.eviews.com/help/helpintro.ht ... ation.html
Follow us on Twitter @IHSEViews

Ricardo98
Posts: 14
Joined: Tue Jun 20, 2017 12:49 pm

Re: Date Arithmetic (irregular workfiles)

Postby Ricardo98 » Tue Jun 27, 2017 12:31 pm

Spot on Gareth!

Thanks a lot.

EViews Matt
EViews Developer
Posts: 560
Joined: Thu Apr 25, 2013 7:48 pm

Re: Date Arithmetic (irregular workfiles)

Postby EViews Matt » Wed Jun 28, 2017 2:27 pm

When you have many holidays that you may need to propagate from a weekend to the following Monday, here's a different approach: http://forums.eviews.com/viewtopic.php?f=7&t=17871.


Return to “Data Manipulation”

Who is online

Users browsing this forum: No registered users and 15 guests