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

# st: RE: forval predict values into same column

 From Nick Cox To "'statalist@hsphsun2.harvard.edu'" Subject st: RE: forval predict values into same column Date Mon, 24 Jan 2011 20:22:40 +0000

```Q1.

tempvar work
gen Y1HAT = .

forval i=1/6 {
regress Y1 X1 X2 if GROUPID ==`i'
predict `work' if e(sample)
replace Y1HAT = `work' if e(sample)
drop `work'
}

Q2.

su GROUPID, meanonly

forval i = 1/`r(max)' {

But this will get into difficulties if there are gaps in the observed values. For more general technique, see

FAQ     . . . . . . . . . . Making foreach go through all values of a variable
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  N. J. Cox
8/05    Is there a way to tell Stata to try all values of a
particular variable in a foreach statement without
specifying them?
http://www.stata.com/support/faqs/data/foreach.html

Nick
n.j.cox@durham.ac.uk

Michael Bondegård

1. question 1

I wonder how to make this
forval i=1/6 {
regress Y1 X1 X2 if GROUPID ==`i'
predict Y1HAT if e(sample)
}

When i do this it tells me  Y1HAT already defined (so it stops after
second run, return just values first time)

I know that i can do this:
forval i=1/6 {
regress Y1 X1 X2 if GROUPID ==`i'
predict Y1HAT_`i' if e(sample)
}

As you see i add same number to the predictor that way i can go trough
this problem but what i want is to put the prediction in same column
from the start.

This is because the GROUPID is diffrent for each prediction so it wont
overwrite anything once it input the prediction values into it.

2, question 2
How to make return a maximum value for the GROUPID, as you see in the
example above the forval runs 6 timas (6 is the max in GROUPID)

My question is, is there any way to make the similar to this wish:
forval i=1/MAX(GROUPID)

I been looking for the right answer but no luck:(

*
*   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/
```