StatLab


<  August 2014 >
Su Mo Tu We Th Fr Sa
          1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31

Reserve the classrooms


Schedule for 08/23/2014


Stat Consultants
CSSSI (219 Prospect St)
To see the Consultant's schedule, please see our calendar on the CSSSI website.
To see the room schedule for the CSSSI Classroom, please see our calendar on the schedule.yale.edu website.
To see the room schedule for the CSSSI Rosenkranz, please see our calendar on the schedule.yale.edu website.

StatLab Frequently Asked Questions (FAQ)

We keep track of the questions we hear frequently and post them here on our web site. If you don't find the question and answer you are look for, try our help pages, and if you still don't find it, contact us!


In Stata, how do you sum variables across columns into a new variable?

In Stata, the egen command combined with rsum allows the user to sum
data across columns into a new variable.  For example, with two
variables, var1 and var2, we can create a third, var3, which is the sum
of the two, with the command:


egen var3 = rsum(var1 var2)


We can then create the variable var4 as the sum of var1, var2, and var3,
using the command,

egen var4 = rsum(var1 var2 var3)

or equivalently,

egen var4 = rsum(var1-var3)

An alternative way to sum across columns is just to add, as in,

gen var4 = var1 + var2 + var3

There is one difference between the two methods - "egen rsum" sums across
columns even if there are missing values.  "gen sum" sums across columns 
only if all of the values are non-missing. 

Note: there are many other row operator commands in Stata that work
similarly, such as, rmax, rmean, rmiss, etc.