SPSS - How to Convert String Variables into Numeric?
SPSS tutorials website header logo SPSS TUTORIALS VIDEO COURSE BASICS ANOVA REGRESSION FACTOR

How to Convert String Variables into Numeric Ones?

Introduction & Practice Data File

Converting an SPSS string variable into a numeric one is simple. However, there's a huge pitfall that few people are aware of: string values that can't be converted into numbers result in system missing values without SPSS throwing any error or warning.

This can mess up your data without you being aware of it. Don't believe me? I'll demonstrate the problem -and the solution- on convert-strings.sav, part of which is shown below.

SPSS String To Numeric Data View

SPSS Strings to Numeric - Wrong Way

First off, you can convert a string into a numeric variable in variable view as shown below.

SPSS Convert String To Numeric Variable In Variable View

Now, I never use this method myself because

So What's the Problem?

Well, let's do it rather than read about it. We'll

  1. set empty cells as user missing values for s3;
  2. convert s3 to numeric in variable view;
  3. run descriptives on the result.
*Set empty string as user missing value for s3.

missing values s3 ('').

*Inspect frequency table for s3.

frequencies s3.

*Now manually convert s3 to numeric under variable view.

*Inspect result.

descriptives s3.

*N = 444 instead of 459. That is, 15 values failed to convert and we've no clue why.

Result

SPSS Frequency Table Before Conversion

Note that some values in our string variable have been flagged with “a”. We probably want these to be converted into numbers. We have 459 valid values (non empty cells).

SPSS String To Numeric Descriptives Conversion Errors

After converting our variable to numeric, we ran some descriptives. Note that we only have N = 444. Apparently, 15 values failed to convert -probably not what we want. And we usually won't notice this problem because we don't get any warning or error.

Conversion Failures - Simplest Solution

Right, so how can we perform the conversion safely? Well, we just

In our first example, the frequency table already suggested we must remove the “a” from all values before converting the variable. We'll do just that in a minute.
Although safe, I still think this method is too much work, especially for multiple variables. Let's speed things up by using some handy syntax.

SPSS - String to Numeric with Syntax

The fastest way to convert string variables into numeric ones is with the ALTER TYPE command.This requires SPSS version 16 or over. For SPSS 15 or below, use the NUMBER function. It allows us to convert many variables with a single line of syntax.
The syntax below converts all string variables in one go. We then check a descriptives table. If we don't have any system missing values, we're done.

SPSS ALTER TYPE Example

*Close data without saving and reopen before proceeding.

*Convert all variables in one go.

alter type s1 to s3 (f1) s4 (f6.3).

*Inspect descriptives.

descriptives s1 to s4.

Note: using alter type s1 to s4 (f1). will also work but the decimal places for s4 won't be visible. This is why we set the correct f format: f6.3 means 6 characters including the decimal separator and 3 decimal places as in 12.345. Which is the format of our string values.

Result

SPSS String To Numeric System Missings Descriptives Table

Since we've 480 cases in our data, we're done for s1. However, the other 3 variables contain system missings so we need to find out why. Since we can't undo the operation, let's close our data without saving and reopen it.

Solution 2: Copy String Variables Before Conversion

Things now become a bit more technical. However, readers who struggle their way through will learn a very efficient solution that works for many other situations too. We'll basically

Precisely, we'll flag non empty string values that are system missing after the conversion. As these are at least suspicious, we'll call those conversion failures. This may sound daunting but it's perfectly doable if we use the right combination of commands. Those are mainly STRING, RECODE, DO REPEAT and IF.

Copy and Convert Several String Variables

*Close data without saving and reopen before proceeding.

*Copy all string variables.

string c1 to c4 (a7).

recode s1 to s4 (else = copy) into c1 to c4.

*Convert variables to numeric.

alter type s1 to s3 (f1) s4 (f6.3).

*For each variable, flag conversion failures: cases where converted value is system missing but original value is not empty.

