Statalist The Stata Listserver

[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

Re: st: scalar question

From   "Michael Blasnik" <>
To   <>
Subject   Re: st: scalar question
Date   Fri, 21 Jul 2006 16:04:37 -0400

I don't think this suggested approach will handle missing values properly since it will treat them as zeros added to the row sums and give no indication of a reduced number of observations.

Getting back to the original approach. I'm not sure if you wouldn't be better off with a reshape long to get what you want, but if you keep with the current approach, I would change the middle of the loop to :

summarize V`i', meanonly
scalar average_V`i'= r(mean)
scalar z= z + cond( average_V`i'<. , average_V`i',0)

The cond() function will avoid adding a missing value into your sum (but it will replace it with 0). You may want to count how many missing values you end up with (in another scalar counter). I also added the meanonly topion to the summarize command to make it faster

Michael Blasnik

----- Original Message ----- "Radu Ban" <> wrote

you can try the -egen rsum- command and then take the mean, as the sum
of means is equal to mean of sums. for example:

egen rowsum = rsum(V*)
summarize rowsum
scalar z = r(mean)
drop rowsum

2006/7/21, Jeffrey W Ladewig <>:
I am using a simple while loop statement (see below) to add the mean of each
variable in a series. The program runs fine except if one of the variables
contains all missing values (there are reasons why I need to keep the
variables). If, for instance, the 500th variable contains all missing
values, then the scalar (i.e., average_V`i') for the 500th variable equals a
missing value (of course), but the additive scalar (i.e., z) from that point
forward only reports missing values. That is, my additive scalar stops
adding. I have been programming a bypass around each of these problematic
variables, but is there a command or something that I could use instead?


scalar z = 0
local i = 1
while `i' <= 1000 {

summarize V`i'
scalar average_V`i'= r(mean)
scalar z= z + average_V`i'

local i = `i' + 1
*   For searches and help try:

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