Dear all

I'm calculating a variable, that is the residuals from a regression calculated for every industry and year. Actually, I found this code on the web and I adapt for my analysis, studying the underlying intuition.
In the first block, i check for duplicates, setting the panel and calculating the variables for the regression
Code:
duplicates report gvkey fyear
duplicates drop gvkey fyear, force
                    
xtset gvkey fyear, yearly
                
gen deltasale=d.sale
gen cfo_at=oancf/lagta
gen one_at=1/lagta
gen sale_at=sale/lagta
gen deltasale_at=deltasale/lagta

drop if missing(cfo_at)
drop if missing(one_at)
drop if missing(sale_at)
drop if missing(deltasale_at)
In the second block, since for this model I need at least 15 observations for every firm(gvkey)-year(fyear) combination, and the model is industry specific (first two digit of SIC codes is the industry), I create the "sic2id" and keeping only those with at least 15 observations.

Code:
destring sic, replace
drop if  sic>4399 & sic<5001
drop if sic>5999 & sic<7000

tostring sic, replace
drop sic2 sic2id
generate sic2=substr(sic,1,2)
destring sic2, replace
drop if missing(sic2)
egen sic2id=group(sic2 fyear)
egen count= count(sic2id), by (sic2id)
keep if count>=15
In the third part I run the loop and capturing the residuals that is my variable of interest
Code:
set more off

gen abn_cfo=.
                                          
sum sic2id
local max=r(max)

forvalues i = 1/`max' {

 di "Calculating abnormal cashflow" `i' " of " `max'
 qui reg cfo_at one_at sale_at deltasale_at if sic2id == `i'
 qui predict res if sic2id == `i', res
 qui replace abn_cfo = res if sic2id == `i'
 qui drop res
 
}
I have a couple of issue with that that I'm doing all my best to solve it alone (I'm not an expert in loops)
1.If I run the codes I got this message:

Code:
no observations
r(2000);
The loop starts, but after 100 firms it stops with the message above. I think that I should include something that tells Stata to go to the next in case there are no observations. Am I right? Do you have any suggestion?

2. As you can see, before the loop I have this constraint "keep if count>=15" but I'm wondering on how don't lose those observations and still running the regression with this condition. I know may sound stupid to you, but what about if I change inside the loop this:
Code:
qui reg cfo_at one_at sale_at deltasale_at if sic2id == `i'
with this one
Code:
qui reg cfo_at one_at sale_at deltasale_at if sic2id == `i' & if count>=15
?

Finally, as you can see from the first block I'm dropping if missings some variables (cfo_at, one_at,sale_at and deltasale_at). As I said I found these codes on the web and I'm wondering if this part is redundant or not. Since I need them for the regression, does stata already take into account if all variables are no missing before run the regression?

Sorry for the long post, I tried to be as clear as I can and I hope you may help me with this points (even if sounds really easy to you).