do repeat #conv = s1 to s4 / #ori = c1 to c4 / #flags = flag1 to flag4.
if(sysmis(#conv) and #ori <> '') #flags = 1.
end repeat.

*If N > 0, conversion failures occurred for some variable.

descriptives flag1 to flag4.

Result

SPSS Alter Type Conversion Failures

Only flag3 and flag4 contain some conversion failures. We can visually inspect what's the problem by moving these cases to the top of our dataset.

*Visually inspect why values fail to convert.

sort cases by flag3 (d).

*Some values flagged with 'a'.

sort cases by flag4 (d).

*Some values flagged with 'a' through 'e'.

Result

SPSS String To Numeric Conversion Failures Data View

Remove Illegal Characters, Copy and Convert

Some values are flagged with letters “a” through “e”, which is why they fail to convert. We'll now fix the problem. First, we close our data without saving and reopen it. We then rerun our previous syntax but remove these letters before the conversion.

Syntax

*Close data without saving and reopen before proceeding.

*Copy all stringvars.

string c1 to c4 (a7).

recode s1 to s4 (else = copy) into c1 to c4.

*Remove 'a' from s3.

compute s3 = replace(s3,'a','').

*Remove 'a' through 'e' from s4.

do repeat #char = 'a' 'b' 'c' 'd' 'e'.
compute s4 = replace(s4,#char,'').
end repeat.

*Try and convert variable again.

alter type s1 to s3 (f1) s4 (f6.3).

*Flag conversion failures again.

do repeat #conv = s1 to s4 / #ori = c1 to c4 / #flags = flag1 to flag4.
if(sysmis(#conv) and #ori <> '') #flags = 1.
end repeat.

*Inspect if conversion succeeded.

descriptives flag1 to flag4.

*N = 0 for all flag variables so we're done.

*Delete copied and flag variables.

delete variables c1 to flag4.

Result

SPSS No Statistics Are Computed Because There Are No Valid Cases

All flag variables contain only (system) missings. This means that we no longer have any conversion failures; all variables have been correctly converted. We can now delete all copy and flag variables, save our data and move on.

Thanks for reading!

SPSS 26 – Review of SPSS’ New Interface

SPSS 26 comes in both a new and the classic interface. We downloaded and tested the new interface. This review walks you through our main findings.

SPSS 26 - Which “Subscription” Am I On?

When downloading SPSS 26, we're offered both the new as well as the classic interface. Curiously, the IBM website mentions “SPSS Subscription” rather than “SPSS 26”.

SPSS 26 Download New Old User Interface

Before downloading it, we're already warned that the new interface “does not yet have all the same features” as the classic version. Well, ok. There's probably some tiny details they still need to work on. But they wouldn't launch a half-baked software package. Would they?

Anyway, we installed both versions and after doing so, our SPSS statistics directory looks like below.

SPSS 26 Subscription No Version Number

I was expecting a subfolder named 26 but instead I've Subscription and SubscriptionNew for the classic and new interfaces. It kinda makes me wonder is this actually SPSS 26? For SPSS 25 and older, this was always crystal clear: these versions mention their version number when starting or closing the program. In the middle of a session, navigating to Help SPSS Menu Arrow About shows the exact release such as 25.0.0.1. Alternatively, running the show license. syntax would also do the trick.
In the new interface, none of those options come up with anything but “IBM SPSS Statistics”. However, when digging into the backend subfolder, we found stats.exe, the actual application. And when we hovered over the file, we saw this:

SPSS Subscription Find Version Number

SPSS 26 indeed. But why do I even want to know my exact version number? Well,

IBM SPSS License Key Center Authorization Codes

SPSS 26 - Data View & Variable View Tabs

Anway, we downloaded and installed the SPSS 26 with the new interface. After opening some data, we get the data view tab shown below.

SPSS 26 Data View

The new interface is based on tabs rather than (data, syntax and output) windows. As we'll see in a minute, settings and dialogs also come as tabs.
Variable view is below data view and slides up upon clicking it. We didn't discover any shortcut for doing so.
The Edit and View menus are absent. I suppose they'll be added later. But for now, this really is a huge problem.
The value/label switch does not toggle between values and value labels but -rather- between variable names and variable labels.
An Analyze Catalog has been added.
There's notifications and settings icons. However, these settings don't include the essential settings under Edit SPSS Menu Arrow Options in previous SPSS versions. These seem unavailable in the new interface.

SPSS 26 - Data View Issues

Even this very first exploration of the new interface resulted in some serious issues.

SPSS 26 - Variable View

In SPSS’ new interface, variable view is called “Variable List”. It's in the same tab as data view and slides up when clicked. Unfortunately, it no longer has the clear and simple structure known from previous SPSS versions.

SPSS 26 Variable View

The order of variable properties has been altered with visibility and measurement levels before variable names.
The ability to hide variables is a welcome new feature. But what's the syntax for doing so? How to hide, say, v1 to v25 but not v26 to v50? That can't require 25 mouse clicks, right?
Width and columns have been removed. However, we'll get to those in a minute.
The value labels column is finally called “Value Labels” rather than “Values”. It now shows the number of labels defined for each variable.
Apart from the weird Serif fonts in Variable List, the alignment looks odd too: it seems some column headers are centered with their property values left aligned. Together with removing the classic grid, this doesn't contribute to a nice and clear interface.

We also noticed that clicking “Type” -which either refers to a variable type or format- opens up the dialog shown below.

SPSS 26 - Change Variable Type, Width or Format Variable type, width and format are shown when clicking “Type” in Variable List.

Note that a much better way to set formats and types are the FORMATS and ALTER TYPE commands: these can address many variables in one go and they are replicable.

SPSS 26 - Menus & Dialogs

For the most part, the new menus look like the old menus but incomplete and badly reorganized. For instance, let's take a look at Analyze as shown below.

SPSS 26 Analyze Menu

The most sought-after analyses in SPSS are

The chi-square independence test -deeply hidden under Analyze SPSS Menu Arrow Descriptive statistics SPSS Menu Arrow Crosstabs.
Correlations, now hidden under Prediction and Association.
Regression, also under Prediction and Association.
ANOVA, scattered over Group Comparison - Parametric and General Linear Model.
The independent samples t-test under Group Comparison - Parametric.

So why are the most popular analyses so hard to find? They're not meant to be literally “sought after”. These analyses should be listed at the top level of Analyze. Instead -however- Correlate and Regression have actually been removed from it.

SPSS 26 - Dialogs

The new dialogs look different from previous versions. Let's take a look at the Correlations dialog below.

SPSS 26 Correlations Dialog

To our disappointment, the Options are still the same as in previous versions. And they are not good: regardless of what we need, we always get all significance levels in our output. Confidence intervals for correlations are still not available. We still can't choose whether we want any sample sizes included in our output table.
The classic functions such as Help and Ok are somewhat scattered over the dialog. Unfortunately, “Paste to syntax” is hidden under “Run Analysis” so this now takes 2 clicks instead of 1. The Alt based shortkeys and Esc no longer work either.

SPSS 26 Output Tab - Tables & Charts

After running our analysis, we're presented with the output tab shown below.

SPSS 26 Output Tab

The output outline looks pretty nice and clean. However, when right-clicking an item, the OMS labels don't show up. For experienced users, this complicates batch editing tables with OUTPUT MODIFY.
The correlation table itself looks much better than previous SPSS versions as well. However, we stumbled upon some issues here too:

SPSS 26 - Charts

As shown below, charts still don't look too great in SPSS 26.

SPSS 26 Chart Example

None of the issues discussed in New Charts in SPSS 25 - How Good Are They Really? have been resolved: still no percent sign on the y-axis. Still the wrong title. And so on.

More worrying, the SPSS 26 Visualize menu only includes the Chart builder. I sure hope the legacy dialogs will soon be added: they are much simpler and result in short and clean syntax that's copy-paste-editable.

SPSS 26 - The Analyze Catalog

Another new feature is the Analyze Catalog shown below. It's accessible from the Data View tab.

SPSS 26 Analysis Catalogue

Sadly, the information here is incomplete and inaccurate.

For example, Group Comparison - Parametric doesn't mention any t-tests. Also, the one sample t-test and paired samples t-test don't compare “two or more groups”. The old description -“Compare Means”- did apply to all these tests.

Group Comparison - Nonparametric suggests that parametric tests require that “data follow the normal distribution”. This is wrong: the normality assumption is rarely required for reasonable sample sizes. This is why the Kolmogorov-Smirnov test is usually useless.

The way I see it is that

A product that tries to combine these conflicting purposes will suck at both. This point could hardly be illustrated more convincingly than by the analyze catalog.

Anyway. One thing I do like about the analyze catalog is the search function. So I don't immediately see ANOVA anywhere in the menu but searching for it will surely get me there right away. Right? Well, let's take a look at the search results below.

SPSS 26 Analyze Catalog ANOVA

The search results lead me to the Oneway dialog but not GLM or MEANS. The problem here is that

The real problem here is that a complete ANOVA can't be run from any single dialog or command in SPSS. Instead, we need to run a single analysis multiple times from different commands if we need all relevant output. And our search results don't even mention MEANS or GLM.

Ok, then let's search for a simpler analysis: the Kruskall-Wallis test.

SPSS 26 Analyze Catalog Kruskall Wallis

My search doesn't come up with anything at all. This is because I tested for a common misspelling of “Kruskal-Wallis test”. And the search function seems completely unable to handle it.

SPSS 26 - Conclusions

Summarizing this review, I think SPSS’ new interface is worse than the old interface
in basically all regards:

SPSS Variable View in Old Interface The nice and clear classic variable view

On top of that, the new interface is seriously incomplete. A handful of examples are that

Some of those may be added to the new interface. But what about first finishing a product and then launching it?

In any case, I agree that SPSS urgently needs major improvements. This review hinted at a couple of those but there's tons more. Sadly, none of the things SPSS really needs was added to version 26. One thing SPSS did not really need, was a new interface.

If it ain't broken, don't fix it.

Thanks for reading.

SPSS – How to fix “Your license will expire in … days”?

SPSS Your License Will Expire In 10 Days

I'm a pretty happy SPSS user but since a while, the software has been telling me that “Your license will expire in ... days.” I had already extended my license with another year but -apparently- SPSS seemed to be oblivious to that. It took me some trial and error to troubleshoot the issue. Since I'm running this website anyway, I thought I may just as well share my solution.
Note: before you start, close SPSS entirely.

Step 1 - Find your Authorization Code

After extending my license, I received a bunch of emails from IBM and one of them contained an authorization code denoted as “AuthCode” as shown below.

SPSS Authcode Example

If you really can't find it, you can get one from the IBM website (if you're a licensed user of course). I should add that actually getting things done here may be a long and tedious process -in my experience at least.

Step 2 - Run law.exe

With your authorization code nearby, open up the SPSS installation folder. For recent versions it may be something like C:\Program Files\IBM\SPSS\Statistics\24 in this folder you'll find a tiny program called law.exe as shown below.

SPSS Law.exe in Installation Folder

At first I thought this referred to something legal (“law” as in “breaking the law”). Then I learned that “law” is short for “License Authorization Wizard”. Let's run it, for instance by double clicking it.

Step 3 - Proceed through License Authorization Wizard

Most of the following steps are rather straightforward as shown below.

SPSS License Authorization Wizard 2

I must confess that I found the next step (below) somewhat confusing. However, I just entered the authorization code here, clicked Next and proceeded.

SPSS License Authorization Wizard3

Well, that did the job for me. In order to really make sure everything's ok, I'll propose a quick final check below.

Step 4 - Check

At this point, you can start SPSS and open a new syntax editor window. Copy-paste show license. into it and run it. The resulting table in the output viewer window should now confirm that you're good to go.

Thanks for reading!