Announcement

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

  • if not found

    Good day guys,

    The first loop runs fine, which stores the e(rmse), however, the second loop gives me the error "if not found". Can someone please assist?

    Thank you

    Code:
    forvalues i=1(1)130 { 
        l id company_id if id==`i' & dif==0
        reg ret market_return if id==`i' & estimation_window==1 
          local rmse_id_`i' = `e(rmse)'
        predict p if id==`i'
        replace predicted_return = p if id==`i' & event_window==1 
        drop p
    }
    Code:
    gen t_value = .
    forvalues i = 1/130 {
          replace t_value = abnormal_return2/`rmse_id_`i'' if id == `i' & event_window == 1
          }

  • #2
    I believe you should pot the conditions after the 'if' in brackets that is
    Code:
     
     if (id==`i' & dif==0)

    Comment


    • #3
      Thank you for your suggestion. I tried it but I am getting the same error

      Comment


      • #4
        I'm guessing that you are running these two blocks of code separately. Try running them altogether with no interruption and I'm pretty sure it will work. Here's why:

        The local macros rmse_id_... get defined in the first block of code. But if you run that code in isolation, as soon as it is done, those local macros go out of scope. When you then pick up with the second block of code, rmse_id_... does not exist, and so is interpreted as an empty string. Thus, what Stata sees in the offending command is:
        Code:
        replace t_value = abnormal_return2/ if id == `i' & event_window == 1
        It attempts to make sense of that by imagining that perhaps if is a variable that belongs in the denominator. But you have no such variable (and can't because -if- is a reserved word and not allowed as a variable name). So it tells you that it can't find it.

        Always remember that the scope of a local macro is limited to the program or do-file it is written in, and a block of code that is highlighted and run from the do-editor is considered its own do-file for this purpose. That sometimes gets in the way, as here, for people who aren't used to that behavior. It is also the reason that local macros are safe to use and globals are not.

        Comment


        • #5
          Thank you. Yes you were correct, it worked this time. I keep forgetting the scope of the local macro

          Comment

          Working...
          X