I would like to learn how to retain a scalar for a given statistic for each level of the sort variable in byable programs.
Please consider the following trivial example in which I might like to save the R-square statistic from each regression when sorting on the variable foreign in the auto.dta data set. Obviously, as written this program will only store the scalar from the final pass of the byable routine.
I don't know how to distinguish separate values for the retained scalar r(Rsq) with distinct scalar names such that they are not over-written with each pass of the byable routine.
I could easily do this with a loop because I can use the loop's step value in naming the stored scalar. That approach would avoid the need to use a byable program, but I'd like to learn how to do this in the case of byable programs if that is possible.
Thanks for any advice you may offer.
Red Owl
Please consider the following trivial example in which I might like to save the R-square statistic from each regression when sorting on the variable foreign in the auto.dta data set. Obviously, as written this program will only store the scalar from the final pass of the byable routine.
Code:
sysuse auto, clear
cap prog drop bytest
prog def bytest, rclass byable(recall)
syntax [varlist] [if] [in]
marksample touse
qui regress `varlist' if `touse'
di "R-square = " %5.4f e(r2) " "_n
* How do I give the following scalar a distinct name
* for each level of the byable sort variable?
return scalar Rsq = e(r2)
end
bysort foreign: bytest price mpg weight
I could easily do this with a loop because I can use the loop's step value in naming the stored scalar. That approach would avoid the need to use a byable program, but I'd like to learn how to do this in the case of byable programs if that is possible.
Thanks for any advice you may offer.
Red Owl
Comment