Announcement

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

  • Loop to generate dummy variable over moving date intervals

    Dear all,

    I have a dataset in wide format with stock tickers, date, and price of the stock. I am trying to generate dummy variables that mark each time period e.g. between 1995m7-1996m6, 1996m7-1997m6 and so on. I worked 1995m7-1996m6 period individually and the code does exactly what I want. I am just trying to loop from 1995-2016. Here is what I have in terms of code and Stata error message:

    forval n=1995/2016 {
    gen ind`n'=0
    gen panel`n'=0
    replace ind`n'=1 if date>=tm(`n'm7) & date<=tm(`n'+1m6)
    by ticker : egen sum`n' = total( ind`n' ) if ind`n'==1
    replace panel`n'=1 if sum`n' ==12
    }

    tm(1995+1m6) invalid
    r(198);

    I am puzzled since if I run the code with `n'+1 below I get what I want:

    forval n=1995/2016 {
    2. display `n'+1
    3.
    . }
    1996
    1997
    1998
    1999
    2000
    2001
    2002
    2003
    2004
    2005
    2006
    2007
    2008
    2009
    2010
    2011
    2012
    2013
    2014
    2015
    2016
    2017

    I would appreciate any help with this.

    Thanks in advance.


  • #2
    If you run -help tm()- you will find, among other things:

    Domain l: month literal strings 0100m1 to 9999m12
    In other words, you can't put numbers or arithmetic inside -tm()-. Only literal month strings.

    I think that you will find the following code, that creates a toy data set with some monthly dates, and then generates indicators for years ending in june, helpful:

    Code:
    clear*
    set obs 50
    
    gen mdate = tm(1995m6) + _n
    format mdate %tm
    
    list
    
    //    CREATE VARIABLE FOR YEARS THAT RUN FROM JULY THROUGH JUNE
    gen year_ending_june = year(dofm(mdate+6))
    
    //    IF YOU REALLY NEED EXPLICIT INDICATOR VARIABLES:
    tab year_ending_june, gen(yej_indicator)
    That said, why are you doing this. If you are planning on doing some kind of regression modeling and want to include year-indicators, you do not need to actually create indicator variables to do this. You can just work with the year_ending_june variable itself using factor variable notation:

    Code:
    regression_command outcome x1 x2 ... i.year_ending_june
    and Stata will create virtual indicator variables for you on the fly. You won't clutter up your data set with a plethora of variables that serve no other purpose. And, best of all, using factor variable notation will enable you to use the -margins- command afterward to simplify interpretation of your results.

    Do read -help fvvarlist-.


    Comment


    • #3
      Dear Clyde,

      Thank you so much for your help. Now I have what I needed!!

      Best regards,

      Rodrigo

      Comment

      Working...
      X