SPSS TUTORIALS BASICS ANOVA REGRESSION FACTOR CORRELATION

# SPSS ALTER TYPE – Simple Tutorial

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.

## SPSS ALTER TYPE Pitfall

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:

• Always copy your variables before converting them. A great tool for cloning many (or all) variables in your data is freely downloadable from Clone Variables.
• Just run ALTER TYPE on your original variables but check the results for system missings immediately afterwards. If they are present and you don't know why, close your data, rerun you syntax up to the suspicious ALTER TYPE command and inspect what values could cause the problem.

## 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 number of cases in our data.

## 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 - 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.

*Set correct start date for case with id = 482.

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

## SPSS ALTER TYPE - Filter

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.

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'.
exe.

*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'.
exe.

## 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 VALUELABEL 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).
exe.

*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.
exe.

## 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).

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

# Tell us what you think!

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

# THIS TUTORIAL HAS 6 COMMENTS:

• ### By Amy on August 9th, 2018

Hi Reuben - Love the site! So helpful :) you know you can use alter type to set string variable widths to the minimum based on the content of the string, for example - 'ALTER TYPE ALL(A=AMIN)' - can the same be done for numeric variables? I can't find a way.... many thanks

• ### By Ruben Geert van den Berg on August 9th, 2018

Hi Amy!

All numbers in SPSS -data and output- are in double-precision floating-point format which means they virtually always have a lot of decimal places. You can't remove those.

However, you can hide/show decimal places with FORMATS if you're willing to specify all variable names such as

FORMATS V1 V5 TO V10 (F1).

Alternatively, using

ALTER TYPE ALL(F3.2 = F1).

will show all variables having the f3.2 format as f1. Keep in mind that this still doesn't actually chance the values -which are still floating points unless you convert them into strings. You merely adjust how SPSS displays the values so the operation is reversible.

Tiny warning: setting all numeric variables to f1 works fine in SPSS but causes a world of trouble when exporting data to Excel/.txt/.dat/.csv and so on. In this case, you should use formats that are (at least) long enough to hold all data you'd like to export.

Hope that helps!

• ### By chris cordell on September 4th, 2018

love your stuff. What's the best way to approach something like "alter type all a=amin" for numeric variables?

• ### By Ruben Geert van den Berg on September 5th, 2018

Hi Chris, thanks for the compliment!

There's no real equivalent for numeric variables. I recently used (note that I need 2 commands)

alter type all(f5.4 = f5).
alter type all(f4.3 = f5).

for hiding all decimal places for a set of numeric variables without having to specify their names. But if some variable is in f3.2 format, it won't be affected and I'll need yet another command.

Insofar as I know, this is as good as it gets. If you don't mind specifying variable names, use FORMATS as in

formats v1 v3 v8 to v10 (f3).

or something. Keep in mind that neither approach changes the actual data values. They are merely displayed without decimal places but the decimal places are still there.

Hope that helps!

SPSS tutorials

• ### By William Peck on January 17th, 2020

SPSS Tutorials comes up straight away on Google … …

Data comes into SPSS as 3.73 (a student's grade point average on scale of 4), but SPSS looks at it like 3.73000000000000 … which is how it downloads to Excel. I'd like it just stay at 3.73 in SPSS.