# Re: st: Perplexed by the SUM function

 From Eva Poen To statalist@hsphsun2.harvard.edu Subject Re: st: Perplexed by the SUM function Date Mon, 16 Mar 2009 22:17:40 +0000

```Lena,

the function sum() creates a _running sum_ over all observations of
the expression within (). That is, sum will add up gh1 + gh2 + gh3 +
gh4 + gh5 for observation 1, and then add gh1 + gh2 + gh3 + gh4 + gh5
for observation 2, and so on. See -help functions- to learn more about
functions.

You are looking for -egen-:

egen gh_raw2 = rowtotal(gh1 gh2 gh3 gh4 gh5)

Here is an example to show you what sum() does in -generate-:

***
clear
set obs 10
gen x = _n
list
gen runsum = sum(x)
list
***

Hope this helps,
Eva

2009/3/16 Lena Cuisina <lenazoid@yahoo.com>:
>
> I have a very basic question.  I’m adding up responses to 5 questions.  Some of the questions have missing data.  This is what I don’t want to do, because I don't want to lose the 23 observations:
>
> . gen gh_raw = gh1 + gh2 + gh3 + gh4 + gh5
> (23 missing values generated)
>
> . gen gh_avg = gh_raw/5
> (23 missing values generated)
>
> I want to use whatever data I’ve got from the other questions in calculating the sum -- ie, I’d like to great the missing as zero, not missing.  So I tried the sum function:
>
> gen gh_raw1 = sum(gh1+gh2+gh3+gh4+gh5)
>
> gen gh_avg1 = gh_raw1/5
>
> All 5 questions have the same 5-category ordinal response scale that’s coded 0, 25, 50, 75, 100.  So the average of the 5 responses are within the 0 to 100 range.  Unfortunately, the command I've written with the sum function gives me a set of average scores with a maximum of 18,800.  It’s crazy.
>
> I have no idea what I’m doing wrong, because I’m pretty new to Stata.  Sorry for such a basic question.
>
> Lena
>

*
*   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/
```