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:44:10 +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!

--------------------------------------------------------------------------------

Wait a minute.  What do you want to do here?  Do you want to exclude -1 values
and calculate the mean of the remaining values, or exclude both zero and -1
values and calculate the mean of the remaining?  Your first paragraph says the
first, but your penultimate paragraph says the second (well, sort of--I assume
there's a typo or two for your var3 expression).  Either way, what I wrote in
response will work; you just have to revise the missing-value list for
-mvdecode- and the -if- criterion for the manual 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