Page 1 of 10

Dummy Variables

Posted: Tue Mar 08, 2011 10:30 am
by EViews Gareth
We get asked questions on dummy variable creation in EViews fairly regularly, so I thought I'd write up a quick all-inclusive guide.
Simple Dummies
The easiest way to create a dummy variable is with the @recode function. @recode lets you specify a logical test, and the values a variable should take if that test is true, or if it is false:

Code: Select all

series dummy1 = @recode(X>0.5, 1, 0)

This will create a series called dummy1 that is equal to 1 whenever the series X is greater than 0.5, and equal to 0 otherwise. You can use the AND or OR operators to make more complicated logical tests:

Code: Select all

series dummy2 = @recode(X>=0.5 and X<=1, 1, 0)

will create a dummy that is equal to 1 whenever X is between 0.5 and less than 1.

Date Dummies
You can use @recode to create dummy variables based upon dates too. In dated workfiles there are a collection of keywords you can use to refer to the date of each observation. The first of these is the @date keyword that returns the date number associated with each observation's date. You can couple this with the @dateval command to create a date number based upon the text representation of a date, in order to create dummy variables based upon dates:

Code: Select all

series dummy3 = @recode(@date>@dateval("2010/03/02"), 1, 0)

This will create a dummy variable, dummy3 that is equal to 1 for all dates after 2010/03/02.

You can extend this with ANDs and ORs too:

Code: Select all

series dummy4 = @recode(@date>@dateval("2010/03/02") and @date<@dateval("2010/06/02"), 1, 0)

creates a dummy variable equal to 1 between 2010/03/02 and 2010/06/02.

Code: Select all

series dummy5 = @recode(@date<@dateval("1980") or @date>@dateval("1990"), 1, 0)

creates a dummy equal to 1 for all dates before 1980 or after 1990.

You can use other date keywords to create dummies too. The @year specifies the year part of each observations. Thus:

Code: Select all

series dummy6 = @recode(@year>1990, 1, 0)

creates a dummy equal to 1 for all observations which lie after 1990.

Code: Select all

series dummy7 = @recode(@month=1, 1, 0)

creates a dummy equal to 1 for all observations in January.

Code: Select all

series dummy8 = @recode(@weekday=3, 1, 0)

creates a dummy equal to 1 for all observations on a Wednesday.

Dummies in Equations
Note you do not have to actually create the dummy series inside the workfile to use dummy variables in an equation, rather you can enter the dummy expression directly in the equation specification, either via command:

Code: Select all

equation eq1.ls Y C X @date>@dateval("1990")

or via dialog:
Image

Note that in both cases you should not have spaces in the logical expression. @year>1990 is fine, but @year > 1990 is not.

Categorical Dummies
If you have a categorical variable and wish to create dummy variables for each unique values in that variable, you can do so easily using the @expand command. For example, say you have a variable called "Sex" that is either "M" or "F", you could create an equation with the following specification:
Image
which would give the following output:
Image

