We wanted to create an do-file for a coefficient-estimation for the previos 36 months for each fund and then calculate a 12-month ahead estimate for alpha , but when we want to apply this do file to our dataset we always get the error message 'invalid syntax' r(198), but stata does not specify where our problem is.


//load dataset




gen SPret = benchmark - rf


//generate variables for results with momentum

gen alpha = .


gen beta_SPret = .

gen beta_smb = .

gen beta_hml = .

gen beta_mom = .

gen r2 = .

//generate variables for results (without momentum)

gen alpha_wom = .

gen beta_SPret_wom = .

gen beta_smb_wom = .

gen beta_hml_wom = .

gen r2_wom = .

//begin loop



//create group identifier with variable code

egen id = group(name)

sort name year month

sum id

local max `r(max)'


forvalues i = 1/`max' {


foreach id == `i' {

local y = min(calmt) //create local min of calmt to know where to start
local z = max(calmt) //create local max to identify ending point




di in red "`c(current_time)': @id: `i'" //show loop results


sum id if id == `i' // Calculate the sum of observations for each funds


if `r(N)' > 47 { // There have to be at least 47 months calmt observations, because the coefficients are calculated over a period of 36 months and then an alpha is calculated 12 months ahead



rolling _b, start(y) window(36) end(z-12) saving(betas_m) stepsize(1) : reg mgret SPret smb hml mom



replace alpha = _b[_cons] if id == `i'


replace beta_SPret = _b[SPret] if id == `i'


replace beta_smb = _b[smb] if id == `i'


replace beta_hml = _b[hml] if id == `i'


replace beta_mom = _b[mom] if id == `i'


replace r2 = e(r2) if id == `i'
}
}
}

di in red "`c(current_time)': @id: `i'" //show loop results

sort name year month

sum id

local max `r(max)'


forvalues i = 1/`max' {

foreach id == `i' {


local y = min(calmt) //create local min of calmt to know where to start
local z = max(calmt) //create local max to identify ending point

di in red "`c(current_time)': @id: `i'" //show loop results

if `r(N)' > 47 {
//regression without momentum
rolling _b, start(y) window(36) end(z-12) saving(betas_wom) stepsize(1): reg mgret SPret smb hml




replace alpha = _b[_cons] if id == `i'


replace beta_SPret = _b[SPret] if id == `i'



replace beta_smb = _b[smb] if id == `i'



replace beta_hml = _b[hml] if id == `i'



replace r2_wom = e(r2_wom) if id == `i'

}
}


}


}