Announcement

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

  • Count the lenght in numeric observations

    Hello,

    I am working with a numeric variable that, on some occasions, uses 10 digits, and 8 in others. I would like to count the occurrences of each situation. Any suggestion?

    Thanks.

  • #2
    If you have only digits, no decimals, you could do something like this.
    Code:
    clear
    set obs 5
    set seed 4321
    gen x = runiformint(1, 2000)
    gen length = strlen(string(x))
    list
    tab length
    
    
    . list
    
         +---------------+
         |    x   length |
         |---------------|
      1. | 1808        4 |
      2. |  687        3 |
      3. | 1032        4 |
      4. |  328        3 |
      5. |   75        2 |
         +---------------+
    
    . tab length
    
         length |      Freq.     Percent        Cum.
    ------------+-----------------------------------
              2 |          1       20.00       20.00
              3 |          2       40.00       60.00
              4 |          2       40.00      100.00
    ------------+-----------------------------------
          Total |          5      100.00

    Comment


    • #3
      Excellent Wouter, this is what I needed.
      Thank you!

      Comment


      • #4
        floor(log10(whatever)) should show 8 or 10 correspondingly.

        Comment


        • #5
          If the two choices are really just 8 digits or 10 digits
          Code:
          generate ten_dig = whatever >= 1000000000
          And let me note that if you do have a numeric variable holding 10-digit values, it must be of type double or else you have precision problems and possibly incorrect values.
          Code:
          . set obs 1
          number of observations (_N) was 0, now 1
          
          . generate float x = 1234567890
          
          . generate double y = 1234567890
          
          . format %12.0f x y
          
          . list
          
               +-------------------------+
               |          x            y |
               |-------------------------|
            1. | 1234567936   1234567890 |
               +-------------------------+

          Comment


          • #6
            Thanks, Mr. Cox and Mr. Lisowski for your help!

            Comment


            • #7

              Also, 1 billion lies in between

              Code:
              count if x < 1e9 
              count if inrange(x, 1e9, .)

              Comment


              • #8
                Originally posted by Nick Cox View Post
                floor(log10(whatever)) should show 8 or 10 correspondingly.
                This needs to be
                Code:
                floor(log10(whatever))+1
                or it could be
                Code:
                ceil(log10(whatever))
                Was looking up this to determine leading digit for Benford's law.

                Comment

                Working...
                X