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

st: Re: operate on allbut

From   baum <>
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()

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'"'
program define _allvars,rclass
syntax varlist
return local allvars `varlist'

. allbut med

. collapse (sum) `r(allbut)',by(med)

Could readily be extended to allow for multiple items in the bylist.

Christopher F Baum, Boston College Economics, Chestnut Hill MA 02467 USA             
*   For searches and help try:

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