Hello everyone,
It has been a while since I posted here, and I miss it
I am currently working with a panel dataset that spans 15 years and includes firms from different countries, operating in different industries.
I wish to run a regression for each group of firms that operate in the same country, within the same industry, and in a given year. Then I need to store the residuals obtained from each regression (I placed a condition that the regression should have a minimum of 10 observations).
Before inserting the code of the loop that I wrote, I shall define a few variables:
Y: dependent variable
X: independent variable
CountryCode: a string variable that stores the code of the corresponding country
IndustryClass: takes the values 0-11, each number referring to an industry
Year: takes the values 1991-2015
Residuals: the variable in which I store the residuals estimated from the regressions
Therefore, I wrote the following code (loop):
The code I wrote does not return an error; however, it does nothing. That is, there are no regressions running following the execution of the code and thus the variable Residuals has no values.
I truly appreciate your help in finding the wrong piece(s) in the code above.
Thank you very much indeed.
Mostafa
It has been a while since I posted here, and I miss it

I am currently working with a panel dataset that spans 15 years and includes firms from different countries, operating in different industries.
I wish to run a regression for each group of firms that operate in the same country, within the same industry, and in a given year. Then I need to store the residuals obtained from each regression (I placed a condition that the regression should have a minimum of 10 observations).
Before inserting the code of the loop that I wrote, I shall define a few variables:
Y: dependent variable
X: independent variable
CountryCode: a string variable that stores the code of the corresponding country
IndustryClass: takes the values 0-11, each number referring to an industry
Year: takes the values 1991-2015
Residuals: the variable in which I store the residuals estimated from the regressions
Therefore, I wrote the following code (loop):
Code:
gen Residuals=. levelsof CountryCode, local(country) foreach c of local country { levelsof Year if CountryCode==`c', local(years) foreach y of local years { levelsof IndustryClass if CountryCode==`c' & Year==`y', local(industry) foreach i of local industry { quietly count if !missing(Y, X) if r(N) >= 10 { capture regress Y X if CountryCode==`c' & Year==`y' & IndustryClass==`i' predict uhat if e(sample), residuals replace Residuals=uhat if e(sample) drop uhat } } } }
The code I wrote does not return an error; however, it does nothing. That is, there are no regressions running following the execution of the code and thus the variable Residuals has no values.
I truly appreciate your help in finding the wrong piece(s) in the code above.
Thank you very much indeed.
Mostafa
Comment