You are correct that there is a problem
with your syntax. It is illegal.
Try something more like this:
levels year, local(Y)
gen predict = .
qui foreach y of local Y {
reg y x if year == `y'
predict temp
replace predict = temp if year == `y'
drop temp
}
Nick
[email protected]
R.E. De Hoyos
> How can I regress several cross-sections, predict the
> values by year and
> amend them into the main dataset?
>
> I have an unbalanced panel and I want to run a regression
> for each year:
>
> bys year reg y x
>
> The point is to save the y-hat (predicted values) emanated
> from the year by
> year regressions into a single column. I tried the
> -foreach- command put
> apparently there is a problem with the syntax:
>
> foreach num of local year {
> reg y(num) x
> predict y(num)
> }
*
* For searches and help try:
* http://www.stata.com/support/faqs/res/findit.html
* http://www.stata.com/support/statalist/faq
* http://www.ats.ucla.edu/stat/stata/