Note that by default the @expand keyword will create a full set of dummies, thus you should not include a constant in your equation (since you'll create a singular matrix problem).

Alternatively, @expand lets you drop certain values from the expansion to avoid the singularity problem. You can use @dropfirst or @droplast to drop the first or the last categorisation. Thus:

Code: Select all

Y C X @expand(sex, @dropfirst)

Would regress Y on a constant, X and SEX=M, and not SEX=F.

Fixed Effect Dummies
Fixed effects in panel estimation can be thought of as having a dummy variable for each cross-section. In most cases you don't need to worry about that, since EViews will add the fixed effects for you as an option during estimation. But sometimes you might want to create the dummy variables yourself. To do so is relatively simple, using @expand. Simply include this:

Code: Select all

@expand(@crossid)

in your specification to include cross-section fixed effects. For period fixed effects, simply include:

Code: Select all

@expand(@obsid)


Remember to be careful of the dummy variable trap. You might want to drop one of the dummies, as outlined above.

Re: Dummy Variables

Posted: Mon Mar 14, 2011 1:06 pm
by esse0001
Dear there

Thanks for your help first.

I want to create seasonal dummies for month 8 and month 9( August and September) together for my time series data. When I use the formula m8=@seas (8) or m9=@seas(9) it creates me individually but not together. on the other hand when I use the formula sea_dummy = @seas(8) and then at seas(9), it does not create to together rather only it accepts the last creating dummies.

Could please help me how I can create dummies for month 8 and month 9 together? (with one spreadsheet) and make the reaining months 0

best regards
Menge

Re: Dummy Variables

Posted: Mon Mar 14, 2011 1:10 pm
by EViews Gareth
Following the Date Dummies section above:

Code: Select all

series dummy = @recode(@month=8 or @month=9, 1, 0)

Re: Dummy Variables

Posted: Wed Mar 23, 2011 7:38 am
by trinhndt
EViews Gareth wrote:Following the Date Dummies section above:

Code: Select all

series dummy = @recode(@month=8 or @month=9, 1, 0)


Hi Gareth,

How can we set Year dummy variables in panel workfile. Year range is 2006 to 2009 and I choose 2006 is based year. So, what do I set year dummy in this case?
Thanks for your support!

Re: Dummy Variables

Posted: Wed Mar 23, 2011 7:53 am
by EViews Gareth
I'm not sure I understand the question. If you just want dummies for each year, aren't you just doing period fixed effects?

Re: Dummy Variables

Posted: Wed Mar 23, 2011 7:33 pm
by trinhndt
EViews Gareth wrote:I'm not sure I understand the question. If you just want dummies for each year, aren't you just doing period fixed effects?


Hi Gareth, I mean that I perform data with panel workfile form. So, I want to test whether year variable effects to dependent variable if I observe in 4 years from 2006 to 2009. How can I code year dummy variable in Eviews for this test?

Re: Dummy Variables

Posted: Wed Mar 23, 2011 9:01 pm
by EViews Gareth

Code: Select all

if @year=2006 or @year=2009

etc...?

Re: Dummy Variables

Posted: Fri Mar 25, 2011 3:18 am
by oybar
Hi!

Thanks for the guide Gareth! I was wondering on how to construct a dummy variable representing several periods of a sample period.
For. ex i want a dummy variable to represent the period from "1980/01/01 to 1983/09/01" and the period from "1991/04/01 to 1993/01/01"

I have tried this code: series dummy4 = @recode(@date>@dateval("1980/01/01") and @date<@dateval("1983/09/01"), and @date>@dateval("1991/04/01") and @date<@dateval("1993/01/01"),1, 0)

Best,

Oystein

Re: Dummy Variables

Posted: Fri Mar 25, 2011 7:51 am
by EViews Gareth

Code: Select all

 series dummy4 = @recode((@date>@dateval("1980/01/01") and @date<@dateval("1983/09/01")) or (@date>@dateval("1991/04/01") and @date<@dateval("1993/01/01")),1, 0)

Re: Dummy Variables

Posted: Wed Jun 15, 2011 5:27 am
by rogierhanselaar
Hello there,

I have had a peak at the forum and it has helped me a lot already. Currently I'm still stuck with the following issue: I want to run a regression on a series that consists of observations made every hour of every day for 19 years. I suspect hourly effects and weekly effects. How can I best create dummies to check the relevance of these effects? I'd appreciate your advice.

Best,
Rogier

Re: Dummy Variables

Posted: Wed Jun 15, 2011 8:04 am
by EViews Gareth
Assuming you have your workfile structured as an hourly workfile, then you can add hourly dummies to your equation with:

Code: Select all

@expand(@hour, @dropfirst)

Which will create dummy variables for every hour (apart from the first).

You'll have to define what you mean by weekly dummies.

Re: Dummy Variables

Posted: Wed Jun 15, 2011 9:02 am
by jlbrillet
Hello from Paris,

In my experience I find it modre convenient to create a time variable called, let us say, t, with rhe value of the year. For quarterly models I start with the year for the 1st quarter, and increment by 0.25 for each quarter (this does not work if 1/periocity is not a decimal fraction, as with months).

Using this technique:

1 - I can manage logical dummies easily, like :

(t>=1990.25)*(t=<2000) meaning from 1990Q2 to 200Q4;

2 - used in a regression, the coefficient of t represents a yearly trend (like yearly labor productivity growth). No need to multiply by 4.

Jean Louis Brillet.

Re: Dummy Variables

Posted: Sat May 12, 2012 4:11 am
by ParisEllen
I am totally stumped. I have tried a whole lot of different methods for creating a dummy variable, but nothing quite works. I have a whole set of data, which has been given, and I need to create a dummy variable. I have 40 observations, the first 20 of which are for one company and the last 20 are for a second company. The first company has to equal 0 and the second 1. I initially tried view-descriptive statistics- histogram and stats -sample -@all if D=1, sample size 21-40. But this results in the first 20 values given the value of N/A. Then I tried some of your suggestions, c @expand(name) resulted in both having a value of one. Then I tired a variety of series dummy1= @recode(@name=company1 @name=company2, 1, 0). I just have no idea what to do... Thanks for any help.

Dummy Variables

Posted: Sat May 12, 2012 6:54 am
by EViews Gareth
Sounds like all the things you've tried should work. Perhaps you could post your workfile.

Re: Dummy Variables

Posted: Sat May 19, 2012 12:46 am
by toa
Help needed please! I'm fairly new to eview (using eveiw6) and econometrics altogether so will greatly appreciate any help.
I have tried a number of ways as suggested on this forum but keep getting error messages.
I need to create a dummy variable representing a certain county (which contains the number of votes for each candidate) from 67 counties. The problem is, there is no variable for county itself. My variables are the candidates (and each observation is the number of votes received in each county).
Thanks in advance for any help