Announcement

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

  • No observations after running forvalues command

    After running the following command for a number of companies I get the following error:

    forvalues i=1(1)487 {
    2. l id group_ID if id==`i' & dif==0
    3. reg ret market_return if id==`i' & estimation_window==1
    4. predict p if id==`i'
    5. replace predicted_return = p if id==`i' & event_window==1
    6. drop p
    7. }

    No observations
    r(2000);

    Does this error imply that I have missing values or observations that are equal to 0 in my dataset?

  • #2
    you don't make clear on what command the error occurs (you can use "trace" to help find it); the issue may be missing data (start with "misstable summarize") or it may be that some variable you think is numeric is actually string (check with "describe"); or it may be that estimation_window is never equal to 1, or ... we really need to know which line is causing the error message

    Comment


    • #3
      Richard Goldstein, thanks for your reply. Here is the describe output:

      date long %d Names Date
      company_ID str10 %10s CUSIP Header
      ret double %11.0g Returns without Dividends
      market_return double %10.0g Return on the S&P 500 Index
      set float %9.0g
      event_date long %d Announce Date, SAS Format
      group_ID float %9.0g group(company_ID)
      datenum float %9.0g
      td float %9.0g
      dif float %9.0g
      event_window float %9.0g
      count_event_obs float %9.0g
      estimation_wi~w float %9.0g
      count_est_obs float %9.0g
      id float %9.0g group(group_ID)
      predicted_ret~n float %9.0g

      And this is the error shown after using set trace on command:

      -------------------------------------------------- end _get_diopts ---
      - if _by() {
      `version' `BY' BYREG `anything' `if' `in' [`weight'`exp'], `options' `
      > diopts0' `diopts' `robust' `cluster'
      }
      - else {
      - `version' _regress `anything' `if' `in' [`weight'`exp'], `diopts0' `di
      > opts' `options' `robust' `cluster'
      = version 14.1, missing : _regress ret market_return if id==3 & estimati
      > on_window==1 [],
      no observations
      }
      -------------------------------------------------------- end regress ---
      -------------------------------------------------------------- end reg ---
      predict p if id==`i'
      replace predicted_return = p if id==`i' & event_window==1
      drop p
      }
      r(2000);

      It seems that the error occurs when ID 3 is running?

      Comment


      • #4
        If id is not continuous from 1 to 487, then you might want to use levelsof

        Code:
        levelsof id, local(levels)
        foreach l of local levels {
             reg ret market_return if id==`l' & estimation_window==1 
        ...
        }
        Another option would be to use capture

        Comment


        • #5
          Thanks Scorr Merryman for your reply, but I get the following output:

          . foreach l of local levels {
          2. reg ret market_return if id==`l' & estimation_window==1
          3. predict p if id==`i'
          4. replace predicted_return = p if id==`i' & event_window==1
          5. drop p
          6. }

          Source | SS df MS Number of obs = 7
          -------------+---------------------------------- F(1, 5) = 0.22
          Model | .000212989 1 .000212989 Prob > F = 0.6612
          Residual | .004916656 5 .000983331 R-squared = 0.0415
          -------------+---------------------------------- Adj R-squared = -0.1502
          Total | .005129644 6 .000854941 Root MSE = .03136

          -------------------------------------------------------------------------------
          ret | Coef. Std. Err. t P>|t| [95% Conf. Interval]
          --------------+----------------------------------------------------------------
          market_return | -1.861726 4.000251 -0.47 0.661 -12.1447 8.421246
          _cons | .0747753 .0675795 1.11 0.319 -.0989435 .248494
          -------------------------------------------------------------------------------
          invalid syntax
          r(198);

          Comment


          • #6
            As Scott Merryman suggests, you might have a non-continuous sequence of id numbers (so, no id #3). In this case you can use levelsof to loop over the actual id numbers as he suggests.

            And/Or, you might indeed have missing values on your dependent or independent variables. Using capture will suppress error messages and continue on with the loop.

            You can use capture with foreach or levelsof, but you'll need it with the following commands:
            Code:
            cap reg ret market_return if id==`i' & estimation_window==1
            cap predict p if id==`i'
            cap replace predicted_return = p if id==`i' & event_window==1
            cap drop p
            Stata/MP 14.1 (64-bit x86-64)
            Revision 19 May 2016
            Win 8.1

            Comment


            • #7
              Ercan,
              Run these commands all at once in the do-file editor. Type: doedit to open it. Local macros and loops can be tricky to run from the command line.

              Please use the [CODE][/CODE][ delimiters around any code or output to make it readable. See the FAQ for more information.

              Stata/MP 14.1 (64-bit x86-64)
              Revision 19 May 2016
              Win 8.1

              Comment


              • #8
                You wrote:

                Code:
                . foreach l of local levels {
                2. reg ret market_return if id==`l' & estimation_window==1 
                3. predict p if id==`i'
                4. replace predicted_return = p if id==`i' & event_window==1
                5. drop p
                6. }
                You have id == `l' in the regression statement and then id == `i' in the predict and replace statements

                Comment


                • #9
                  Thank you Carole J. Wilson for your reply, when using levelsof id command id #3 is included in the list. Moreover, I get the following when running the command you gave me:

                  gen predicted_return=.
                  (548,942 missing values generated)

                  . forvalues i=1(1)487 {
                  2. l id group_ID if id==`i' & dif==0
                  3. cap reg ret market_return if id==`i' & estimation_window==1
                  4. cap predict p if id==`i'
                  5. cap replace predicted_return = p if id==`i' & event_window==1
                  6. cap drop p
                  7. }

                  However, the predicted_return variables seems to be deleted after this command, how is this possible? Because I will need that variable to calculate abnormal returns.

                  Comment


                  • #10
                    Please read #7 above!!

                    Whenever the regression does not run (for whatever reason) there will be no predicted values--The predicted values will be missing for that the id where the regression failed.

                    To check if you have enough cases to estimate the regression for id #3:

                    Code:
                    **This will count the possible cases
                    count if id==3 & estimation_window==1
                    
                    **This will count the cases with no missing values
                    count if !mi(ret) & !mi(market_return) & id==3 & estimation_window==1
                    Do you have enough cases to run the regression?
                    Stata/MP 14.1 (64-bit x86-64)
                    Revision 19 May 2016
                    Win 8.1

                    Comment


                    • #11
                      Code:
                       count if id==3 & estimation_window==1
                      0

                      Code:
                        
                       count if id==3 & estimation_window==1  0
                      It seems that I have no cases to run the regression? but how is this possible

                      Comment


                      • #12
                        Only you can answer this. You have no data where id is 3 and estimation_window is 1. You can list the id's where estimation_window is 1:

                        Code:
                        levelsof id if estimation_window==1
                        Stata/MP 14.1 (64-bit x86-64)
                        Revision 19 May 2016
                        Win 8.1

                        Comment


                        • #13
                          Thanks for the reply. I have used the command you indicated, and it seems that there are more id numbers with no cases to run the regression. What can I do in such a case? Also I would like to know why the generated variable predicted_return is being deleted by Stata? Is this because the forvalues command is not able to finish going through all id numbers from 1 until 487?

                          Comment


                          • #14
                            Is changing the estimation window from 30 days to 15 days for example and option to have cases for all id numbers in my sample?

                            Comment


                            • #15
                              Your command predict fails if the regression fails, therefore the variable p is never created. When you get to the command drop p, Stata drops predicted_return because "p" is an acceptable abbreviation for predicted_return if there is no other variable that begins with p.

                              Option 1: Tell Stata to disallow abbreviations:
                              Code:
                              set varabbrev off
                              *this will turn off abbreviations until you turn them back on or
                              *the default abbreviation behavior will return when you open Stata again
                              
                              *Or, you can tell Stata to do it permanently
                              set varabbrev off, perm
                              Option 2: rename one of the variables:
                              Code:
                              cap predict px if id==`i'
                              cap replace predicted_return = px if id==`i' & event_window==1
                              cap drop px 
                              
                              *and /or:
                              cap predict p if id==`i'
                              cap replace _predicted_return = p if id==`i' & event_window==1
                              cap drop p

                              I have no idea about the estimation window. This is something you will have to determine on your own.
                              Stata/MP 14.1 (64-bit x86-64)
                              Revision 19 May 2016
                              Win 8.1

                              Comment

                              Working...
                              X