I think there are a few problems with the code. The fundamental problem is that -predict- needs to create a new variable, so after you've run -predict- once it won't allow you to generate new predictions with the same variable name. One way around this is to create a variable for your predictions with missing values, use -predict- to place predictions in a temporary variable, then -replace- your variable with the values from the temporary variable (see code below).
A couple of other comments about the code:
This -levelsof- command creates a macro called `x' containing all values of sic; but then the -foreach- command immediately overwrites `x' with the number 10. It doesn't look like you need the -levelsof- command at all.
You should just be able to say -if e(sample)- because the other conditions were already specified when you ran the regression. e(sample) is true if the observations were included in the regression.
I'm not sure what you're trying to achieve here. In your code pcfo is a variable name, not a macro name. Using the single quotes `pcfo' is the way to reference a local macro, and putting it in double quotes as "`pcfo'" means you're getting the value of the local macro `pcfo' and converting it to a string - surely this is not what you're trying to do.
You don't need to use -end- here; you use it to end a program (see -help program-) or after manually entering data using the -input- command.
Suggested fix:Code:gen pcfo=. // empty variable for predictions gen rcfo=. // empty variable for residuals tempvar pcfo rcfo // temporary variables for each set of predictions foreach x of numlist 10/87 { foreach z of numlist 2005/2010 { reg cfo at s sch if sic==`x' & fyear==`z', noconstant predict `pcfo' // predictions are now in temporary variable replace pcfo=`pcfo' if e(sample) // transfer predictions from temp variable predict `rcfo' // residuals are now in temporary variable replace rcfo=`rcfo' if e(sample), residuals // transfer residuals from temp variable drop `pcfo' `rcfo' // drop temporary variables in preparation for next regression } }





Reply With Quote

