Bookmark and Share

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


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

st: RE: Re: Generating the sum of a variable


From   Nick Cox <[email protected]>
To   "'[email protected]'" <[email protected]>
Subject   st: RE: Re: Generating the sum of a variable
Date   Wed, 23 Feb 2011 20:06:58 +0000

The sum is returned by -summarize- (just not displayed). You can access it as r(sum).  

. sysuse auto, clear
(1978 Automobile Data)

. su mpg, meanonly

. di r(sum)
1576

. su mpg

    Variable |       Obs        Mean    Std. Dev.       Min        Max
-------------+--------------------------------------------------------
         mpg |        74     21.2973    5.785503         12         41

. ret li

scalars:
                  r(N) =  74
              r(sum_w) =  74
               r(mean) =  21.2972972972973
                r(Var) =  33.47204738985561
                 r(sd) =  5.785503209735141
                r(min) =  12
                r(max) =  41
                r(sum) =  1576

There is a small efficiency gain in using -su, meanonly- if you only care about the sum. 
See also

J-7-3  st0135  . . . . . . . . . . . Stata tip 50: Efficient use of summarize
        . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  N. J. Cox
        Q3/07   SJ 7(3):438--439                                 (no commands)
        tip on using the meanonly option of summarize to more
        quickly determine the mean and other statistics of a
        large dataset

If you wanted the sum to be shown always, I think you'd need to write a small program to that effect. Someone may well have done this already. This is just to show the spirit, and can naturally be extended. 

program mystat
	version 8
	syntax varname(numeric) [if] [in] 
	
	marksample touse
	qui count if `touse' 
	if r(N) == 0 error 2000 
	
	su `varlist' if `touse', meanonly 
	local format : format `varlist' 
	
	di 
	di "Mean " `format' r(mean) 
	di "Min  " `format' r(min)
	di "Max  " `format' r(max) 
	di "Sum  " `format' r(sum) 
end

. mystat mpg

Mean  21.2973
Min        12
Max        41
Sum      1576

. mystat mpg if foreign

Mean  24.7727
Min        14
Max        41
Sum       545


Nick 
[email protected] 

W R Goe

How can I easily generate the sum of a variable? I know this can be done with  the sum function, but can you get the sum as part of the output of a Stata program that generates univariate statistics (e.g. Summarize).


*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/


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