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

st: RE: Re: operate on allbut


From   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   st: RE: Re: operate on allbut
Date   Mon, 23 Sep 2002 16:10:43 +0100

Caleb Southworth answered by Kit Baum

> > 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.
>

Another tool here, with some other features, is -ds3- on SSC.

. ds3 tract, complement
. collapse (sum) `r(varlist)', by(tract)

returns all variables except -tract-.

Nick
[email protected]

*
*   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–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index