Statalist The Stata Listserver


[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

Re: st: RE: accessing variables created within a foreach loop


From   "Austin Nichols" <austinnichols@gmail.com>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: RE: accessing variables created within a foreach loop
Date   Fri, 9 Jun 2006 10:11:43 -0400

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/



© Copyright 1996–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index