Search code examples
spss

SPSS: How to count number of digits in variable


I need to count the number of digits in an SPSS numeric variable, and assign it to a different variable.

I tried converting it to a string and counting the length of the string with char.length(), but this returns the defined length of the variable, rather than the length of the actual string in each line.

Any ideas how this can be done?


Solution

  • when canculating the length of your string variable, use ltrimor rtrim(depending on how you calculated your string - just to be sure you could use both) to get rid of spaces and count only digits:

    compute Ndigits=char.length(ltrim(rtrim(YourString))).
    

    you could also do away with the text variable altogether and just use this function:

    compute Ndigits=trunc(lg10(YourNumber))+1.