Bookmark and Share

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

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

Re: st: st: Calculating row average with egen rmean

From   Nick Cox <>
Subject   Re: st: st: Calculating row average with egen rmean
Date   Sun, 3 Apr 2011 00:07:56 +0100

Your posting seems very confused. Your text and your supposed example
are contradictory.

Note first that as from Stata 9 the -egen- function -rmean()- is
undocumented but works as a just a caller for -rowmean()-. If you are
using a version of Stata other than the current version, you are asked
to make that clear in your postings.

I don't know why you think that either function would ignore zeros.
That isn't documented and more importantly that would not be
statistical and it is not what either function does. The code is
accessible to you by

. viewsource _growmean.ado

In terms of your desired goal of using a divisor of 1 for one
non-missing value that is precisely what this function does.

If you regard 0s as equivalent to missings, then fix them accordingly
e.g. using -mvdecode-. Alternatively you could write your own code
that only averages positive values.  Here is an example:

gen myposcount = 0

qui forval j = 1/48 {
      replace myposcount = myposcount + (month`j' > 0 & month`j' < .)

egen rowmean = rowtotal(month*)
replace rowmean = cond(myposcount == 0, ., rowmean/myposcount)


On Sat, Apr 2, 2011 at 10:57 PM, Clifton Chow
<> wrote:

> I am trying to calculate an average hours worked across 48 months for each observation using egen rmean(hours1....hours48).  I discovered that while this works as long as a person has a positive hours across 2 or more months, for someone with positive hours on ONLY 1 month, the result for the average hours variable for that observation is a 0.
> I thought egen rmean counts only columns with data and ignores columns with missing or 0 values so that if obs 2 exhibits the data pattern below,
> obs     month1 month2  month3   month4........month48  average48
> 1           20          .           14.75     .......(missing or 0)..       17.37
> 2             .        .              32         ........(missing or 0)..       32
> the average48 for that person reflects the single month for which there is positive hours worked.
> Is there away to correct this situation so that egen rmean would divide an observation with only one month of data by 1 ?

*   For searches and help try:

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