Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

st: Re: Conditional Variable dropping in Stata

From   Maarten Buis <[email protected]>
To   [email protected]
Subject   st: Re: Conditional Variable dropping in Stata
Date   Fri, 2 Sep 2011 10:05:17 +0200

---Ana Gomez Lemmen Meyer wrote me privately:
> I am an economics PhD student. I am trying to do a conditional drop
> of variables in Stata, conditional on the mean of the variable being
> smaller than a certain value. I saw your post about the program you
> implemented for conditional dropping:
> However, I do not know how to use the function you created. Do you
> have a help menu for this function?

I do not have a help file for that function and I do not intend to
develop that program further. The reason is that I think such problems
are easier solved from first principle.

So in your case you would loop over a list of variables, compute the
mean and drop that variable if that mean is less than some number:

*-------------- begin example ----------------------
sysuse auto, clear

// get a list of variables,
// in this case all numeric variables
// also see -help unab-
// or just type them in a local macro
ds, has(type numeric)
local varl `"`r(varlist)'"'

foreach var of local varl {
	sum `var', meanonly
	if r(mean) < 10 {
		drop `var'
		// just to keep a record of which
		// variable got dropped
		local dropped `"`dropped' `var'"'

di as text "The following variables have been dropped:"
di as result `"`dropped'"'
*-------------------- end example ------------------------
(For more on examples I sent to the Statalist see: )

Hope this helps,

Maarten L. Buis
Institut fuer Soziologie
Universitaet Tuebingen
Wilhelmstrasse 36
72074 Tuebingen
*   For searches and help try:

© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index