Create a New Variable with Conditional Logic in SPSS: A Practical Syntax Guide
Learn how to add a new variable in SPSS using COMPUTE and IF, handle missing values, verify the result, and roll back if needed.
27 Feb 2026, 17:42 UTC

Desired Outcome
Generate a new numeric variable that reflects a specific condition (e.g., a binary flag for adults with income). The variable must handle missing values correctly, avoid accidental overwrites, and be easily verifiable.
Prerequisites
- SPSS 28 or later (syntax is compatible with earlier releases).
- A dataset loaded in the active session.
- Administrative or user permission to modify the active file.
- Knowledge of the variable names you will reference (e.g.,
age,income).
Focused Procedure
- Check for Existing Variable
Before creating a new variable, confirm that the name you plan to use does not already exist. In the Syntax Editor, run:
DISPLAY DICTIONARY.Scroll the output for the proposed name. If it exists, choose a unique name or use
DELETE VARIABLESto remove it. - Define Missing Values
Missing system values (blank) and user‑defined missing codes should be treated consistently. For example, if
ageuses 999 as a missing code:MISSING VALUES age (999). MISSING VALUES income (999). - Compute the New Variable
Use
COMPUTEwith anIFclause. The syntax below createsadult_income_flag, assigning 1 when the person is 18 or older and has a positive income, otherwise 0. Missing values propagate correctly because theIFstatement evaluates to false for any case where either operand is missing.COMPUTE adult_income_flag = 0. IF (age >= 18 AND income > 0) adult_income_flag = 1. EXECUTE.Note:
EXECUTEforces immediate creation; omit it if you want the variable to be temporary (seeTEMPORARYbelow). - Optional: Create a Temporary Variable
If the new variable is needed only for a specific procedure (e.g., a single FREQUENCIES call), wrap the compute in
TEMPORARY:TEMPORARY. COMPUTE adult_income_flag = 0. IF (age >= 18 AND income > 0) adult_income_flag = 1. EXECUTE. FREQUENCIES VARIABLES=adult_income_flag. - Verify the Result
Run a descriptive check to confirm the variable’s range and missing count:
DESCRIPTIVES VARIABLES=adult_income_flag /STATISTICS=MIN MAX. FREQUENCIES VARIABLES=adult_income_flag /FORMAT=DFREQ.In the output, ensure that
adult_income_flagshows values 0 and 1, that the minimum is 0, the maximum is 1, and that the missing count matches the number of cases whereageorincomewere missing. - Check Variable View
Switch to the Variable View tab in the Data Editor. Verify that the new variable appears, is numeric, and has the correct label (you can set a label with
VARIABLE LABELSif desired).
Expected Checks and Risks
- Syntax Errors: Missing semicolons or parentheses stop execution. Use the Syntax Editor’s Syntax Check button before running.
- Overwriting: If a variable with the same name exists, SPSS will silently overwrite it. Always verify existence beforehand.
- Missing Value Propagation: The
IFstatement treats any missing operand as false, resulting in a 0. If you need a system‑missing outcome instead, useIF (age >= 18 AND income > 0) adult_income_flag = 1. ELSE IF (MISSING(age) OR MISSING(income)) adult_income_flag = $SYSMIS. - Performance: Re‑running a compute on a very large dataset repeatedly can slow the session. Use
TEMPORARYwhen the variable is only needed for a single procedure.
Recovery Options
If the new variable was created incorrectly or you wish to undo the change, you have two common options:
- Delete the Variable
Run:
DELETE VARIABLES adult_income_flag. EXECUTE. - Reload the Original Dataset
Close the current file and open the original .sav file. This discards all unsaved changes, including the new variable.
Practical Checklist
- Confirm variable names and missing codes.
- Run syntax in the Syntax Editor; check for errors.
- Verify variable type and label in Variable View.
- Check descriptive statistics for expected ranges.
- Save the file after confirming correctness.
Conclusion
Using COMPUTE and IF in SPSS syntax is a lightweight, reproducible way to add derived variables. By explicitly handling missing values, guarding against accidental overwrites, and validating the result with descriptive statistics, you can confidently extend your dataset without compromising data integrity.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.