Dear Colleagues

I have daily stock returns and market return for 100 firms and for a number of years (Panel). I want to run a regression for each firm and year (i.e. my observations are my daily data) and save the residuals from each regression. So I wrote this:


Code:
egen group = group(Firm Year)
su group, meanonly

gen resi=.

forval g = 1/`r(max)' {
     regress StockReturn MarketReturn if group == `g'
     predict r ,resid 
     replace resi = r if group == `g'
     drop r
     
}
it worked. I now want to only run the regressions if I have at least 100 daily observation in each firm-year group. I just do not know how to integrate this condition in the loop!

Any Idea?