Hello everyone.

I am trying to do a loop to calculate CARs (Cumulative Abnormal returns) for an Event Study. My estimation window is (-300, -91) and my event window is (-2,+2). I have a dataset with market returns and firm returns and the event information, over the 302 days for each deal. Deals were gathered from Thomson One over the period 1990-2011 and the returns from CRSP with cusip identifiers for firms, period 1989-2012.

I tried to run this loop but it didn't work and I cannot understand why.

I want to sort by cusip and date of announcement as i am interested in the deals made by each firm over the period.

Code:
foreach i in 1/`r(max)' {

*Estimate market model parameters
preserve
keep if t < -91
keep if t > -300
sort cusip DateAnnounced
quietly by cusip: reg company_ret mrkt_ret

* Compute abnormal returns and corresponding t-stats
predict exp_ret
gen `ar' = company_ret - exp_ret
gen `car' = ar if t> -2 & t < 2
gen `std_car' = ar if t > -300 & t < -91
collapse (sum) car (sd) std_car, by(cusip)
gen `car_divided' = car / (sqrt(3)*std_car)
}
Do you have maybe some suggestions or hints on what needs to be fixed?