Reputation: 13
I'm trying to compute a new variable with the first four digits of a numeric variable I have.
To begin with, I changed the existing numeric variable to String, and then used the following code in the syntax editor:
COMPUTE New_Varible = NUMBER(SUBSTR(Student_ID,1,2),f4) .
EXECUTE .
When I run the first line of code, it goes to the Transformation Pending mode, and a numeric scale variable is created with missing values (dots). But when I run the execute, the new scale variable suddenly changes to nominal, without showing any values. I've also tried it with the 'CHAR.SUBSTR' command, but to no avail.
P.S. I'm using SPSS version 26, 64-bit on Windows 10.
Upvotes: 1
Views: 893
Reputation: 11310
I assume your problem started when you changed your number variable into string. If the string is wider thant the number, you may have some extra spaces to the left of the digits in the string. So your command is reading spaces instead of digits, and then fails to turn them into a number.
This corrected command should hopefully do the trick, using ltrim
to remove the extra spaces before reading the digits (also note that you only read two characters with your original command - corrected here to four instead):
COMPUTE New_Varible = NUMBER(CHAR.SUBSTR(ltrim(Student_ID),1,4),f4) .
EXECUTE .
Upvotes: 1