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