help for rowsum

Sum by row

Syntax

rowsum namelist, sumlist(varlist) [new(str) keep example(varlist)]

Description

rowsum sums the values of multiple observations. It then generates a new observation and assigns the summed value to it.

namelist needs at least 3 inputs: one identifier (a variable) and at least two observation names. These names cannot contain space (e.g. <North Carolina> will be regarded as <North> and <Carolina>).

The new observation with summed values will be flagged with _rowsum.

Options

sumlist(varlist) specifies the variable list to sum up. Do not include string or categorical variables.

The program will stop if string variables are included. The program will keep running if categorical variables are included but will give a warning.

If varlist is very long, <ds, not> could be used to generate a list of all variables except the ones specified.

Values of unspecified variables (including string variables and categorical variables) of the new observation is the same as the first input observation.

new(str) gives the new observation a new name. If no new name is given, the name of the first input observation will be used.

keep keeps the original observations. It may result in duplications if no new name is given. By default, involved observations are dropped after summing up.

example(varlist) gives examples of the summing result. By default, it uses the first variable in the sumlist(). The user can specify variables to generate examples.

Examples

Sum the population of North and South Carolina. Generate a new observation called "Carolina_Total" and drop the original observations. Because names in <state> contains space, we need to use <state2> as the identifier.

. sysuse census, clear

. rowsum state2 NC SC, sumlist(pop) new(Carolina_Total)

We can check the result manually. A new observation called "Carolina_Total" is generated.

The values of string variable <state> and categorical variable <region> are kept the same as the first input observation <NC>:

. list state2 state region pop _rowsum

Sum all variables of North and South Carolina. Generate a new observation called "Carolina_Total" and the keep the original observations. Use <pop> and <marriage> as examples.

. sysuse census, clear

. ds state state2 region, not

. rowsum state2 NC SC, sumlist(`r(varlist)') new(Carolina_Total) keep example(pop marriage)

Check the result manually:

. list state2 state region pop _rowsum

Author

Pengzhan, Qian.

E-mail: p.qian@qmul.ac.uk.