Module 8: Making new variables
Question: the analysis you want needs a variable you don't have: a pulse-pressure number, or BMI as categories. Derive it; never overtype raw data.
Open the sample (Ctrl+Shift+O). Both tools are in the Transform menu.
Compute Variable: a formula makes a new column
Transform → Compute Variable… Target PulsePressure, expression
SystolicBP - DiastolicBP, OK.
A new numeric column appears, computed for every case (cases missing either input come out blank; missingness propagates, correctly). Give it a label in Variable View, then run Descriptives on it.
Recode: grouping a scale variable into categories
BMI has standard clinical bands; a categorical version unlocks crosstabs, boxplot grouping and ANOVA factors.
Transform → Recode into Different Variables… Source BMI, new variable
BMIGroup, and the ranges:
| Range | New value |
|---|---|
| lowest – 18.4 | 1 |
| 18.5 – 24.9 | 2 |
| 25.0 – 29.9 | 3 |
| 30.0 – highest | 4 |
Then, in Variable View, label the codes (1=Underweight, 2=Normal, 3=Overweight, 4=Obese) and set the measure to Ordinal. A recode is only finished once it is labelled.
Always "into Different": recoding into same overwrites the original values, and the analysis you have not thought of yet will want them back.
Grouping vs histogram bins
Module 4's histogram bins slider also "grouped" BMI, but only in the picture. A recode makes a real variable every procedure can use, and is recorded in the document. Rule of thumb: exploring shape → bins; analyzing groups → recode.
Exercise
- Crosstab
BMIGroup× Hypertensive with chi-square: the module 7 BMI→SBP effect, now visible as a category pattern. - Compute
MAP(mean arterial pressure)= DiastolicBP + PulsePressure / 3. Check one case by hand against the Data View. - Boxplot SystolicBP by
BMIGroup. Do the medians climb across the four bands?
The syntax trail
compute target=PulsePressure, expression="SystolicBP - DiastolicBP"
recode variable=BMI, rules=["10 thru 18.4=1", "18.5 thru 24.9=2", "25 thru 29.9=3", "30 thru 60=4"], into="BMIGroup"
compute target=MAP, expression="DiastolicBP + PulsePressure / 3"
crosstabs row=BMIGroup, column=Hypertensive, chi_square=true
The Recode is in the trail too: a recode command carrying its ordered
rules, one rule string each, first match wins, so the whole preparation
pipeline on this page replays from the raw file. The labelled .ckd you
Save is still part of the reproducible record: it carries the metadata
(labels, missing values) that no transform command sets.