converting string fraction to numeric

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

converting string fraction to numeric

Marty Giesen-2
I've found various syntax for converting parts of strings or string
numbers to true numerics. However, can't seem to find a way to do what
should be an easy conversion.

I have a string variable that contains fractions such as

3/90
74/90
"blanks"
...

Looking for a conversion to the actual numeric fractional values and
blanks to sysmis.
Any suggestions would be appreciated.

Marty Giesen
Miss. State U.

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

Marks, Jim
Marty:

Assuming one "/" character in each string.

NEW FILE .
DATA LIST free /frac_string (a10).
BEGIN DATA
 3/90 , , 76/99 ,1/100
END DATA.
DATASET NAME tst WINDOW = FRONT.

DO IF INDEX(frac_string, '/') > 0.
COMPUTE frac_value =
   NUMBER(SUBSTR(frac_string,1,INDEX(frac_string,'/')-1),F8.0)
      /
  NUMBER(SUBSTR(frac_string,INDEX(frac_string,'/')+1),F8.0).
END IF.
EXECUTE .

This finds the string before the '/' and converts it to a number, finds
the string after the '/' and converts it into a number, then divides the
first by the second.

--jim

-----Original Message-----
From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of
Marty Giesen
Sent: Tuesday, May 19, 2009 4:40 PM
To: [hidden email]
Subject: converting string fraction to numeric

I've found various syntax for converting parts of strings or string
numbers to true numerics. However, can't seem to find a way to do what
should be an easy conversion.

I have a string variable that contains fractions such as

3/90
74/90
"blanks"
...

Looking for a conversion to the actual numeric fractional values and
blanks to sysmis.
Any suggestions would be appreciated.

Marty Giesen
Miss. State U.

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

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

Art Kendall
In reply to this post by Marty Giesen-2
Open a new instance of SPSS.  Copy the syntax below to a syntax file.
Click <run>. Click <all>.
Is this what you are looking for?





data list list/ mystring (a8).
begin data
"3/90"
"74/90"
"    "
end data.
compute splitter = index(mystring,'/').
compute howlong=length(rtrim(mystring)).
compute thepiece= howlong-splitter.
do if splitter ne 0.
compute numerator   = number(substr(mystring, 1, splitter-1), f5).
compute denominator = number(substr(mystring, splitter+1),f5).
compute myfraction  = numerator/denominator.
ELSE  .
compute myfraction  = -999.
compute numerator   = -999.
compute denominator = -999.
missing valuesnumerator denominator myfraction (-999).
end if.
value labels myfraction numerator denominator -999 ' input did not
contain a fraction as was expected'.
formats splitter howlong, thepiece numerator denominator (f5) myfraction
(f7.4).
list.

Art Kendall
Social Research Consultants

Marty Giesen wrote:

> I've found various syntax for converting parts of strings or string
> numbers to true numerics. However, can't seem to find a way to do what
> should be an easy conversion.
>
> I have a string variable that contains fractions such as
>
> 3/90
> 74/90
> "blanks"
> ...
>
> Looking for a conversion to the actual numeric fractional values and
> blanks to sysmis.
> Any suggestions would be appreciated.
>
> Marty Giesen
> Miss. State U.
>
> =====================
> 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
>
>
>

=====================
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
Art Kendall
Social Research Consultants