See Crunch Automation basics for more information.
The CREATE TEXT CASE command allows you to create your own text variable, within which you obtain values from multiple sources under different conditions.
The logic behind creating the variable is the same as it is using the CREATE CATEGORICAL CASE command, except that the output is a text variable. Each output value is defined on a case-by-case basis by a logical condition (see the expressions section for more information on conditions/expressions). When a case (i.e., row of the data and typically a respondent) evaluates to TRUE in the expression (boolean logic), then it is allocated that value (which can have decimals, i.e., a float).
For each condition, in turn, only the cases that haven’t yet been allocated are eligible. It’s equivalent to the "IF, ELSE, ELSE IF" commands you see in other programming languages (such as R and JavaScript). Any unallocated cases that have not met any conditions are then set as missing. You can also set cases to be missing at any time by using NULL.
- A common user error is to forget the END argument, which is needed to tell the script that the series of conditional statements is over.
- Use commas (,) in the script to separate each statement. Commas are needed in all but the last statement (see examples below).
Optional argument
THEN ... VARIABLE — You can also optionally set the output value as a constant (string) or as a fill from another variable. If you are filling from another variable, then this is when you use the THEN ... VARIABLE argument.
CREATE TEXT CASE
WHEN condition THEN (string|VARIABLE alias),
...
WHEN condition THEN (string|VARIABLE alias)
[ELSE (INTO NULL|string|VARIABLE alias)]
END
AS alias
[TITLE "string"]
[DESCRIPTION "string"]
[NOTES "string"];
Use case
Case statements can be used to coalesce multiple columns, each of which may contain partial or missing data, into a single variable with one observation per row:
REPLACE TEXT CASE name
WHEN IS_MISSING(preferred_name) THEN VARIABLE name
ELSE VARIABLE preferred_name;