*load nhanes webuse nhanes2, clear *generate a variable where all values = 0 (to make sure your function works) gen test=0 *create a list of all variable names ds //list all variable local varlist `r(varlist)' display "`varlist'" *function to drop all variables that sum to 0 foreach var of local varlist { sum `var', meanonly if r(mean)==0 { drop `var' } }