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

Re: st: row mean (mean across columns)

From   "Martin Weiss" <>
To   <>
Subject   Re: st: row mean (mean across columns)
Date   Wed, 8 Oct 2008 21:14:29 +0200

In addition, let -ds- watch out for -numeric-s only, otherwise it chokes on -make-

sysuse auto, clear
qui ds,has(type numeric)
local varlist `r(varlist)'
egen miss = rowmiss(_all)
egen mean = rowmean(`varlist') if miss==0

----- Original Message ----- From: "Eva Poen" <>
To: <>
Sent: Wednesday, October 08, 2008 9:01 PM
Subject: Re: st: row mean (mean across columns)


2008/10/8 Jacob Wegelin <>:
Given any dataset of all numeric variables, I want to generate a new
variable called myMean, which is the arithmetic mean (the average) across
all the variables. The program below solves this problem. But surely there
is a one-line command that will perform this task in Stata?

The post
appears to contain a bug, in the sense that the row total computed is not
corrected as in my code below.

This should be done in a general manner:

(1) As in the current dataset, the variables will not necessarily be in a
form like a1 to a100.

(2) The number of variables is arbitrary, so I cannot hard-code the
denominator as when myMeanByHand is computed below.

(3) If any value in a row is missing (.), the mean computed must also be
missing, since then the mean across all variables is not defined. (Thus egen
rowtotal is not the answer.)

I might be misunderstanding you, but wouldn't

qui ds
local varlist `r(varlist)'
egen miss = rowmiss(_all)
egen mean = rowmean(`varlist') if miss==0

be a solution? It will calculate the average you want, in a flexible
manner, but only if there are no missings anywhere in the observation.

/* A related question: The following gives an incorrect answer. What in the
world is it doing? */
egen junk=rowmean(*) list

Interesting one. I did a test, with the auto data:

sysuse auto
drop make
egen junk = rowmean(_all)
sum junk
drop junk
egen junk2 = rowmean(*)
sum junk2

and also received different results. Setting -trace- on revealed that
in the second case, Stata seems to be including a temporary variable
(sort order?) into the calculation. It does look like a bug to me.

*   For searches and help try:

*   For searches and help try:

© Copyright 1996–2023 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index