Hello everyone,
I am kind of new to Stata and have been reading in this forum a lot. I learned a lot from it so far, but now it seems like I am stuck.
Stataversion: Stata SE 14.2
Here is my issue:
I am trying to build a dynmiac new regression for a sample of companies in a semi annually interval. Thus, I need to run a regression, identify the lags for newey and then use newey with the correct amount of lags. I build a loop and so far it worked out perfectly for complete datasets - but as soon as one company is lacking observations for a timeperiod, it breaks the loop with 'r(2000) no observations'.
Here is how my data is structured (excerpt) :
This is the code I am currently using:
The code breaks with no observations for 2001/1 in u2.
I already tried to work with capture and read a lot of topics and discussions & the stata help tutorials, but somehow I can not fit in capture correctly it seems: I tried to put it before the inner loop: capture foreach element of varlist u* {. This makes the code work, but although there are observations for 2001/1 & u3 it just skips them and continues listing the values for 2001/2 & u1. Thus the output for 2001/1 only includes the newey regression for u1.b Since in 2001/2 there are observations for u2 as well, it does not break here and lists values for u1, u2 and u3 in 2001/2.
Please note, I am new to stata, thus I might have some misunderstanding regarding the capture command here.
Can anyone relate to this and help me out here?
Thank you so much in advance
I am kind of new to Stata and have been reading in this forum a lot. I learned a lot from it so far, but now it seems like I am stuck.
Stataversion: Stata SE 14.2
Here is my issue:
I am trying to build a dynmiac new regression for a sample of companies in a semi annually interval. Thus, I need to run a regression, identify the lags for newey and then use newey with the correct amount of lags. I build a loop and so far it worked out perfectly for complete datasets - but as soon as one company is lacking observations for a timeperiod, it breaks the loop with 'r(2000) no observations'.
Here is how my data is structured (excerpt) :
| ID | year | key | fx | u1 | u2 | u3 |
| 1 | 2001/1 | 1 | 0.1 | 0.1 | . | 0.2 |
| 2 | 2001/1 | 1 | 0.2 | -0.3 | . | -0.1 |
| 3 | 2001/1 | 1 | 0.1 | 0.2 | . | 0.2 |
| ... | ... | ... | ... | ... | ... | ... |
| 131 | 2001/2 | 1 | 0.3 | 0.5 | 0.1 | 0.3 |
| ... | ... | ... | ... | ... | ... | ... |
| 2000 | 2008/2 | 16 | 0.2 | -0.1 | 0.3 | -0.2 |
This is the code I am currently using:
Code:
local i=0
foreach a of numlist 1/16 {
local i=`i'+1
foreach element of varlist u* {
regress `element' fx if key==`i'
estat durbinalt, lags(1/3)
matrix A = r(p)
quietly scalar A1 = A[1,1]
quietly scalar A2 = A[1,2]
quietly scalar A3 = A[1,3]
quietly scalar B = 0.05
quietly if(A1>B) {
scalar C = 0
}
quietly if(A1<B) & (A2>B) {
scalar C = 1
}
quietly if(A1<B) & (A2<B) & (A3>B) {
scalar C = 2
}
newey `element' fx, lag(`=C'), if key==`i'
matrix test= nullmat(test) \ _b[FX]
local regressanden `regressanden'
}
}
matrix colnames test = gamma
matrix rownames test = `regressanden'
matrix list test
I already tried to work with capture and read a lot of topics and discussions & the stata help tutorials, but somehow I can not fit in capture correctly it seems: I tried to put it before the inner loop: capture foreach element of varlist u* {. This makes the code work, but although there are observations for 2001/1 & u3 it just skips them and continues listing the values for 2001/2 & u1. Thus the output for 2001/1 only includes the newey regression for u1.b Since in 2001/2 there are observations for u2 as well, it does not break here and lists values for u1, u2 and u3 in 2001/2.
Please note, I am new to stata, thus I might have some misunderstanding regarding the capture command here.
Can anyone relate to this and help me out here?
Thank you so much in advance

Comment