data type conversion from string to numeric

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

data type conversion from string to numeric

s_pushparaj
Hi List
What is the command syntax to convert a previously defined string type variable into to a new numeric type variable in the same name.
with thanks
pushparaj

====================To manage your subscription to SPSSX-L, send a message to
[hidden email] (not to SPSSX-L), with no body text except the
command. To leave the list, send the command
SIGNOFF SPSSX-L
For a list of commands to manage subscriptions, send the command
INFO REFCARD
Reply | Threaded
Open this post in threaded view
|

Re: data type conversion from string to numeric

Richard Ristow
At 11:17 AM 11/25/2007, s_pushparaj wrote:

>What is the command syntax to convert a previously defined string
>type variable into to a new numeric type variable in the same name.

In SPSS 16, see syntax command ALTER TYPE. Otherwise, it's a little
crude. If your initial data is,
|-----------------------------|---------------------------|
|Output Created               |26-NOV-2007 18:22:04       |
|-----------------------------|---------------------------|
CaseID Value Alpha

     1  6.0   Alpha
     2  5     Beta
     3  004   Gamma

Number of cases read:  3    Number of cases listed:  3

where 'Value' is a four-character string variable (A4), you can do this:

NUMERIC NewValu (F3).
COMPUTE NewValu = NUMBER(Value,F4).

RENAME VARIABLES
    (Value NewValu=OldValue Value).
LIST.

List
|-----------------------------|---------------------------|
|Output Created               |26-NOV-2007 18:22:31       |
|-----------------------------|---------------------------|
CaseID OldValue Alpha  Value

     1  6.0      Alpha     6
     2  5        Beta      5
     3  004      Gamma     4

Number of cases read:  3    Number of cases listed:  3


However, it does leave the original string variable in the file,
under a changed name; and it puts the new numeric variable at the end
of the variable list, rather than at the original variable's place.
===================
APPENDIX: Test data
===================
DATA LIST LIST
   /CaseID (F3) Value (A4) Alpha (A6).
BEGIN DATA
01 6.0 Alpha
02 5   Beta
03 004 Gamma
END DATA.

LIST.

=====================
To manage your subscription to SPSSX-L, send a message to
[hidden email] (not to SPSSX-L), with no body text except the
command. To leave the list, send the command
SIGNOFF SPSSX-L
For a list of commands to manage subscriptions, send the command
INFO REFCARD