## Date Arithmetic

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

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:

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

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

### Re: Date Arithmetic

Hi,

Im using the following date command in order to create a dummy variable for Spring Bank Holiday (last Monday of May):

series bholidays = (@month=4 and @weekday=1 and @datediff(@date,@datefloor(@date,"m"),"w")+1=4)

But it only works when May has 4 Mondays. Therefore, year 2006 is being biased.

How could I correct the command for the "last Monday of the month" independently of the number of Mondays of the month?

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

### Re: Date Arithmetic

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

### Re: Date Arithmetic

Hi,

Finally I used a more complex solution I found in another post:

series bholidays = (@month=5 and @weekday=1 and @datediff(@dateadd(@datefloor(@date, "m"),1,"m"), @date, "d")<=7)

But @holidays is definetly better indeed.

Thanks,

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

### Re: Date Arithmetic

In case someone might find it useful, although for sure there are easier ways to program this. I just wanted to leave here what I have done in order to:

- Create dummy variables for 6 UK Bank Holidays (does not include Good Friday and Easter Monday as they don't follow any arithmetic as far as I am aware).
- Each BH on weekend is placed next Monday (not previous Friday, as might happen at other countries).
- When Boxing Day and Christmas Day are Saturaday and Sunday, these are moved onto next Monday and Tuesday.

series bholidays = (@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) or (@month=12 and @day=25 and @weekday<6) or (@month=12 and @day=26 and @weekday=1) or (@month=12 and @day=27 and @weekday=1) or (@month=1 and @day=1 and @weekday<6) or (@month=1 and @day=2 and @weekday=1) or (@month=1 and @day=3 and @weekday=1) or (@month=5 and @weekday=1 and @datediff(@dateadd(@datefloor(@date, "m"),1,"m"), @date, "d")<=7) or (@month=5 and @weekday=1 and @datediff(@date,@datefloor(@date,"m"),"w")=0) or (@month=8 and @weekday=1 and @datediff(@dateadd(@datefloor(@date, "m"),1,"m"), @date, "d")<=7) or (@month=12 and @weekday=2 and @day=28) or (@month=12 and @weekday=2 and @day=27)

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

### Re: Date Arithmetic

Yeah, @holiday would have made that much simpler.

### Who is online

Users browsing this forum: No registered users and 2 guests