Stata The Stata listserver
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

st: Re: operate on allbut


From   baum <baum@bc.edu>
To   statalist@hsphsun2.harvard.edu
Subject   st: Re: operate on allbut
Date   Mon, 23 Sep 2002 09:41:10 -0400

--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
baum@bc.edu                       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/



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