Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to create dummy variables for day of the week, day of the month etc.

    Hello, I am fairly new at using stata, thus since I want to control for time varying influences (my time series data set consista of daily data) I want to create binary variables for day of the week, day of the month and month of the year, however I am having trouble understanding how to do this. I tried by first creating a day of the week variable with stata, and then using the following code to create day of the week dummies: tab dow, gen (d) and I ended up with seven dummy variables and not six, is that correct? Are there other methods of doing it? I'd also like to know how to do it for day of the month, month of the year and for holidays. I am a beginner with stata, thus any help would be highly appreciated.

  • #2
    Welcome to the Stata Forum/ Statalist.

    You may wish to read this thread.
    Best regards,

    Marcos

    Comment


    • #3
      Welcome to Statalist.

      I think you may need to learn about Stata's factor variable notation, which saves you from needing to create dummy variables. Here is a quick example using the auto dataset included with Stata.
      Code:
      . sysuse auto, clear
      (1978 Automobile Data)
      
      . list make foreign i.foreign in 51/55, nolabel
      
           +---------------------------------------------+
           |                                 0.        1.|
           | make            foreign   foreign   foreign |
           |---------------------------------------------|
       51. | Pont. Phoenix         0         1         0 |
       52. | Pont. Sunbird         0         1         0 |
       53. | Audi 5000             1         0         1 |
       54. | Audi Fox              1         0         1 |
       55. | BMW 320i              1         0         1 |
           +---------------------------------------------+
      
      . regress mpg weight i.foreign
      
            Source |       SS           df       MS      Number of obs   =        74
      -------------+----------------------------------   F(2, 71)        =     69.75
             Model |   1619.2877         2  809.643849   Prob > F        =    0.0000
          Residual |  824.171761        71   11.608053   R-squared       =    0.6627
      -------------+----------------------------------   Adj R-squared   =    0.6532
             Total |  2443.45946        73  33.4720474   Root MSE        =    3.4071
      
      ------------------------------------------------------------------------------
               mpg |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
      -------------+----------------------------------------------------------------
            weight |  -.0065879   .0006371   -10.34   0.000    -.0078583   -.0053175
                   |
           foreign |
          Foreign  |  -1.650029   1.075994    -1.53   0.130      -3.7955    .4954422
             _cons |    41.6797   2.165547    19.25   0.000     37.36172    45.99768
      ------------------------------------------------------------------------------
      We see that the categorical variable foreign is coded 0 and 1 for Domestic and Foreign, and in the regression, a dummy for the Foreign category (of the variable foreign) is included automatically, and of course the Domestic category is excluded to avoid collinearity.

      For your question,
      Code:
      i.dow
      will include 6 day-of-week indicator variables, excluding dow 0 (Sunday). Or if you only have weekdays (dow = 1 to 5), excluding dow 1 (Monday). But if you want some other day to be excluded, factor variable notation can do that instead by specifying the "base level" to be excluded..

      This is a very brief overview. Before proceeding, do read the output of help factor variables and section 11.4.3 of the Stata User's Guide PDF included with your Stata installation and accessible from Stata's Help menu. Your effort will be amply repaid.

      And a more general piece of advice.

      I'm sympathetic to you as a new user of Stata - it's a lot to absorb.

      When I began using Stata in a serious way, I started, as have others here, by reading my way through the Getting Started with Stata manual relevant to my setup. Chapter 18 then gives suggested further reading, much of which is in the Stata User's Guide, and I worked my way through much of that reading as well. There are a lot of examples to copy and paste into Stata's do-file editor to run yourself, and better yet, to experiment with changing the options to see how the results change.

      All of these manuals are included as PDFs in the Stata installation (since version 11) and are accessible from within Stata - for example, through the PDF Documentation section of Stata's Help menu. The objective in doing the reading was not so much to master Stata as to be sure I'd become familiar with a wide variety of important basic techniques, so that when the time came that I needed them, I might recall their existence, if not the full syntax, and know how to find out more about them in the help files and PDF manuals.

      Stata supplies exceptionally good documentation that amply repays the time spent studying it - there's just a lot of it. The path I followed surfaces the things you need to know to get started in a hurry and to work effectively.

      Comment

      Working...
      X