Dear all,

I am currently trying to write a code to estimate the abnormal cash flow for my dataset with 70,608 observations and 30 variables.

After checking for duplicates and creating a date variable useable with xtset, I ran the commands to calculate the abnormal cash flow, including a loop. The code that I ran is:

gen oancf_ram=oancf/L.at
gen l_at=L.at
gen ram1=1/L.at
gen ram2=sale/L.at
gen ram3=S.sale/L.at

levelsof obs, local(levels)
foreach x of local levels {
gen mark=1 if obs==run
gen sic_lp=sic_2 if obs==run
qui summ sic_lp
replace sic_lp=r(mean) if sic_lp==.
gen datadate_lp=date if obs==run
qui summ datadate_lp
replace datadate_lp=r(mean) if datadate_lp==.
format datadate_lp %ty
gen sample=1 if sic_lp==sic_2 & datadate_lp==date & l_at!=. & oancf_ram!=. & ram1!=. & ram2!=. & ram3!=.
egen sample_sum=sum(sample) if mark!=1
capture reg oancf_ram ram1-3 if sample==1 & mark!=1 & sample_sum>3
capture predict u_hat_temp, resid
capture replace u_hat_ram1=u_hat_temp if obs==run & u_hat_ram1==.
drop mark sic_lp datadate_lp sample sample_sum
capture drop u_hat_temp
replace runn=runn+1
}

The levelsof obs, local(levels) shows all 70,608 observations. However, afterwards, the loop runs for the first time quickly, then it keeps on repeating this:

(70,607 missing values generated)
(70,607 missing values generated)
(70,607 real changes made)
(70,607 missing values generated)
(70,607 real changes made)
(69,793 missing values generated)
(1 missing value generated)

Whereby only the 69,793 sometimes changes.

I assume that the mistake is in the line " foreach x of local levels { "
But I cannot find out how to properly change it.

I hope one of you can help me. Thank you in advance.

Hidde