IBM Support

Which SPSS commands convert string to numeric variables?

Troubleshooting


Problem

I'm using SPSS for Windows. In the past, I've been able to convert variables I have stored in a string format to numeric variables simply by opening a Define Variable menu and checking the appropriate option under the Type button. SPSS echoes in my Output Viewer (or Navigator, or Window) a FORMATS command specifying my string variable name and a format of F8. Now I'd like to do the same thing in an SPSS command file. Yet when I include the very command SPSS claims to have used in converting my previous variable, I get a message saying FORMATS strngvar (F8). >Error # 4836 in column 9. Text: STRNGVAR >Only the formats A and AHEX can be applied to string variables. >This command not executed. If this command works when SPSS changes the format in the Define Variable menu, why doesn't it work when I invoke it through command syntax?

Symptom

Resolving The Problem

When SPSS converts a variable from a string to numeric format (or from any format to another) through the Define Variable menu, it executes a process in the Data Editor that is not driven exclusively by SPSS commands. This process allows SPSS to convert invisibly a variable format while leaving it in the same place in the data file. The FORMATS command is eventually applied to the new variable, but this is the only portion of the process executed via SPSS commands, and hence is the only command that shows up in the log and in the journal file. It is a "new" variable in so far as all other dictionary information attached to the previous variable is lost, but it otherwise assumes the position in the file of the previous variable.

You can compute a new numeric variable from the original string variable through SPSS command syntax using either of two routes:

RECODE strngvar (CONVERT) INTO numvar.

or

COMPUTE numvar=NUMBER(strngvar,Fw.d).

Substitute for "Fw.d" in the second example the numeric format you want to use to read the string variable. Once numvar is computed through either means, you can use a FORMATS command to format it for display, as does the routine executed via the Define Variable menu.

This method will not, as does the Define Variable menu option, replace the original string variable with a new numeric variable of the same name. It merely adds the new variable to the end of the file and leaves the original string intact. If you have no further use for the original string variable, you
can specify it on a DROP subcommand the next time you execute the SAVE command.

If, for whatever reasons, you would like to emulate in SPSS command syntax what the menu option does, you can MATCH the file to itself, specifying the same variable order as found in the current file but substituting the numeric variable name for the original string one, and then execute a RENAME VARIABLES command which converts the numeric variable name to the original string name. None of this is necessary to make use of the new numeric variable, and need only be done if there is an overriding need to do exactly what the Define Variable menu does when it converts a variable format. Below is a brief sample job which, when run, illustrates this procedure, converting v3 to a numeric variable and situating it in the active file where its string counterpart formerly existed.


DATA LIST/v1 to v4 1-4 (a).
BEGIN DATA
1234
3456
2345
END DATA.

COMPUTE numvar=NUMBER(v3,F1).
MATCH FILES FILE=*
/KEEP v1 v2 numvar v4.
RENAME VARIABLES (numvar=v3).
EXE

[{"Product":{"code":"SSLVMB","label":"IBM SPSS Statistics"},"Business Unit":{"code":"BU059","label":"IBM Software w\/o TPS"},"Component":"Not Applicable","Platform":[{"code":"PF025","label":"Platform Independent"}],"Version":"19.0","Edition":"","Line of Business":{"code":"LOB10","label":"Data and AI"}}]

Historical Number

14810

Document Information

Modified date:
16 April 2020

UID

swg21476065