Discussion:
SPSS: Script to replace one value by a new value across all variables
(too old to reply)
Adam Smith
2020-01-21 13:29:51 UTC
Permalink
I am quite new to SPSS and I would like to replace the value 999 by 99999 across all my variables. The SPSS dialog "Find & Replace" works for one column/variable at a time only. Can someone help out with a script to make replacement across all variables possible?
Bruce Weaver
2020-01-21 15:20:19 UTC
Permalink
Post by Adam Smith
I am quite new to SPSS and I would like to replace the value 999 by 99999 across all my variables. The SPSS dialog "Find & Replace" works for one column/variable at a time only. Can someone help out with a script to make replacement across all variables possible?
RECODE ought to do the trick. How efficient you can make the code depends on whether there are any string variables, whether all numeric variables are contiguous in the file, etc. Here's a simple example to get you started.

DATA LIST LIST / x1 x2 x3 (3F8.0).
BEGIN DATA
1 2 4
999 999 999
END DATA.

* If all variables are numeric, use keyword ALL.
RECODE ALL (999 = 999999).
EXECUTE.

* If there is at least one string variable, but numeric variables are contiguous, use keyword TO.
RECODE x1 to x3 (999 = 999999).
EXECUTE.

* If numeric variables are not contiguous, list them, but bear in mind you can use TO
* in stretches where they are contiguous.

RECODE x1 TO x2 x3 (999 = 999999).
EXECUTE.

* If all numeric variables are surrounded by string variables, list each variable.
RECODE x1 x2 x3 (999 = 999999).
EXECUTE.

HTH.
Adam Smith
2020-01-21 16:44:35 UTC
Permalink
Post by Bruce Weaver
Post by Adam Smith
I am quite new to SPSS and I would like to replace the value 999 by 99999 across all my variables. The SPSS dialog "Find & Replace" works for one column/variable at a time only. Can someone help out with a script to make replacement across all variables possible?
RECODE ought to do the trick. How efficient you can make the code depends on whether there are any string variables, whether all numeric variables are contiguous in the file, etc. Here's a simple example to get you started.
DATA LIST LIST / x1 x2 x3 (3F8.0).
BEGIN DATA
1 2 4
999 999 999
END DATA.
* If all variables are numeric, use keyword ALL.
RECODE ALL (999 = 999999).
EXECUTE.
* If there is at least one string variable, but numeric variables are contiguous, use keyword TO.
RECODE x1 to x3 (999 = 999999).
EXECUTE.
* If numeric variables are not contiguous, list them, but bear in mind you can use TO
* in stretches where they are contiguous.
RECODE x1 TO x2 x3 (999 = 999999).
EXECUTE.
* If all numeric variables are surrounded by string variables, list each variable.
RECODE x1 x2 x3 (999 = 999999).
EXECUTE.
HTH.
Great. Thank you a lot!

Loading...