r_first(C;S)

Returns the first valid (non-N/A) value across a given set of columns.

Function type

Vector only

Syntax

r_first(C;S)

Input

Argument Type Description
C integer, decimal, or string 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.

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_first returns a decimal value corresponding to the first non-N/A value in the columns specified by C.

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

Note: If C is a string, r_first returns the first value, regardless of whether it is a N/A value. To omit N/A values, you must explicitly omit them using the S argument. See the example below.

Sample Usage

c1 c2 c3 c4 r_first(;) r_first(c1 c2;)
2 5 7 2 2 2
7 7 4 1 7 7
NA 0 9 3 0 0
NA NA NA NA NA NA
NA 'foo' 'bar' 'baz' NA NA
'a' 'b' 'c' 'a' 'a'

Example

In the following example, r_first(C;S) and r_last(C;S) return the first and last valid value in two rows containing strings. r_first(C;S) and r_last(C;S) use the S argument to omit N/A values.

<table>,foo,bar,baz;a,b,c,
</table>
<willbe name="not_na" value="string(rcv_)<>NA"/>
<willbe name="first" value="r_first(c1 c2 c3 c4;not_na)"/>
<willbe name="last" value="r_last(c1 c2 c3 c4;not_na)"/>