Vera E. Troeger--
If you want to use the variables *_m later (outside of the program),
you cannot make them tempvars. Try something like this instead:
sysuse auto, clear
cap prog drop test
program define test, rclass
syntax varlist , Ext(str) Ivar(varlist) [replace]
foreach z of local varlist {
sort `ivar'
tempvar `z'_tmp
by `ivar': egen ``z'_tmp'= mean(`z')
cap g `z'`ext'=``z'_tmp'
if _rc!=0 {
cap `replace' `z'`ext'=``z'_tmp'
}
local newvars `newvars' `z'`ext'
}
return local newvars "`newvars'"
end
test len price, e(_m2) i(for)
ret li
*
* 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/