Automating Survey Cleaning with SPSS Syntax: Conditional Compute and If Statements
Learn how to use SPSS Syntax with Compute, If, and loops to automate survey cleaning steps such as outlier flagging and missing‑value replacement.
19 Jun 2026, 07:16 UTC

Problem: repetitive cleaning steps in a survey dataset
\nWhen preparing a survey file you often need to flag extreme values, replace missing codes, and create derived scores for dozens of variables. Doing this through the point‑and‑click menu is tedious and error‑prone.
\nThesis: SPSS Syntax lets you script those steps once, making the workflow reproducible and faster
\nPrepare the data
\nOpen your .sav file, check the variable names you will work with (e.g., income, age, sat_score). Ensure they are numeric.
Write conditional logic with Compute and If
\nThe COMPUTE command creates a new variable; the expression can be a logical test that returns 1 (true) or 0 (false). The IF command changes existing variables conditionally.
* Flag outliers for income (values > 100000).\nCOMPUTE income_outlier = (income > 100000).\n* Replace system‑missing with a placeholder value.\nIF (MISSING(income)) income = 999.\n* Recode low sat_score values to missing.\nIF (sat_score < 200) sat_score = $SYSMIS.\nEXECUTE.\nProcess multiple variables with a loop
\nIf you need the same outlier flag for several variables, a DO IF block inside a LOOP avoids repeating code.
* Define a list of variables to check.\nSTRING varList (A50).\nCOMPUTE varList = 'income age sat_score'.\nLOOP #i = 1 TO 3.\n COMPUTE #var = SUBSTR(varList, (#i-1)*16+1, 15).\n STRIP #var.\n DO IF NOT(MISSING(#var)).\n COMPUTE !CONCAT('outlier_', #var) = (#var > 100000).\n END IF.\nEND LOOP.\nEXECUTE.\nNote: SPSS treats the macro‑like syntax above as illustrative; in practice you would use a VECTOR or DO REPEAT construct. A simpler, version‑safe approach is:
VECTOR v = income age sat_score.\nVECTOR out = out_income out_age out_sat.\nLOOP # = 1 TO 3.\n COMPUTE out(#) = (v(#) > 100000).\n IF (MISSING(v(#))) v(#) = 999.\nEND LOOP.\nEXECUTE.\nTrade‑off: initial learning curve
\nThe syntax language requires familiarity with commands and quoting rules. Mistyped variable names produce silent errors, so always inspect the output with DESCRIPTIVES or FREQUENCIES after running.
Actionable next steps
\n- \n
- Create a new Syntax file (
File → New → Syntax). \n - Paste the example block, adjusting variable names and thresholds to match your study. \n
- Run the file (
Run → All) and switch to Data View to verify the new columns. \n - Save the syntax file (.sps) alongside your .sav for version control. \n
- Re‑open the session later and re‑run the syntax to confirm reproducibility. \n
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.