SPSS offers several ways to change a variable’s type. This tutorial will walk you through some examples.
SPSS Variable Type – Why Change it in the First Place?
- What you can do with a variable may depend on its type and – to a lesser extent – on its format.
- For example, you can extract substrings from string variables but not from numeric variables.
- For time calculations (“how many hours between …”) you’ll want to use proper time variables and/or date variables.
- In some cases, you may want to use such operations on a variable that are not possible with its current type or format. In such cases, changing these is often the way to go.
Changing Variable Type in SPSS
- First note that there’s really only two variable types in SPSS: string and numeric. For more on this, see SPSS Numeric Variables Basics.
- Starting from SPSS version 16, one can use ALTER TYPE in order to change a variable’s type. Keep in mind that it overwrites the original variable and results in system missing values if it can’t apply the requested conversion.
- However, loss of data or effort can be prevented here by working from syntax and making proper backups.
- Alternatively, use SPSS Clone Variables Tool before applying
ALTER TYPE.
SPSS Alter Type Syntax Examples
(The test data used by the syntax below are found here.)
alter type birthday(a10).
*2. Convert back to numeric variable with date format.
alter type birthday(edate10).
SPSS String and Numeric Functions
- An alternative and more classical way for converting numeric variables into strings is with the
STRINGfunction. - Note that new string variables (in contrast to numeric variables) first have to be declared (“created”) by the
STRINGcommand. - Converting string variables to numeric is done with the
NUMERICfunction.
SPSS String and Numeric Syntax Examples
(The test data used by the syntax below are found here.)
string str_q1(a1).
*2. Fill new string variable with values.
compute str_q1 = string(q1,f1)./* String function.
exe.
*3. Convert new string variable back into a (new) numeric variable.
compute num_q1 = numeric(str_q1,f1).
exe.
*4. Remove both new variables after exercise.
delete variables str_q1 num_q1.
SPSS Autorecode Command and Valuelabels Function
- Last but not least, string variables can be converted to numeric variables by the
AUTORECODEcommand. - Every distinct string value is rendered as the value label of a value in a new numeric variable.
AUTORECODEis usually applied to string variables with many duplicate values.- The reverse, passing value labels as values into a new string variable is done by the
VALUELABELSfunction. - Before using it, the new string variable must first be declared with the
STRINGcommand.
SPSS Autorecode and Valuelabels Syntax Examples
(The test data used by the syntax below are found here.)
autorecode name
/into num_name.
*2. Declare new string variable with string command.
string str_name(a10).
*3. Fill string variable with value labels of (numeric) variable.
compute str_name = valuelabels(num_name).
exe.
*4. Delete both variables at end of exercise.
delete variables num_name to str_name.
