See Crunch Automation basics for more information.
The CREATE NUMERIC CASE command allows you to create your own numeric variable. There are many use cases for this: for example, combining weight variables into a single variable or fixing survey skips (DP jargon sometimes refers to this as "rebasing").
The logic behind creating the variable is the same as it is using the CREATE CATEGORICAL CASE command, except that the output is a numeric 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 (float) 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 NUMERIC CASE
WHEN condition THEN (float|VARIABLE alias),
...
WHEN condition THEN (float|VARIABLE alias)
[ELSE (INTO NULL|float|VARIABLE alias)]
END
AS alias
[TITLE "string"]
[DESCRIPTION "string"]
[NOTES "string"];
Use cases
The following use cases are described, all of which you can find in this datafile:
- Capping a numeric variable
- Recoding a numeric variable (fixing a survey skip)
- Combining different numeric variables (for weighting purpose)
Capping a numeric variable
The following describes the numeric variable that represents the number of take-out meals eaten in the last week, which we want to cap at a maximum value of 10. We will then derive the variable on the right (max value 10) from the variable on the left (max value 13), as seen in the following:
CREATE NUMERIC CASE
WHEN q2 > 10 THEN 10.0
ELSE VARIABLE q2
END
AS q2_capped
TITLE "Number of take-out eaten"
NOTES "Capped at value of 10";
Recoding a numeric variable based on another question
The variable (alias = q2) captures the number of take-outs eaten in the last week. This was only supposed to be asked of the people who answered “Yes” in q1, which asks if they ever eat take-out or not. We can recode the values of q2 to missing based on a condition involving q1:
CREATE NUMERIC CASE
WHEN q1 = "Yes" THEN VARIABLE q2
ELSE INTO NULL
END
AS q2_rc
TITLE "Number of take-out eaten amongst eaters"
NOTES "Base: All those who eat take-out";
Combining different numeric variables
In the same datafile, which collected data over different waves, there are three different weighting variables for each respective wave. If we want to combine them into one variable, we can do so using Crunch Automation. However, in doing so, we should note that combination does not in any way adjust for relative proportions between the waves—it simply provides a single weighting variable:
CREATE NUMERIC CASE
WHEN wave = 1 THEN VARIABLE weight_wave1,
WHEN wave = 2 THEN VARIABLE weight_wave2,
WHEN wave = 3 THEN VARIABLE weight_wave3
END
AS weight_combined
TITLE "Combined Weight"
DESCRIPTION "All waves combined weight";
SET WEIGHT weight_combined;
The weighting variable in this example, weighted gender (alias d3) to 50/50, which can be verified when the master weighting is applied to the cross-tabulation as seen in the following: