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") `

We can build on this to get the date at the beginning of the following month:

Code: Select all

`show @dateadd(@datefloor(@date, "m"),1,"m")`

The difference between these is the number of days in the current month:

Code: Select all

`show (@dateadd(@datefloor(@date, "m"),1,"m") -@datefloor(@date,"m")) `

For the number of business days (Monday to Friday only) in the month, we can use @datediff instead of simple subtraction:

Code: Select all

`show @datediff(@dateadd(@datefloor(@date, "m"),1,"m") , @datefloor(@date,"m") ,"b")`

We can also use simple subtraction to get the number of days until the end of the current month

Code: Select all

`show (@dateadd(@datefloor(@date, "m"),1,"m") -@date)`

Following a slightly different line of thought, we can use @datediff to calculate which week of the month we are in:

Code: Select all

`show @datediff(@date, @datefloor(@date, "m"), "w")+1`

We can then use this to create a dummy variable for the fourth Thursday in the month:

Code: Select all

`show (@weekday=4 and @datediff(@date, @datefloor(@date, "m"), "w")+1=4)`

Which by restricting the month to November, gives us Thanksgiving:

Code: Select all

`show (@month=11 and @weekday=4 and @datediff(@date, @datefloor(@date, "m"), "w")+1=4)`

A slightly different approach can be used to find the last Friday of the quarter:

Code: Select all

`show (@weekday=5 and @datediff(@dateadd(@datefloor(@date, "q"),1,"q"), @date, "d")<=7)`

(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

Code: Select all

`show @mod(@dateadd(@datefloor(@date+7, "y"),1,"y")-7-@date, 365)`

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:

viewtopic.php?f=3&t=476