Date Arithmetic
Posted: Thu Feb 19, 2009 12:25 pm
Date Arithmetic
EViews has some fairly powerful functions for working with dates, but using them is a bit of an art. I thought a few examples might provide some useful building blocks.
I’m going to assume we’re working with a daily file. The same principles apply in other frequencies with appropriate adjustments.
There are a few obvious functions available in the workfile such as:
@year - year of current observation
@month – month of current observation
@day – day in month of current observation
@weekday – day in week of current observation
But a lot of useful calculations are going to require working with dates more generally. The important functions are ones such as:
@date – returns the ‘day number’ for the start of this observation
@dateadd – shifts a date forward or backward by a unit of time
@datediff – calculates the difference between two dates in a unit of time
@datefloor – rounds down a date to the beginning of a unit of time
You should consult the help system or Command Reference for details on the functions, but the following examples should give you some idea as to what sort of things can be done with them.
First, let’s calculate the date at the beginning of the month containing the current observation:
We can build on this to get the date at the beginning of the following month:
The difference between these is the number of days in the current month:
For the number of business days (Monday to Friday only) in the month, we can use @datediff instead of simple subtraction:
We can also use simple subtraction to get the number of days until the end of the current month
Following a slightly different line of thought, we can use @datediff to calculate which week of the month we are in:
We can then use this to create a dummy variable for the fourth Thursday in the month:
Which by restricting the month to November, gives us Thanksgiving:
A slightly different approach can be used to find the last Friday of the quarter:
(because the last Friday of the quarter must be in the last seven days of the quarter).
As a final brainteaser, the following expression can be used to find the number of days until Christmas
Note that @mod is simply being used here to replace 365 with 0 leaving all other values unchanged.
For information on dealing with irrelgular workfiles, see this post:
http://forums.eviews.com/viewtopic.php?f=3&t=476
EViews has some fairly powerful functions for working with dates, but using them is a bit of an art. I thought a few examples might provide some useful building blocks.
I’m going to assume we’re working with a daily file. The same principles apply in other frequencies with appropriate adjustments.
There are a few obvious functions available in the workfile such as:
@year - year of current observation
@month – month of current observation
@day – day in month of current observation
@weekday – day in week of current observation
But a lot of useful calculations are going to require working with dates more generally. The important functions are ones such as:
@date – returns the ‘day number’ for the start of this observation
@dateadd – shifts a date forward or backward by a unit of time
@datediff – calculates the difference between two dates in a unit of time
@datefloor – rounds down a date to the beginning of a unit of time
You should consult the help system or Command Reference for details on the functions, but the following examples should give you some idea as to what sort of things can be done with them.
First, let’s calculate the date at the beginning of the month containing the current observation:
Code: Select all
show @datefloor(@date,"m") Code: Select all
show @dateadd(@datefloor(@date, "m"),1,"m")Code: Select all
show (@dateadd(@datefloor(@date, "m"),1,"m") -@datefloor(@date,"m")) Code: Select all
show @datediff(@dateadd(@datefloor(@date, "m"),1,"m") , @datefloor(@date,"m") ,"b")Code: Select all
show (@dateadd(@datefloor(@date, "m"),1,"m") -@date)Code: Select all
show @datediff(@date, @datefloor(@date, "m"), "w")+1Code: Select all
show (@weekday=4 and @datediff(@date, @datefloor(@date, "m"), "w")+1=4)Code: Select all
show (@month=11 and @weekday=4 and @datediff(@date, @datefloor(@date, "m"), "w")+1=4)Code: Select all
show (@weekday=5 and @datediff(@dateadd(@datefloor(@date, "q"),1,"q"), @date, "d")<=7)As a final brainteaser, the following expression can be used to find the number of days until Christmas
Code: Select all
show @mod(@dateadd(@datefloor(@date+7, "y"),1,"y")-7-@date, 365)For information on dealing with irrelgular workfiles, see this post:
http://forums.eviews.com/viewtopic.php?f=3&t=476