r_sum(C;S)

Returns the sum of values across a given set of columns.

Function type

Vector only

Syntax

r_sum(C;S)

Input

Argument Type Description
C any numeric type A space- or comma-separated list of column names

C may also be a string expression (in single quotes) constituting a pattern for matching columns. For instance, 'foo*' specifies all columns that begin with "foo".

If C is omitted, the values in all of the columns are included.

N/A values in C are ignored.

S integer A boolean column or an expression in rcv_/rcn_/rct_ which determines whether or not that column is selected to be included in the calculation. See System variables for more information about rcv_/rcn_/rct_.

If S is omitted, all columns will be considered by the function (subject to any prior column selections).

Return Value

For each row of the table, r_sum returns a decimal value corresponding to the sum of the non-N/A values in the columns specified by C.

If all the values in the columns specified by C are N/A, the result is 0.

Sample Usage

c1 c2 c3 c4 r_sum(;) r_sum(c1 c2;)
2 5 7 2 16 7
7 7 4 1 19 14
NA 0 9 3 12 0
NA NA NA NA 0 0

Example

In the following example, the column this_and_that displays the results of adding the values in the columns thisone and thatone. The column all_that displays the results of adding the values in all the columns that begin with "that", which in this example are thatone and thatotherone.

<table cols="thisone,thatone,thatotherone">
  1,2,3;
  4,5,6;
  7,8,9;
</table>  
<willbe name="this_and_that" value="r_sum(thisone thatone;)"/> 
<willbe name="all_that" value="r_sum('that*';)"/>