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
Related Posts with Problem in running a loop - does not stop
Creating a variable that counts the number of respondents in different ethnic groups per yearGreetings, I'm running Stata 15.1 and working with data from the American Community Survey. My goal…
Small sample mixed methods with varying degrees of freedom and lincomHi Stata experts! I have a situation that I've been unable to Google my way to a resolution. I hav…
"record too long" in Stata16 MPI was dealing with a dataset (about 50 thousand variables and unknown observations) download from So…
Cannot get ipdfc to workI happened to come across a user written program which might be the solution to what I am attempting…
Diff-in-diff analysis with two filesDear Stata community I am having some problems with my data. I want to run a diff-in-diff analysis …
Subscribe to:
Post Comments (Atom)
0 Response to Problem in running a loop - does not stop
Post a Comment