Announcement

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

  • Stata Loop using variables

    Hello everyone,

    I am running an event study on a panel data of US stocks, the events occur quarterly normally but are missing for some stocks and quarters. I am using the estimation window to predict the normal returns of an event-window. The data looks like this:

    Code:
    clear
    input float(permno date ret sp500 event permqtr TD event_window event_no est_window_90 ar_sd)
    1  1   1 .5 0 1  1 0 0 1 2.3220067
    1  2 -.5 .6 0 1  2 0 0 1 2.3220067
    1  3  .5 .3 0 1  3 0 0 1 2.3220067
    1  4  .6 .4 0 1  4 0 0 1 2.3220067
    1  5 -.5 .5 0 1  5 0 0 1 2.3220067
    1  6 -.6 .6 0 1  6 0 0 1 2.3220067
    1  7   7 .3 1 1  7 1 1 0 2.3220067
    1  8   6 .4 0 1  8 1 1 0 2.3220067
    1  9   4 .5 0 1  9 1 1 0 2.3220067
    1 10   8 .6 0 1 10 1 1 0 2.3220067
    1 11   3 .3 0 1 11 1 1 0 2.3220067
    1 12  .6 .4 0 1 12 0 1 . 2.3220067
    1 13 -.5 .5 0 1 13 0 1 2 2.3220067
    1 14 -.6 .6 0 1 14 0 1 2 2.3220067
    1 15  .5 .3 0 1 15 0 1 2 2.3220067
    1 16   1 .4 0 2 16 0 1 2  .6046011
    1 17 -.5 .5 0 2 17 0 1 2  .6046011
    1 18  .5 .6 0 2 18 0 1 2  .6046011
    1 19  .6 .3 0 2 19 0 1 2  .6046011
    1 20 -.5 .4 0 2 20 0 1 2  .6046011
    end
    
    I am running the following code to predict the returns and it works alright.
    Code:
    gen firmno=group(permno)
    gen predict_ret=.
    forvalues i=1(1)10506 {
    forvalues j=1(1)37 {
    capture noisily reg ret sp500 if firmno==`i' & est_window_90==`j'
    if c(rc) == 0 {
    predict p if firmno==`i'
    replace predict_ret = p if event_window==1 & firmno==`i' & event_no==`j'
    drop p
    }
    }  
    }
    The problem is searching through all firm-quarters with or without an event. My life would become much more simple if I could replace the numeric values in the for loops with variables. is that possible to do? Any help is appreciated.
    Hamid

  • #2
    Code:
     
     gen firmno=group(permno)
    is almost certainly wrong here. You should mean
    Code:
       
     egen firmno=group(permno)
    There is a function group() outside egen. See https://www.stata.com/statalist/arch.../msg00406.html Otherwise, sorry, but I don't understand the question.

    Comment


    • #3
      Thanks for noting Nick, yes that was a typo in my code which I later fixed. To reiterate my problem, I don't want to go through all 37 values of j for each firmno. I can store the specific values of j I need for each firmno in a new variable k, but I cannot run the loop replacing the number 37 with any variable.

      Comment


      • #4
        The following code partly solves the problem

        Code:
         forvalues i=1(1)10506 {
        egen x=mean(evcount) if firmno==`i'
        if x!=0{
        levelsof event_no, local(k)  
        foreach j of local k {
        capture noisily reg ret sp500 if firmno==`i' & est_window_90==`j'
        if c(rc) == 0 {
        predict p if firmno==`i'
        replace predict_ret = p if event_window==1 & firmno==`i' & event_no==`j'  
        drop p
        }
        }  
        }
        Last edited by Hamidreza Roohian; 13 Dec 2021, 13:13.

        Comment

        Working...
        X