Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Count the digits of variable

    Hello,

    I would like to create a variable ('digit') that counts the number of digits of a number variable.
    For instance:
    code Digit
    1 1
    1233 4
    Thank you in advance!

  • #2
    This seems to do what you want on your limited data example.
    Code:
    . generate wanted = length(string(code))
    
    . list, clean noobs
    
        code   digit   wanted  
           1       1        1  
        1233       4        4  
    
    .

    Comment


    • #3
      it is much easier if you use -dataex- for your examples; please read and follow the FAQ; here is one way:
      Code:
      gen length=length(string(x))
      where I used "x" instead of your "code" and I named the new variable "length" instead of "Digit" - change as befits your data and your desires

      Comment


      • #4
        Thank you very much for the replies! I will use -dataex- all the time from now!

        Comment

        Working...
        X