Hey Everyone,

I'm currently trying to run a regression/prediction of some abnormal returns. All good so far, but during my iteration, the command constantly breaks if my data set (announcedates) do not match the respective trading dates. My dataset contains daily stock returns in a time span from 2009 until 2019. Furthermore I matched respective announce dates into the data set. Due to size, I cannot upload it here. Hope the code is enough, otherwise I will supply more information if required.
The dataset also grouped bond announcements intro groups which are sorted by G_ID. So for every G_ID i want to iterate the predicted return based on a given event window around the announce date.
The code I am running currently is this one:


sort G_ID date
by G_ID: gen datenum=_n
by G_ID: gen target=datenum if date==announcedate
egen td=min(target), by(G_ID)
drop target
gen dif=datenum-td

//create Event Window

by G_ID: gen event_window=1 if dif>=-1 & dif<=0
egen count_event_obs=count(event_window), by(G_ID)
by G_ID: gen estimation_window=1 if dif<-2 & dif>=-202
egen count_est_obs=count(estimation_window), by(G_ID)
replace event_window=0 if event_window==.
replace estimation_window=0 if estimation_window==.


//predicted returns

gen predicted_return=.
egen id=group(G_ID)
sum id
forvalues i=1(1)`r(max)' {
l id G_ID if id==`i' & dif==0
capture quietly reg return market_return if id==`i' & estimation_window==1
capture noisily predict p if id==`i'
capture noisily replace predicted_return = p if id==`i' & event_window==1
capture noisily drop p
}


// generate abnormal returns based on predictions for estimated return.*/

sort id date
gen abnormal_return=return-predicted_return if event_window==1
by id: egen cumulative_abnormal_return = sum(abnormal_return)

/*Test for statistical Significance
//70 is number of days in event window. If prolonged, change digets */

sort id date
by id: egen ar_sd = sd(abnormal_return)
gen test =(1/sqrt(count_event_obs)) * (cumulative_abnormal_return/ar_sd)
quietly list G_ID cumulative_abnormal_return test if dif==0

/*export data*/
export excel G_ID announcedate cumulative_abnormal_return test using "stats_group_announcedate01_large.xls" if dif==0, firstrow(variables) replace

//calculate the cumulative abnormal for all companies treated as a group
reg cumulative_abnormal_return if dif==0, robust

In the red marked area my code typically bugs around.

Before reading about capture function, I used this code:

forvalues i=1(1)`r(max)' {
l id G_ID if id==`i' & dif==0
quietly reg return market_return if id==`i' & estimation_window==1
predict p if id==`i'
replace predicted_return = p if id==`i' & event_window==1
drop p
}

which lead to constant breaks with the error R2000. Thus is tried to establish a capture command! What I want is for the code to display a zero as predicted return, instead of an error code which breaks the code. Then i want to analyse what exactly leads to the break (i.e. announcedate on a weekday, instead of a trading day). It appears useless to repeat entering the code over and over again, after it breaks and i thus just want it to make "zero changes" instead of stopping.

Would be really really happy if you could provide some help here! Hope im also complying with all rules applying here, as this is my very first post!!

Best

Kai