SPSS adding another decimal point to string with decimal when converting to numeric?

Viewed 36

Somewhere along the way, the decimal points got dropped from a portion of my dataset, and I am trying to put them back in. I know that they go after the first character.

The field is called Weight, and is numeric with nine decimal places. Here's an idea:

DATA LIST  FREE / Weight (f11.9).
BEGIN DATA.
12736489222, 738572978222, 82928375623, 1.0293847562, 0.8273628367, 0.9283740189
END DATA.

This is the code I'm currently using:

string weightString3(a11).
COMPUTE weightString3 = string(Weight,f11.9).
EXECUTE.

NUMERIC weightFixed3(f11.9).
COMPUTE weightFixed3 = 0.
IF (Weight < 0.5) weightFixed3 = Weight.
IF (Weight > 0.5) weightFixed3 = NUMBER(CONCAT(char.substr(weightString3,1,1),".",char.substr(weightString3,2)), f11).
EXECUTE.

This is throwing an error, because NUMBER is adding a decimal point to the end of my strings - which already contain decimal points thanks to "." in CONCAT. So 738572978222 should be "7.385729782", but instead it becomes "7.385729782.", which isn't a valid number format so it gets set as SYSTEM MISSING.

I can't rstrip the final "." because it's a numeric variable, and if I convert it to string for rstrip and then back to numeric it'll just happen again. Specifying 'f11.0' for the number format didn't help either. Any ideas? Is there a smarter way to insert the "." I need than the CONCAT and SUBSTR functions?

1 Answers

It seems like the condition for changing weight variable are wrong - right now the code is trying to change ALL the numbers in the example, but 1.0293847562 and 0.8273628367 should not be changed - trying to add a decimal to them will of course fail because they already have one... When I try to change the condition everything seems to go well:

IF (Weight < 100) weightFixed3 = Weight.
IF (Weight > 100) weightFixed3 = 
 NUMBER(CONCAT(char.substr(weightString3,1,1),".",char.substr(weightString3,2)), f11.0).

Now if the new condition is right, here's another, simpler way to get to the same results:

compute weightALT=weight.
if weight>100 weightALT=weight/10000000000.
Related