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]

Re: st: row means for at least 10 observations greater than 0


From   Nick Cox <njcoxstata@gmail.com>
To   "statalist@hsphsun2.harvard.edu" <statalist@hsphsun2.harvard.edu>
Subject   Re: st: row means for at least 10 observations greater than 0
Date   Tue, 25 Feb 2014 15:37:36 +0000

Let's count positives first. One simple way is a loop. I imagine
variable names -var1990-var2013-. I throw in free a check for
non0missing.

gen positive = 0

qui foreach v of var var1990-var2013 {
      replace positive = positive + (`v' > 0 & `v' < .)
}

egen rowmean = rowmean(var1990-var2013) if positive >= 10.

For technique here see

SJ-9-1  pr0046  . . . . . . . . . . . . . . . . . . .  Speaking Stata: Rowwise
        (help rowsort, rowranks if installed) . . . . . . . . . . .  N. J. Cox
        Q1/09   SJ 9(1):137--157
        shows how to exploit functions, egen functions, and Mata
        for working rowwise; rowsort and rowranks are introduced

http://www.stata-journal.com/sjpdf.html?articlenum=pr0046

(If negative values are acceptable, modify the loop accordingly.)

Nick
njcoxstata@gmail.com

On 25 February 2014 15:30, Nicholas Winters
<nicholas.winters@mail.mcgill.ca> wrote:
> I need to take an average of several variables in a row, but I want to average of the row only if there are at least 10 variables without 0 as their observation. The data looks like this:
>
> PersonID  1990 1991 1992 1993 1994 1995 etc...
> 1               0       0       3      3      5       6
> 2               1       1       5      3      0       7
> 3               1       1       2      1      4       3
> 4               0       0       0      0      5       5
>
> does anyone have an idea how to do this? I've tried egen and rowmean() but I am not sure how to code for if a row has more at least 10 observations > 0
*
*   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