Announcement

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

  • Loops and dates

    I am trying to create multiple dummies for an event study with the following loop:

    Code:
    forval y=2018m1/2019m12 {
        gen d`y' = (year_month==`y')
        local x = `y' - 2018m10
        local z = abs(`x')
        
        if `x'<-1 {
            gen treated_Tm`z' = support * d`y'        
        }
        
        if `x'>-1 {
            gen treated_Tp`z' = support * d`y'
        }
        
    }
    Unfortunately, I keep getting the message "invalid syntax." I think it has something to do with the fact that my time variable was created using the following command:

    Code:
    gen year_month=ym(ano,month)
    format year_month %tm
    Which is the composition of year (ano) and month of my panel that generated a float value.

  • #2
    No; that is not the problem. The issue is that forvalues will not evaluate references like 2018m1 on the fly. The simplest way to get what you want is to loop over 696/719.

    Whether you really need 24 and yet more extra variables is a different question.

    Comment


    • #3
      Thank you, Nick Cox! I did it and worked.

      Comment


      • #4
        This syntax might help too:

        Code:
        forvalues y=`=ym(2018,1)'/`=ym(2019,12)' {

        Comment

        Working...
        X