It seems that this doesn't answer the original question, and I am not
sure what does. But as -summarize- has been mentioned, correctly, as a
good way to get the sum of a variable, let me emphasise that if
efficiency is of concern, then -summarize, meanonly- is more efficient.
The StataCorp option name -meanonly- may well mislead here, as the
option in question does more than calculate means. If more detail is
needed, see
SJ-7-3 st0135 Stata tip 50: Efficient use of summarize
Q3/07 SJ 7(3):438--439
tip on using the meanonly option of summarize to more
quickly determine the mean and other statistics of a
large dataset.
Also in what follows, Sergiy has
local sigma=r(sum)
display `sigma'
There's no need for the indirection for immediate display, as
display r(sum)
will do it in one. For the later use which Sergiy has in mind,
storage naturally is important, but a scalar is more precise than a
local.
tempname sigma
scalar `sigma' = r(sum)
Nick
[email protected]
Sergiy Radyakin
Martin Weiss has shown an example where a new variable is created and
its values for all observations are set to the sum of the values of
another variable. Although indeed this provides the desired sum, it is
rather inefficient in terms of both memory use and computational
performance.
To find a sum of the values of a variable, use
summarize x
display r(sum)
If need to reuse this value later in your program, store it in a local:
local sigma=r(sum)
display `sigma'
A number of other related statistics (min, max, sd) can be obtained in
a similar fashion. Type
return list
after summarize (or any other command) to see the results available.
*
* 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/