Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.

# Re: st: RE: predicting residuals in regression

 From Rudy Hover To statalist@hsphsun2.harvard.edu Subject Re: st: RE: predicting residuals in regression Date Wed, 9 May 2012 19:09:16 +0200

```oh i'm sorry that was a typo, the code is

gen residuals=.
levelsof firmno, local(firmnolist)

forvalues y=1999/2006 {
foreach f of local firmnolist {
reg VarA VarB if firmno==`f' & year==`y'
predict temp, residuals
replace residuals=temp if firmno==`f' & year==`y'
drop temp
}
}

I found out that the regression is running, but only for (some of) the year 1999 the residuals are generated.

However after 26 iterations which look like

405566 missing values generated

Stata errors with:
no observations
r(2000);

The 12 real changes are correct: that's the 12 months per year for which it creates residuals. How do I find out for what variable it finds no observations?

On May 9, 2012, at 6:59 PM, Nick Cox wrote:

> Note that your inner loop is
>
> foreach f of local firmnolist
>
> whereas you started with
>
> levelsof permno, local(permnolist)
>
> I guess you need
>
> levelsof firmno, local(firmnolist)
>
> before your loops. Otherwise the macro -firmnolist- will be empty and no regressions will be carried out.
>
> Nick
> n.j.cox@durham.ac.uk
>
> Rudy Hover
>
> I have panel on thousands of firms over 6 years. I want to regress using the foreach command:
>
> gen residuals=.
> levelsof permno, local(permnolist)
>
> forvalues y=1999/2006 {
> foreach f of local firmnolist {
> reg VarA VarB if firmno==`f' & year==`y'
> predict temp, residuals
> replace residuals=temp if firmno==`f' & year==`y'
> drop temp
> }
> }
>
> However I'm getting a variable residuals only containing missing values (.)
> What am I doing wrong?
>
> *
> *   For searches and help try:
> *   http://www.stata.com/help.cgi?search
> *   http://www.stata.com/support/statalist/faq
> *   http://www.ats.ucla.edu/stat/stata/

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/
```