Bookmark and Share

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

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

st: RE: RE: forval predict values into same column

From   Nick Cox <>
To   "''" <>
Subject   st: RE: RE: forval predict values into same column
Date   Mon, 24 Jan 2011 20:36:25 +0000

Jesper's answer to Q2 gives the right answer, but it is never necessary. -summarize, meanonly- yields the maximum directly and efficiently, so that putting that constant into a variable and then getting it out again is by comparison roundabout. 


Jesper Lindhardsen

Regarding Q1 I would use -egen rowmax- or -egen rownonmiss- to gather the data in one column after the loop, e.g., 


Egen jointpredictvar=rowmax(y1hat*)

As for Q2 you can do it several ways. One approach could be

egen maxgroup=max(groupid)
Local n = maxgroup[1]

Forval i=1/`n' {

On Behalf Of 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)

*   For searches and help try:

© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index