Dear Stata comunity,

I need to run month-wise fama and french three factor model regressions using daily data. I have got the following code from one of my friends, but it has some bug. When i run this code, it works only for one stock for one year. Further i require that the code should ignore the stock-month combinations where no of daily returns observation is less than 17.


egen ccode = group(Stock)
egen count_check = count(ccode), by(ccode month year) /* Generate the count of the unique_id combination to check if there are minimum number of observations for the regression */
drop if count_check <=1 /* drops all firm month combinations where the number of trading days is less than 1 */


egen ccode_month_year = group(Stock month year) /* I have groued it by stock month and year so that you can use this code for other years too */
gen resid_reg =. /* Creating a variable to store the residuals */

/* You have to count the number of unique firm_month id... for that you can use the following command */
egen max = max(ccode_month_year)
disp max

/* This will do the regressions and store the residuals in the resid_reg variable */

forvalues i = 1(1)9 {
regress Rt Mkt SMB HML if ccode_month_year==`i'
predict temp , resid
replace resid_reg = temp if ccode_month_year==`i'
drop temp
}


Kindly look into it and advise.