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.)
*1. Convert "birthday" to string.
alter type birthday(a10).
*2. Convert back to numeric variable with date format.
alter type birthday(edate10).
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
STRING
function. - Note that new string variables (in contrast to numeric variables) first have to be declared ("created") by the
STRING
command. - Converting string variables to numeric is done with the
NUMERIC
function.
SPSS String and Numeric Syntax Examples
(The test data used by the syntax below are found here.)
*1. String command for declaring new string variable.
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.
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
AUTORECODE
command. - Every distinct string value is rendered as the value label of a value in a new numeric variable.
AUTORECODE
is 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
VALUELABELS
function. - Before using it, the new string variable must first be declared with the
STRING
command.
SPSS Autorecode and Valuelabels Syntax Examples
(The test data used by the syntax below are found here.)
*1. Convert string to (new) numeric variable with autorecode command.
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.
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.
THIS TUTORIAL HAS 9 COMMENTS:
By Mudi on December 29th, 2014
Thanks a lot for your great support!! : )
By Flo on October 21st, 2015
Last line:
It is "delete variables num_name to str_name" instead of "str_q1"
By Ruben Geert van den Berg on October 21st, 2015
You're totally right, thanks a lot for pointing that out! This is one of the oldest tutorials and I'll rewrite it from scratch as soon as I'll have the time for doing so.
By ARNALDO on November 13th, 2015
thanks a lot i enjoyed, i'll keep reading the other tutorial in order to learn more about spss syntax.
By Diana Kwok on May 5th, 2016
Hi Ruben,
I'm new to SPSS. I'm using it to convert Excel files to an acceptable format for HLM7.
My ID column needs to be in 9-digits, e.g., 000000001 and 123456789. This is correctly specified in Excel. However, when I open the Excel file in SPSS, I get 1 and 123456789.
How can this be rectified?
Thanks and have a great day!
Diana.