Note Data and workfiles for this tutorial are provided in Results Resultswf1 Practice workfile Datawf1 Basic work with Date Functions Data and Workfile Documentation Datawf1 ID: 916728
Download Presentation The PPT/PDF document "EViews Training Date Functions" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.
Slide1
EViews Training
Date Functions
Note:
Data and workfiles for this tutorial are provided in:
Results:
Results.wf1
Practice workfile:
Data.wf1
Slide2Basic work with Date Functions
Slide3Data and
Workfile Documentation
Data.wf1
has a number of workfile pages (note there are no data in any of the pages).
Workfile
Page Annual: structured page, annual frequency, 1960 - 2010. Workfile Page Quarterly: structured page, quarterly frequency, 1960q1-2010q4. Workfile Page Monthly: structured page, monthly frequency, 1960m1-2010m12. Workfile Page Daily: structured page, daily frequency (weekday), 1960/01/01-2010/12/31.
3
Slide4EViews has some
very powerful built-in functions that allow you to easily work with dates in dated workfiles.
Main Functions
4
Date Functions
Function
Description
@year
Returns the year in which each observation begins.
@quarter
Returns the quarter of the year in which each observation begins.
@month
Returns the month of the year in which each observation begins.
@day
Returns the day of the month in which each observations begins.
@weekday
Returns the day of the week.
@hour
Returns the hour of each observation as
an integer.
@minute
Returns the minute
of each observation as an integer.
@second
Returns the second of each observation as an integer.
@seas(x)
Returns a seasonal dummy variable.
Slide55
Date
Functions
(cont’d)
Function
Description
@
daycount
Returns the number of days within each
observation.
@before(“date”)
Returns a
dummy variable with value equal to 1 prior to the specified date.
@after(“date”)
Returns a
dummy variable with value equal to 1 after the specified date.
@during(“date1
date2”)
Returns a dummy variable with value equal to 1 for each observation between date1 and date2.
@holiday
Returns
the proportion of an annual event (such as a holiday) that lies in each observation.
Slide66
Function
Description
series y=@year>1979
Series equals 1 post-1979 and 0 otherwise
Date functions can be very useful when creating date-related dummy variables.
Suppose you want to create an annual dummy variable which has the value of 1 post-1979 and 0 otherwise.
Date Functions and Dummy Variables:
Example 1
Date Functions and Dummy Variables: Example 1
Open
Data.wf1.
Click on the
Annual
page.
Type in the command window:
series y=@year>1979
Press Enter.
Slide77
Function
Description
series y_EV8=@after(“1979”)
Series equals 1 post-1979 and 0 otherwise
More easily, you could use the
@after
function to achieve the same result.
Date Functions and Dummy Variables:
Example 1
(cont’d)
Date Functions and Dummies: Example 1 (cont’d)
Click on the
Annual
page.
Type in the command window:
series y_Ev8=@after("1979")
Press Enter.
Slide88
Function
Description
series x_EV8=@before(“1987”)
Series equals 1 pre-1987 and 0 otherwise
Likewise,
EViews
offers a number of easy-to-use functions that allow you to create date-related dummy variables.
Suppose you want to create an annual dummy variable which has the value of 1 pre-1987 and 0 otherwise.
Date Functions and Dummy Variables:
Example 2
Date Functions and Dummies: Example
2
Click on the
Annual
page.
Type in the command window:
series x_ev8=@before("1987") Press Enter.
Slide99
Function
Description
series z_EV8=@during(“1987 1994”)
Series equals 1 between 1987
and 1994
As an additional example, consider the case of creating a dummy variable which equals 1 between 1987 and 1994 and 0 otherwise.
Again, we can create this indicator very easily.
Date Functions and Dummy Variables:
Example 3
Date Functions and Dummies: Example
3
Click on the
Annual
page.
Type in the command window:
series z_ev8=@during("1987 1994") Press Enter.
Slide1010
Function
Description
series y=@month=1
Series equals 1 if month=January, 0 otherwise
Create a dummy variable equal to 1 for the month of January, and 0 otherwise
.
Date Functions and Dummy Variables:
Example 4
Date Functions and Dummy Variables: Example 4
Click on the
Monthly
page.
Type in the command window:
series y=@month=1
Press
Enter.
Slide1111
Function
Description
series
y=@weekday=5Series equals 1 if day=Friday, 0 otherwise
Create a dummy variable equal to 1 if the day of the week is Friday, 0 otherwise.
Date Functions and Dummy Variables:
Example 5
Date Functions and Dummy Variables: Example 5
Click on the
Daily
page.
Type in the command window:
series y=@weekday=5
Press
Enter.
Slide1212
Function
Description
@seas(1), @seas(2) @seas(3), @seas(4)
Creates 4 dummy variables for the four quarters of the year in a quarterly
workfile
Create seasonal dummies for all four quarters of the year
Date Functions and Dummy Variables:
Example 6
Date Functions and Dummy Variables: Example 6
Click on the
Quarterly
page.
Type in the command window:
show
@seas(1) @seas(2) @seas(3) @seas(4)
Press
Enter.
Slide13More Advanced Work with Date Functions
Slide14Date Arithmetic
14
EViews
provides several functions for manipulating dates more generally.
EViews
first converts a date string into a date number so that you can manipulate it.
For example,
EViews
first converts “January 1, 1999” into a numerical value (729754) which allows you to manipulate dates more generally.
Function
Description
@date
Returns the ‘day number’ for the start of this observation
@
dateadd
Shifts a date forward or backward by a unit of time
@datediffCalculates the difference between two dates in a unit of time@datefloor
Rounds down a date to the beginning of a unit of time@datevalConverts a text date into a numerical value Main Functions
Slide1515
You can use
@date
and
@dateval
to create date-related dummy variables.
Suppose you want to create a dummy variable in the monthly workfile which takes on the value of 1 for all dates prior to March 1980.
Date Arithmetic:
Example 1
Date Arithmetic: Example
1
Click on the
Monthly
page.
Type in the command window:
series z=@date<@
dateval
("1980m03") Press Enter.
Function
Description
series
z=@date<@
dateval
(“1980 m03”)
Series equals 1 pre-March 1980, 0 otherwise
Slide1616
You can use
@
datediff
to perform a number of date calculations.
Suppose for example, you want to find the number of days (or business days) between 10/15/2007 and 3/1/1995).
Date Arithmetic:
Example 2
Date Arithmetic: Example 2
Click on the
Monthly
page.
Type in the command window:
series x=@
datediff
(
@dateval("10/15/2007"),@dateval("3/1/1995"),"d") Press Enter.
Function
Description@datediff(d1, d2, u)
Returns the difference between two date numbers
d1 is the start date
d2 is the end date
u is the time unit
d1 and d2, measured by the time unit u
Use
“b”
for business days
Slide1717
# of days
# of business days
Note:
You can use any of the pages (annual, quarterly, monthly, etc.) in the
Data.wf1
file for this example. All will produce similar results, calculating the difference (in either days or business days) between two dates.
Date Arithmetic:
Example 2
(cont’d)
Results for the number of days “
d
” and number of business days “
b
” are shown here.
Slide1818
In a monthly dated file, find the number of days (or business days) in a month.
Date Arithmetic:
Example 3
Date Arithmetic: Example
3
Click on the
Monthly
page.
Type in the command window:
series w=@
datediff
(
@date(+1),@
date,"d
")
Press Enter.
Use
“b”
for business days
Slide1919
Date Arithmetic:
Example 3
(cont’d)
# of days in the month
# of business days in the month
Results for the number of days “
d
” and number of business days “
b
” are shown here.
Slide2020
More easily, you could use the
@
daycount
function.
Date Arithmetic:
Example 3
(cont’d)
Date Arithmetic: Example 3 (cont’d)
Click on the
Monthly
page.
Type in the command window:
series dayEV8=@
daycount
Press
Enter.
Slide2121
You can use the
function
@
daycount
to perform a slightly different exercise.
For example, you would like to count the number of “Fridays” that appear in a given month.
Date Arithmetic:
Example 3
(cont’d)
Date Arithmetic: Example 3 (cont’d)
Click on the
Monthly
page.
Type in the command window:
series FridayEV8=@
daycount("Friday") Press Enter.
Slide2222
Likewise, you can use
@
daycount
function to count the number of days in a month that fall in an interval defined by two weekdays (including those days).
For example suppose you would like to count the number of days in a month that fall between “Wednesday” and “Friday” (including Wed. and Fri).
Date Arithmetic:
Example 3
(cont’d)
Date Arithmetic: Example 3 (cont’d)
Click on the
Monthly
page.
Type in the command window:
series Wed_Fri_EV8=@
daycount
("Wednesday Friday") Press Enter.
Slide2323
In a monthly dated file, find the number of days (or business days) in a quarter.
Note that this example is similar to Example 3, except that you cannot use
@datediff(@date(+1), @date, “
d
”)
,
because the file is structured in monthly frequency and you are looking for the number of days in a quarter.
Here is a more general
approach in dealing with dates, regardless of the structure of the dated
workfile
.
Date Arithmetic:
Example 4
Date Arithmetic: Example 4
Click on the
Monthly
page. Type in the command window: show @datefloor(@date,"q") Press Enter.
Note that this command instructs EViews to show the date at the beginning of the quarter.
Slide2424
Date Arithmetic:
Example 4
(
cont’d)
Next, type in the command window:
show @
dateadd
(@
datefloor
(@
date,"q
"),1,"
q
")
Press
Enter. Note that this command instructs EViews to show the date at the beginning of the next quarter.
Slide2525
Date Arithmetic:
Example 4
(
cont’d)
Finally, type in the command window:
show
@
datediff
(@
dateadd
(@
datefloor
(@
date,"q
"),1,"q"),
@
datefloor
(@date,"q"),"d")Press Enter. Note: Please make sure that the above command is typed in one line in the command bar. Note also that this command instructs EViews to find the difference (in days) between the dates found in steps (2-3) and those found in steps (4-5).
# of days in the quarter
Slide2626
You can use
@
dateadd
and
@
datediff
function to find the number of days remaining until the end of the month, or the number of days that have elapsed since the start of the month.Let’s illustrate how to find the number of days until the end of the month.
Date Arithmetic:
Example 5
Date Arithmetic: Example 5
Click on the
Daily
page.
Type in the command window:
show (@dateadd(@datefloor(@date, "m"),1,"m")-@date) ORshow @datediff(@dateadd(@datefloor(@date, "m"),1,"m"),@date,"d")
Press
Enter.
Slide2727
Date Arithmetic:
Example 5
(cont’d)
The result is shown here.
Note that this shows the number of days between a given date until the end of the month. If you want to find the number of
business days
until the end of the month, you should type “
b
” instead of “
d
” at the end of the command.
Note also that the first portion of the command
(@
dateadd
(@
datefloor
(@date, "m"),1,"m
")), instructs EViews to find the first date of the next month, while -@date subtracts the current date from the next month’s first value. # of days until the end of the month
Slide2828
Let’s find the number of days since the beginning of the month.
Date Arithmetic:
Example 6
Date Arithmetic: Example
6
Click on the
Daily
page.
Type in the command window:
show
(@date-@
datefloor
(@
date,"m
"))If instead, you would like to find the number of business days since the start of the month, type: show @datediff(@date, @datefloor(@date,"m"), "b") Press Enter.
Slide2929
Date Arithmetic:
Example 6
(cont’d)
Results for both actual days and business days are shown here.
# of days since the start of the month
# of business days since the start of the month
Slide3030
You can also
use the
@date
function to create dummy variables that pick out the first or the last day of the month (or quarter).
Date Arithmetic:
Example 7
Date Arithmetic: Example 7
Click on the
Daily
page.
Type in the command window:
series
x=@
datefloor
(@date,
"m")<>@datefloor(@date(-1), "m")Press Enter. Note: Please make sure that the above command is typed in one line in the command bar. If you wanted to create a dummy variable for the last day of the month, type in the command window:
series x1=@datefloor(@date, "m")<>@datefloor(@date(+1), "m")
Slide3131
You can also use
@datediff
function to find out the week of the month.
Date Arithmetic:
Example 8
Date Arithmetic: Example
8
Click on the
Daily
page.
Type in the command window:
show @
datediff
(@date, @
datefloor
(@date, "m"), "w")+1
Press Enter.
Note that the
command finds the difference (in weeks) between the current date and the date at the beginning of the month. The
“+1” is used so that the week count starts from “1” and not “0”.
Slide3232
You can also use the set up of the previous example, to create a dummy variable for the first Monday of every month.
Date Arithmetic:
Example 9
Date Arithmetic: Example 9
Click on the
Daily
page.
Type in the command window:
series w1=(@
weekday=1
and @
datediff
(@date
, @
datefloor
(@
date
,"m"),"w")+1=1)Press Enter. Note: Please make sure that the above command is typed in one line in the command bar. If you wanted to create a dummy variable for the fourth Friday of the month, type in the command window:series w2=(@weekday=5 and @datediff(@date, @
datefloor(@date, "m"), "w")+1=4)
Slide3333
Building on the previous two examples, we can now create a dummy variable that picks out
US Labor
D
ay
(which occurs on the first Monday of
Sept.).
Date Arithmetic:
Example 10
Date Arithmetic: Example 10
Click on the
Daily
page.
Type in the command window:
series w3=(@
month=9 and @weekday=1 and
@
datediff
(@date,@datefloor(@date, "m"), "w")+1=1)Press Enter.
Note: Please
make sure that the above command is typed in one line in the
command
bar.
Slide3434
Date Arithmetic:
Example 11
Let’s illustrate a slightly more complex example which is a combination of some of the previous examples.
Suppose you would like
to weight
each monthly observation by the days in the month/normalized (divided by) the total days in the year.
You can accomplish this in a few steps as shown here.
Date Arithmetic: Example 11
Click on the
Monthly
page.
As a first step, let’s find the number of business days in the month (similar to example 4 above). For this, type in the command window:
series t1=@
datediff
(@
dateadd
(@
datefloor
(@
date
,"m
"),
1
,"
m
"),@
datefloor
(@
date,
"m
"),"b")
Press
Enter
.
Note: Please
make sure that the above command is typed in one line in
the command
bar.
Slide3535
Date Arithmetic:
Example 11
(cont’d)
Next, let’s find the number of business days in the year by typing in the command window:
series t2=@
datediff
(@
dateadd
(@
datefloor
(@
date,"
y
"
),1,"
y
"), @
datefloor(@date, "y"),"b") Press Enter.
Note: Please
make sure that the above command is typed in one line in the
command bar.
Slide3636
Date Arithmetic:
Example 11
(cont’d)
Lastly, let’s form the weights by dividing
t1
by
t2,
typing in the command window:
series weights=t1/t2
Press
Enter
.
Note: The commands for this example are shown here: