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)
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
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 }
1.If I run the codes I got this message:
Code:
no observations r(2000);
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'
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).
0 Response to Problem in running loop for regression-industry-year
Post a Comment