droping variables that have "0" for values

droping variables that have "0" for values

by Laura Koth -
Number of replies: 1

Hi,

I have a very large dataset (26,000 variables in columns) and 300 subjects (in rows) and I need to create a new file that drops any variable in which all of the values are "0" for every subject.

is there a way to do this in stata?

drop if varname == 0

something like this?

In reply to Laura Koth

Re: droping variables that have "0" for values

by Kristen -

You can write a simple function to delete all variables that sum to 0. Here is an example I wrote using nhanes data set (.do file is attached):


webuse nhanes2, clear

gen test=0

ds

local varlist `r(varlist)'

display "`varlist'"


 foreach var of local varlist {

        sum `var',  meanonly

        if r(mean)==0 {

                drop `var'

         }

 }