if(C1;R1;C2;R2;...;D)
Returns the value corresponding to the first condition that evaluates to true; if no conditions are true, it returns the specified default value.
if(C1;R1;C2;R2;...;D)
that is part of the expression
language is different from the <if>
block code construct. For more information, see the
Additional information section below.If
you have a large number of arguments in
if()
, you may want to consider
using iff()
.
iff()
is potentially faster and
more memory efficient.
Syntax
if(C1;R1;C2;R2;...;D)
Input
Argument | Type | Description |
---|---|---|
C[N] |
integer | A scalar value or the name of a column The
value of |
R[N] |
any | The value to return if C[N] evaluates to
1The values specified for all instances of
|
D |
any | The default return value (if none of the instances of C
evaluate to 1)The value specified for |
C
and
R
pairs and one D
argument).Return Value
R
and D
) corresponding to the following:- If
C1
is 1, returnR1
; else ifC2
is 1, returnR2
; and so on. - If none of the conditions specified by
C[N]
evaluate to 1, returnD
.
If any of the values corresponding to C[N]
are N/A, an
error is returned.
Sample Usage
C1 |
R1 |
D |
if(C1;R1;D) |
---|---|---|---|
0 | 5 | 10 | 10 |
1 | 'true' | 'false' | 'true' |
0 | 100 | NA | NA |
C1 |
R1 |
C2 |
R2 |
D |
if(C1;R1;C2;R2;D) |
---|---|---|---|---|---|
0 | 'one' | 1 | 'two' | 'three' | 'two' |
Example
Let's use the table pub.demo.retail.item to
illustrate the use of if(C1;R1;C2;R2;...;D)
.
<willbe name="location" value="if(store=1;'New York';store=2;'Miami';store=3;'London';'Not a valid location')"/>
This will give us the following new column:
Since, in our example, there are no values in the store
column that are not 1, 2, or 3, the default value ('Not a
valid location
') is never used.
Let's see an example where we use the default value.
sales
column (which correspond to returns)
to N/A; otherwise we want to keep all the other values the same. We
can do this with the following
code:<willbe name="xsales" value="if(sales<0;NA;sales)"/>
This will result in the following:
You can see that the first row, whose sales
column had a
value of -5, now contains an N/A value in the
newly created xsales
column. All the other values
in that column have the same value as the sales
column.
Additional information
The 1010data function if(C1;R1;C2;R2;...;D)
that is part of the expression
language is different from the <if>
block code construct.
The if(C1;R1;C2;R2;...;D)
function in the expression language takes a
series of conditions and returns the value corresponding to the first condition that
evaluates to true (or the final value if none of the conditions are true).
The <if>
construct in block code, on the other hand, takes a single
condition and expands its contents only if the condition is true. If it contains
<then>
and <else>
clauses, it expands the contents
of the <then>
clause if the test condition is true, otherwise it expands
the contents of the <else>
clause. For more information, see <if>
.
Note that the block code <switch>
statement implements behavior
analogous to that of the if(C1;R1;C2;R2;...;D)
function. For more
information, see <switch>
.