Bookmark and Share

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


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

st: Re: Exclude non-positive values when using collapse


From   "Joseph Coveney" <[email protected]>
To   <[email protected]>
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/


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