Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.
From | "Joseph Coveney" <stajc2@gmail.com> |
To | <statalist@hsphsun2.harvard.edu> |
Subject | st: Re: Exclude non-positive values when using collapse |
Date | Wed, 22 Jan 2014 12:33:28 +0900 |
Chris Yang wrote: I want to collapse a dataset using the means of the variables. However, in my data, some variables may contain value -1, and I want to exclude only such values from the mean calculation for the variable when collapsing. For example, given the following dataset: var1 var2 var3 group 0 1 1 1 0 -1 1 1 1 0 -1 1 1 1 1 1 1 1 1 2 1 0 0 2 After the collapse (by group), for group 1: I want var2 to be (1 + 0 + 1) / 3 and var3 to be (1 + 1 + 1) / 3, i.e. ignoring -1 values when averaging. How should I do that? Thanks a lot! -------------------------------------------------------------------------------- Couldn't you just set it to missing beforehand? See -mvdecode- for the quickest way, or if the negative value varies from -1, then just use -replace `var' = . if `var' < 0- in a -foreach var of varlist var?- loop. Joseph Coveney * * For searches and help try: * http://www.stata.com/help.cgi?search * http://www.stata.com/support/faqs/resources/statalist-faq/ * http://www.ats.ucla.edu/stat/stata/