Recode with a Switch Statement

Top  Previous  Next

How To... > Work with Formulas > Work with the Formula Editor > Recode with a Switch Statement

Switch statements are incredibly helpful for recoding data. Unlike if-statements, switch statements can deal with more than two possibilities.

There are three parts to a switch statement: the value to switch on, the test values, and the result values.

_img437

 

Enter a switch statement

Type switch( Fathom will fill in the closing parenthesis and a brace.

To add a test value, position the caret in an existing test or result value. Press the Insert key (or Ctrl+Enter (Win) Option+Return (Mac)).

To delete a test, position the caret in the test value and press the up arrow until the entire test value is selected. Then press Delete twice. If the test is empty, the up arrow will not work. Just position your caret in the empty test and press Backspace or Delete.

 

_img438

Usually you will write switch statements similar to the preceding one. Your value to switch on will be the name of an attribute, the test values will be values that the attribute takes on, and the result values will amount to a recoding of the original attribute. Fathom returns the result value whose test value matches the value being switched on.

A switch statement’s tests are not limited to equality. In this statement, for example, Fathom first compares the value of N with 0 and returns “zero” if N does equal 0. If not, it tests to see whether N is less than 5 and returns “low” if it is. If not, it goes on to compare N with 10 and returns “medium” if N is less than 10. If no test has succeeded thus far, it returns “high.”

Here are some things to keep in mind when writing switch statements.

_img439

 

If you do not explicitly specify a comparison operator, Fathom will test for equality.

Fathom returns the first result value whose test returns true. The fact that later tests might also return true has no effect on the result.

A switch value can itself be an expression, not just the name of an attribute, for example, _img140. The result of evaluating the expression is used in the test.

Result values may be any expression (including if- or switch statements).

The switch value is substituted for any question marks or missing arguments to functions that appear in the test expression.