I concur with the advice to start
with -levels-, but this seems a little
more complicated than it need be.
levels company, local(C)
gen zresid = .
qui foreach c of local C {
regress y x1 x2 x3 if company == `c'
predict temp, rstudent
replace zresid = temp if company == `c'
drop temp
}
save data_with_zresid
Good idea Nick. This should save a bit of file-processing time. (I must
have been thinking in "SAS mode", which is only appropriate if the output
data set has observations corresponding to something other than the
observations of the original data set.)