In addition, there is no reason to use -while- unless
you are using Stata <= 6.
And really is a lot safer to use a tempname, for
reasons recently discussed on the list.
tempname z
scalar `z' = 0
forval i = 1/1000 {
summarize V`i', meanonly
scalar `z' = `z' + cond(r(mean) < . ,r(mean),0)
}
or
tempname z
scalar `z' = 0
foreach v of var V* {
summarize `v', meanonly
scalar `z' = `z' + cond(r(mean) < . ,r(mean),0)
}
Nick
n.j.cox@durham.ac.uk
Michael Blasnik
> 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
Radu Ban
> > 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
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?
> >> Thanks!
> >>
> >> Example:
> >>
> >> 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:
* http://www.stata.com/support/faqs/res/findit.html
* http://www.stata.com/support/statalist/faq
* http://www.ats.ucla.edu/stat/stata/