Announcement

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

  • 'i' invalid name, can someone help? :)

    Hi,

    I am trying to do an event study for my thesis to eventually get the cumulative abnormal returns. I want to do predict the expected return based on CAPM, the FF three-factor model and four-factor model. I tried doing it for CAPM with mktrf only. Below is the code I have so far. I got the code from a guide from Princeton (https://dss.princeton.edu/online_hel...ventstudy.html) which I adjusted to my own variables and event time window (estimation window [-131,-21] and event window [-20,5]. Everything works until I tried to estimate the normal performance.
    When I try to execute the forvalues command, I get the following error: ' i invalid name'.

    Can someone explain me how I can solve this?
    I do not have a lot of experience with Stata so I am not sure where and what to do with i.


    The code so far:
    rename co_id company_id
    rename anndats event_date
    sort company_id date
    by company_id: gen datenum=_n
    by company_id: gen target=datenum if date==event_date
    egen td=min(target), by(company_id)
    drop target
    gen dif=datenum-td

    *event window [-20,5]
    *estimation window [-130,-21]
    by company_id: gen event_window=1 if dif>=-20 & dif<=5
    egen count_event_obs=count(event_window), by(company_id)
    by company_id: gen estimation_window=1 if dif<-21 & dif>=-130
    egen count_est_obs=count(estimation_window), by(company_id)
    replace event_window=0 if event_window==.
    replace estimation_window=0 if estimation_window==.

    tab company_id if count_event_obs<5
    tab company_id if count_est_obs<109

    drop if count_event_obs < 5
    drop if count_est_obs < 109

    *everything above worked, with the commands below I got the error

    gen predicted_return1=.
    egen id=group(company_id)
    summarize id
    forvalues i=1(1)5790 {
    l id company_id if id=='i' & diff==0
    reg ewretd mktrf if id==`i' & estimation_window==1
    predict _p if id==`i'
    replace predicted_return1 = _p if id==`i' & event_window==1
    drop _p
    }

    gen predicted_return=. and egen id=group(company_id) worked.
    Maximum N for id is 5790.
    But after that I get the error: 'i invalid name'
    Hopefully someone can help me, it would be much appreciated!!

  • #2
    Replace the line
    Code:
    l id company_id if id=='i' & diff==0
    with
    Code:
    l id company_id if id==`i' & diff==0
    The problem is the first ' , instead you need ` . These two signs might look similar to you and me, but for Stata it makes a difference. The characters `' are used to denote a local macro. In your case, the macro has the name i and is your counter in the loop.

    Comment


    • #3
      It is running now, so I think it works. Thanks a lot!

      Comment

      Working...
      X