--On Monday, September 23, 2002 2:33 -0400Caleb wrote:
I want to collapse a large number of variables by tract, i.e.
. collapse (sum) _all, by(tract)
Naturally, stata complains that tract is included in _all:
tract may not be both target and by()
r(198);
Any clever way around that? Perhaps defining a macro that excludes one
name? I could type it all in, but this procedure has to be repeated for
many datafiles with different varlists (but which include tract).
There may (probably) be a better way to do this, but...
* allbut varname -> r(allbut) contains all vars but that one
* cfb 23sep2002
program define allbut,rclass
version 7
syntax varlist(max=1)
_allvars _all
local allvar `r(allvars)'
local allbut : subinstr local allvar "`varlist'" "",word
return local allbut `"`allbut'"'
end
program define _allvars,rclass
syntax varlist
return local allvars `varlist'
end
. allbut med
. collapse (sum) `r(allbut)',by(med)
Could readily be extended to allow for multiple items in the bylist.
Kit
________________________________________________________________________
Christopher F Baum, Boston College Economics, Chestnut Hill MA 02467 USA
[email protected] http://fmwww.bc.edu/ec-v/baum.fac.html
*
* 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/