380 likes | 417 Views
Learn how to work with date functions and create dummy variables easily in EViews using practical examples. Explore date arithmetic for manipulating dates effectively.
E N D
EViews Training Date Functions • Note: Data and workfiles for this tutorial are provided in: • Results: Results.wf1 • Practice workfile: Data.wf1
Data 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. • WorkfilePage Daily: • structured page, daily frequency (weekday), 1960/01/01-2010/12/31.
Date Functions • EViews has some very powerful built-in functions that allow you to easily work with dates in dated workfiles. Main Functions
Date Functions and Dummy Variables: Example 1 • 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 • Open Data.wf1. Click on the Annual page. • Type in the command window: • series y=@year>1979 • Press Enter.
Date Functions and Dummy Variables: Example 1 (cont’d) • More easily, you could use the @after function to achieve the same result. 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.
Date Functions and Dummy Variables: Example 2 • 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 Dummies: Example 2 • Click on the Annual page. • Type in the command window: • series x_ev8=@before("1987") • Press Enter.
Date Functions and Dummy Variables: Example 3 • 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 Dummies: Example 3 • Click on the Annual page. • Type in the command window: • series z_ev8=@during("1987 1994") • Press Enter.
Date Functions and Dummy Variables: Example 4 • Create a dummy variable equal to 1 for the month of January, and 0 otherwise. Date Functions and Dummy Variables: Example 4 • Click on the Monthly page. • Type in the command window: • series y=@month=1 • Press Enter.
Date Functions and Dummy Variables: Example 5 • Create a dummy variable equal to 1 if the day of the week is Friday, 0 otherwise. Date Functions and Dummy Variables: Example 5 • Click on the Daily page. • Type in the command window: • series y=@weekday=5 • Press Enter.
Date Functions and Dummy Variables: Example 6 • Create seasonal dummies for all four quarters of the year 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.
Date Arithmetic • 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. Main Functions
Date Arithmetic: Example 1 • 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 • Click on the Monthly page. • Type in the command window: • series z=@date<@dateval("1980m03") • Press Enter.
Date Arithmetic: Example 2 • You can use @datediffto 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 • Click on the Monthly page. • Type in the command window: • series x=@datediff(@dateval("10/15/2007"),@dateval("3/1/1995"),"d") • Press Enter. Use “b” for business days
Date Arithmetic: Example 2 (cont’d) • Results for the number of days “d” and number of business days “b” are shown here. # of business days # of 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 3 • In a monthly dated file, find the number of days (or business days) in a month. 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
Date Arithmetic: Example 3 (cont’d) • Results for the number of days “d” and number of business days “b” are shown here. # of business days in the month # of days in the month
Date Arithmetic: Example 3 (cont’d) • More easily, you could use the @daycount function. Date Arithmetic: Example 3 (cont’d) • Click on the Monthly page. • Type in the command window: • series dayEV8=@daycount • Press Enter.
Date Arithmetic: Example 3 (cont’d) • You can use the function @daycountto 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) • Click on the Monthly page. • Type in the command window: • series FridayEV8=@daycount("Friday") • Press Enter.
Date Arithmetic: Example 3 (cont’d) • Likewise, you can use @daycountfunction 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) • Click on the Monthly page. • Type in the command window: • series Wed_Fri_EV8=@daycount("Wednesday Friday") • Press Enter.
Date Arithmetic: Example 4 • 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 • 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.
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.
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
Date Arithmetic: Example 5 • You can use @dateadd and @datedifffunction 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 • Click on the Daily page. • Type in the command window: • show (@dateadd(@datefloor(@date, "m"),1,"m")-@date) • OR • show @datediff(@dateadd(@datefloor(@date, "m"),1,"m"),@date,"d") • Press Enter.
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
Date Arithmetic: Example 6 • Let’s find the number of days since the beginning of the month. 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.
Date Arithmetic: Example 6 (cont’d) • Results for both actual days and business days are shown here. # of business days since the start of the month # of days since the start of the month
Date Arithmetic: Example 7 • 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 • 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")
Date Arithmetic: Example 8 • You can also use @datediff function to find out the week of the month. 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”.
Date Arithmetic: Example 9 • 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 • 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)
Date Arithmetic: Example 10 • Building on the previous two examples, we can now create a dummy variable that picks out US Labor Day (which occurs on the first Monday of Sept.). 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.
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.
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.
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: