SPSS Tutorials



SPSS ALTER TYPE command is mainly used for converting string variables to numeric variables. However, it has other interesting applications as well. This tutorial quickly walks you through those, pointing out some pitfalls, tips and tricks along the way.
You can follow along by downloading and opening holidays.sav but you do need SPSS version 16 or higher for using ALTER TYPE.

SPSS ALTER TYPE Command Data Screenshot


Although ALTER TYPE is a great option for getting many things done fast, it has one major shortcoming: if it fails to convert one or more values, it returns system missing values without throwing any warnings or errors.
If this goes undetected, it may severely damage your data and bias research outcomes. And even if you do notice system missing values resulting from ALTER TYPE, it will be hard to track down what (if anything) went wrong because the original values are overwritten.

Playing it Safe

We propose two basic strategies for playing it safe:

SPSS ALTER TYPE - String to Numeric

We'd like to know the average age of our participants but we can't calculate it because age is a string variable in our data. The syntax below first copies and converts it to a numeric variable. We can see that no missing values occur in the result by running DESCRIPTIVES; n is equal to the numer of cases in our data.

SPSS ALTER TYPE String to Numeric

SPSS ALTER TYPE Syntax Example 1

*1. String command for creating new string variable.

string copy_age(a2).

*2. Copy age into new string variable.

compute copy_age = age.

*3. Convert string to numeric.

alter type age(f2).

*4. Check for missing values and average age.

descriptives age.
SPSS ALTER TYPE no missing values

SPSS ALTER TYPE - String to Date

We now turn to birthday. This is a string variable and we wish to convert it to a date variable. Now, SPSS date variables are numeric variables holding numbers of seconds that are displayed as normal dates. They have several display options, a quick overview of which is found under date format. Note that the string values in birthday correspond to what date values look like if their format is set to DATE11. We therefore must specify DATE11 in ALTER TYPE (step 3 below) for converting birthday to a date variable.

SPSS ALTER TYPE Syntax Example 2

*1. Create new string variable for copying birthday.

string copy_birthday(a11).

*2. Copy birthday values into new string variable.

compute copy_birthday = birthday.

*3. Convert birthday to date variable.

alter type birthday(date11).

*4. Check for missing values.

descriptives birthday.

SPSS ALTER TYPE - String to Date

Note that start and end are also string variables. Their values look like date values displayed as EDATE10. We'll copy and convert both of them to date variables by the syntax below.

*1. Create two new string variables.

string copy_start copy_end(a10).

*2. Copy values of start and end into new string variables.

compute copy_start = start.
compute copy_end = end.

*3. Convert both string variables to date variables.

alter type start end(edate10).

System Missing Values from ALTER TYPE

Note that ALTER TYPE resulted in a system missing value without any warning or error. Fortunately, we copied our variables before converting them and copy_start tells us that the day and month were reversed for one case. Because cases have unique id values, we can easily correct the problem by combining DATE.DMY and IF as shown in the next syntax example.

SPSS ALTER TYPE command system missing value
*Set correct start date for case with id = 482.

if id = 482 start = date.dmy(15,12,2014).


An interesting but little known feature of ALTER TYPE is converting all variables having a given format. We can do so by specifying an input format, which then acts as a filter: ALTER TYPE affects only variables whose formats match this input format. The example below first addresses all variables but only converts those having an EDATE format.

*Convert all variables in the data having an edate format to date11.

alter type all (edate = date11).

SPSS ALTER TYPE - Change String Lengths

ALTER TYPE can change the lengths of string variables. For example, the last respondent in our data got married and changed her last name to “Hernandez-Garcia”. We can't readily correct this: as we can see in variable view, last_name has an A9 format and can thus hold up to 9 characters.

SPSS ALTER TYPE String Length Insufficient

The syntax below solves this by increasing its length with ALTER TYPE.

*1. This doesn't work because new name is too long for A9 format.

if id = 595 last_name = 'Hernandez-Garcia'.

*2. Increase string length of last_name to (max) 30 characters.

alter type last_name(a30).

*3. Now last_name is corrected successfully.

if id = 595 last_name = 'Hernandez-Garcia'.

Value Labels of Numeric Variable to String Variable

Note that first_name is a numeric variable in our data. We can change it to a string variable with ALTER TYPE but this will convert the values (1 through 10) instead of the last names, which are in the value labels. The solution is using VALUELABELS but the entire process requires some manual steps outlined in the syntax below.

*1. Create new string to pass value labels into.

string tmp(a30).

*2. Pass value labels (last names) into string.

compute tmp = valuelabels(first_name).

*3. Delete original numeric version.

delete variables first_name.

*4. Rename new variable to old variable name.

rename variables tmp = first_name.

*5. Restore original variable order.

add files file */keep id first_name all.

SPSS ALTER TYPE - Minimize String Lengths

A nice ALTER TYPE trick is minimizing the lengths of all string variables in the data. We can do so by setting all A formats to AMIN: a special ALTER TYPE keyword denoting the minimum length for each string variable.
In the previous examples, we guessed that 30 characters would be enough for first_name and last_name. These lengths are actually longer than necessary, causing the size of the data file to increase. We can easily minimize all string lengths by using the aforementioned filter feature: alter type all(a = amin).

SPSS ALTER TYPE minimize string widths

As we see from the result, the minimal required lengths for first_name and last_name are 9 and 16.

Previous tutorial: Path (General Concept)

Next tutorial: SPSS ANY Function

Let me know what you think!

*Required field. Your comment will show up after approval from a moderator.