string to numeric

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

string to numeric

Ian Masson
I need to transform a string variable into a numeric variable. We have
1000's of participants, thus, I was wondering if anyone knew of a syntax
program or feature that can transform letters into numbers in a given
variable. For example, our string variable is what we use to ID
participants. To ensure that the length of the variable was the same for
each participant (for multilevel modeling analyses), we added several X's to
the end of each person's ID. I am looking for a way to convert all of these
X's into 0's. I am afraid of the high likelihood of error if I do it by hand
(and also, the sheer time consuming nature). Thus, any information or wisdom
on how to do this in a more efficient and effective manner would be greatly
appreciated.

Thanks!

Ian

=====================
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: string to numeric

Bruce Weaver
Administrator
Ian Masson wrote
I need to transform a string variable into a numeric variable. We have
1000's of participants, thus, I was wondering if anyone knew of a syntax
program or feature that can transform letters into numbers in a given
variable. For example, our string variable is what we use to ID
participants. To ensure that the length of the variable was the same for
each participant (for multilevel modeling analyses), we added several X's to
the end of each person's ID. I am looking for a way to convert all of these
X's into 0's. I am afraid of the high likelihood of error if I do it by hand
(and also, the sheer time consuming nature). Thus, any information or wisdom
on how to do this in a more efficient and effective manner would be greatly
appreciated.

Thanks!

Ian

Do you want to replace ALL cases of "X" with "0"--i.e., are all the non-X characters digits?  If so, use the REPLACE function (in a COMPUTE command).  There should be examples in the Help files.

Once you have converted all X's to 0's, use ALTER TYPE to change the format from string to numeric.  Again, see the examples in the Help files.

--
Bruce Weaver
bweaver@lakeheadu.ca
http://sites.google.com/a/lakeheadu.ca/bweaver/

"When all else fails, RTFM."

PLEASE NOTE THE FOLLOWING: 
1. My Hotmail account is not monitored regularly. To send me an e-mail, please use the address shown above.
2. The SPSSX Discussion forum on Nabble is no longer linked to the SPSSX-L listserv administered by UGA (https://listserv.uga.edu/).
Reply | Threaded
Open this post in threaded view
|

Re: string to numeric

Ruben Geert van den Berg
In reply to this post by Ian Masson
Dear Ian,
 
If I understand you correctly, the syntax below should help.
 
Best regards!

Ruben van den Berg

Methodologist

TNS NIPO

E: [hidden email]

P: +31 20 522 5738

I: www.tns-nipo.com

 
*Create example data.

data list free/id(a6).
begin data
1XXXXX
12XXXX
123XXX
1234XX
12345X
123456
end data.
 
*Convert id into numeric id.
 
comp numeric_id=num(rep(id,'X','0'),F6).
exe.
 
*end.

 

> Date: Wed, 10 Feb 2010 18:12:12 -0500
> From: [hidden email]
> Subject: string to numeric
> To: [hidden email]
>
> I need to transform a string variable into a numeric variable. We have
> 1000's of participants, thus, I was wondering if anyone knew of a syntax
> program or feature that can transform letters into numbers in a given
> variable. For example, our string variable is what we use to ID
> participants. To ensure that the length of the variable was the same for
> each participant (for multilevel modeling analyses), we added several X's to
> the end of each person's ID. I am looking for a way to convert all of these
> X's into 0's. I am afraid of the high likelihood of error if I do it by hand
> (and also, the sheer time consuming nature). Thus, any information or wisdom
> on how to do this in a more efficient and effective manner would be greatly
> appreciated.
>
> Thanks!
>
> Ian
>
> =====================
> 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


New Windows 7: Simplify what you do everyday. Find the right PC for you.