compute number from string variable

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

compute number from string variable

vanels
Unfortunately, the syntax I found in previous threads did not work with my variable. Can anyone help??

My variable (numeric variable) contains 7 digits. I only want to extraxt the first 3 numbers.

How can I do that?

Thanks so much!

Elsje
Reply | Threaded
Open this post in threaded view
|

Automatic reply: [SPSSX-L] compute number from string variable

Cleland, Patricia (EDU)
I am out of the office until October 23 and will not be checking my emails. If your issue is urgent, please contact Caroline Rodrigues ([hidden email]) Otherwise I will reply when I return.

=====================
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
|

Automatic reply: compute number from string variable

Kevan Edwards (MDH)
In reply to this post by vanels

I am currently out of the office and will return on Tues Oct 30th.  I will have limited email access during that time.   I will try and respond to your email as quickly as possible given my absence from the office.

Reply | Threaded
Open this post in threaded view
|

Re: compute number from string variable

Bruce Weaver
Administrator
In reply to this post by vanels
If your string variable can have leading or trailing spaces, you may have to throw in an LTRIM or RTRIM, but here's the basic idea.

data list free / stringvar (a7).
begin data
1234567
7654321
2345678
3456789
end data.

* OP wants "first 3 numbers".
* Assuming that means 3 left-most digits in the string variable,
* here are two methods that work.

compute numvar1 = trunc(number(stringvar,f7) / 10**4).
compute numvar2 = number(char.substr(stringvar,1,3),f3).

* If first 3 numbers means 3 right-most digits, this will work.

compute numvar3 = number(char.substr(stringvar,5,3),f3).
formats numvar1 to numvar3 (f8.0).
list.

OUTPUT:

stringvar  numvar1  numvar2  numvar3
 
1234567        123      123      567
7654321        765      765      321
2345678        234      234      678
3456789        345      345      789



vanels wrote
Unfortunately, the syntax I found in previous threads did not work with my variable. Can anyone help??

My variable (numeric variable) contains 7 digits. I only want to extraxt the first 3 numbers.

How can I do that?

Thanks so much!

Elsje
--
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/).