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 Ruben Geert van den Berg on May 5th, 2016
Hi Diana!
You can display these "leading zeroes" with FORMATS as I'll show below. SPSS standard numeric format is the f format but if you change it to N10, each number is left padded with zeroes so that it'll always be 10 digits.
Keep in mind that this only changes how numeric values are shown (and copy/pasted). The underlying numbers don't change.
Hope that helps!
data list free/id.
begin data
1 2 3 4 5
end data.
*Take a look at data now.
formats id(n10).
*Take another look at data.
By Ruben Geert van den Berg on May 6th, 2016
Hi Diana!
In the first example I presumed your ID variable was a numeric variable. If it's a string variable, you can left padd it with LOOP. If it contains only numeric values, you can convert it to a numeric variable and then use the previous approach anyway.
I wrote a tiny syntax example that demonstrates both approaches: SPSS - Left Pad String Values with Leading Zeroes.
If that still doesn't get the job done, please let me know ok? There's plenty more options left if needed.
By Alana on September 7th, 2017
I have been having trouble trying to use concat to combine two categorical variables into one combined notes fields, and am wondering if you would be able to help. I have tried to concat the two variables both in the TRANSFORM - COMPUTE - STRING - CONCAT function, and through syntax. In both I get the message 'invalid combination of data types'; yet the two variables are strings in variable view.
This is the syntax I've tried to run -
string MASTER.
compute MASTER = concat(rtrim(Notes),' ',rtrim(FamilyNotes)).
exe.
Notes and FamilyNotes being two fields I want to combine. What am I doing wrong?
By Ruben Geert van den Berg on September 7th, 2017
Hi Alana!
Try
string master (a200).
or any other sufficient length. If you leave out(a200)
or something, the STRING command crashes and no new string variable is declared. Now the COMPUTE command assumes that MASTER is a numeric variable and you get this error.Hope that helps!