Announcement

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

  • r(2000) no observations using foreach and run

    Hi,
    I am conducting an event study and it is my first time that I post on Statalist.

    Panel data:
    -1713 firms having different event date and daily stock returns (2015-2021)
    -4 Market indexes returns (2015-2021)

    Estimation window [-120 -21]
    Event window [-20 20]


    I am getting no observation error when running the below command in order to calcolate the beta and therefore the expected return for each stock I have.
    I already confirm that my ret and IndexReturn are numeric variables.

    Code:
    cap drop est_ret
    gen est_ret=.
    levelsof sedol, local(sedols)
    foreach ii of local sedols {
    cap drop temp
    reg ret IndexReturn if Days>=-120 & Days< -20 & sedol=="`ii'"
    predict temp if sedol=="`ii'" & Days >= -20 & Days <=20
    replace est_ret=temp if sedol=="`ii'" & Days >= -20 & Days <=20
    }
    Any tips, advice or critics may help to avoid this error.

  • #2
    The error message means exactly what it says. There is some value of the variable sedols (there could be several, but the code will break when it encounters the first one) for which there simply are no observations having that value of sedol which meet the conditions for the -reg- command: Days between -120 and -20 and non-missing values for IndexReturn and ret. (I'm glossing over the possibility that this is happening because IndexReturn or ret is a string variable--in which case there will never be any observations in the regression.)

    This kind of problem comes up frequently when doing a bunch of regressions in a loop. If there is some reason why this problem definitely should not arise, then you need to figure out why your data set doesn't meet this expectation and fix the broken data set. Typically however, the nature of sporadic missing values on the regression variables, lead to some empty regressions. So here's what you can do:
    Code:
    cap drop est_ret
    gen est_ret=.
    levelsof sedol, local(sedols)
    foreach ii of local sedols {
        cap drop temp
        capture reg ret IndexReturn if Days>=-120 & Days< -20 & sedol=="`ii'"
        if c(rc) == 0 {
            //  SUCCESSFUL REGRESSION
            predict temp if sedol=="`ii'" & Days >= -20 & Days <=20
            replace est_ret=temp if sedol=="`ii'" & Days >= -20 & Days <=20
        }
        else if inlist(c(rc), 2000, 2001) {
            //  NO OBSERVATIONS OR INSUFFICIENT OBSERVATIONS
            //  ADVISE USER OF THE SITUATIOIN, BUT AS THIS IS
            //  AN EXPECTED PROBLEM, MOVE ON
            display "sedol == `ii': no or insufficient observations"
        }
        else {
            display as error "Unexpected error when sedol == `ii'"
            error c(rc)
        }
    }
    This code will provide the results you want for those values of sedol where the regression can be carried out. For those where there are insufficient or no observations for the regression, you will get a message saying so and telling you which value of sedol is involved. But the code will continue on. If any other error arises during the course of the regression, you will get an "Unexpected error" message that informs you of the offending value of sedol, and the code will halt.

    Note: Not tested because no example data was provided. The gist of this code is correct, but there may be typos or other errors.
    Last edited by Clyde Schechter; 10 Nov 2022, 18:22.

    Comment


    • #3
      Thanks, it works.

      I have another question.

      After calculated the exp return I calculated the AR= ret-exp ret per each stock.
      Now I was able to estimate the CAAR [-20 20] taking the mean from:
      Code:
      sum AR if Days>-20 & Days<20
      But, I am wondering how I can calculate the CAR [-20 +20] per each company. Do you have any suggestion please?

      Comment


      • #4
        Something like this:
        Code:
        rangestat (mean) AR, by(company_id) interval(Days -20 20)
        Replace company_id by the actual variable that identifies companies. As for the -interval()- option, I am assuming, though it is not completely clear from your post, that when you say [-20 +20] you are referring to the range of Days. If you actually mean the range of some other variable from -20 to +20, substitute the name of that variable where I have written Days.

        -rangestat- is written by Robert Picard, Nick Cox, and Roberto Ferrer. It is available from SSC.

        Comment


        • #5
          Thanks for the reply.

          Let me rephrase my question because I recognize that I was not clear enough and I hope I will succeed.

          Having 1713 different SEDOLs, my database look like this:
          sedol days AR CAR
          xxxxx -1 0.10 -0.15
          xxxxx 0 -0.22 -0.15
          xxxxx 1 -0.03 -0.15
          My goal now is to calculate a summation of ARs per each SEDOL because then I need to reg CARs with firms specific characteristics (EBITDA, D/E ratio...ecc.) and appreciate the causality.

          Comment


          • #6
            OK, moral of the story. You should always show example data when asking for help with code, and I should have known better than to assume I understood your data when you didn't. In #3 you wanted your results restricted to the observations with days between -20 and 20. In #5 it appears you no longer care about that, as you no longer mention it.

            If you are no longer interested in the restriction to days between -20 and 20, it's just
            Code:
            by sedol (days), sort: egen CAR = total(AR)
            If you are still interested in the restriction to days between -20 and 20, it's slightly more complicated:
            Code:
            by sedol (days), sort: egen CAR = total(cond(inrange(days, -20, 20), AR, .))
            Now, in this case your example data tableau gave me a clearer picture of your data than I had with nothing. But it is still incomplete in ways that, I think, will not affect the correctness and usability of the code I'm offering here. But sometimes there are still nasty surprises with that. So, in the future, when showing data examples, please use the -dataex- command to do so. If you are running version 17, 16 or a fully updated version 15.1 or 14.2, -dataex- is already part of your official Stata installation. If not, run -ssc install dataex- to get it. Either way, run -help dataex- to read the simple instructions for using it. -dataex- will save you time; it is easier and quicker than typing out tables. It includes complete information about aspects of the data that are often critical to answering your question but cannot be seen from tabular displays or screenshots. It also makes it possible for those who want to help you to create a faithful representation of your example to try out their code, which in turn makes it more likely that their answer will actually work in your data.

            Comment

            Working...
            X