Announcement

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

  • Variable counting the number of digits

    Hello,

    I have the following data:

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input int isic3_4d
     121
     121
     121
     121
     121
     122
     122
     122
     121
     121
     122
     122
     122
     122
     122
     122
     122
    1511
    1511
    1511
    1511
    1511
    1511
    1511
    1511
    1511
    1511
    1511
    1511
    1511
    1511
    1511
    1511
    1511
    1511
    1511
    1511
    1511
    1511
    1511
    1511
    1511
    1511
    1511
    1511
    1511
    1511
    1511
    1511
    1511
    1511
    1511
    1511
    1511
    1511
    1511
    1511
    1511
    1511
    1511
    1511
    1511
     122
    1511
    1511
    1511
    1511
    1511
    1511
    1511
     500
     500
     500
     500
     500
     500
     500
     500
     500
     500
     500
     500
     500
     500
     500
     500
     500
     500
     500
     500
     500
     500
     500
     500
     500
    1512
    1512
    1512
    1512
    1512
    end
    I would like to create a variable next to this one, indicating how many digits are in this variable, for example the first entry should be 3 corresponding to 3 digits in the variable.

    Please let me know how to implement this.

  • #2
    Hi Jad,

    Is this what you are looking for?
    tostring isic3_4d, replace
    gen want =strlen( isic3_4d )

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input str4 isic3_4d float want
    "121"  3
    "121"  3
    "121"  3
    "121"  3
    "121"  3
    "122"  3
    "122"  3
    "122"  3
    "121"  3
    "121"  3
    "122"  3
    "122"  3
    "122"  3
    "122"  3
    "122"  3
    "122"  3
    "122"  3
    "1511" 4
    "1511" 4
    "1511" 4
    "1511" 4
    "1511" 4
    "1511" 4
    "1511" 4
    "1511" 4
    "1511" 4
    "1511" 4
    "1511" 4
    "1511" 4
    "1511" 4
    "1511" 4
    "1511" 4
    "1511" 4
    "1511" 4
    "1511" 4
    "1511" 4
    "1511" 4
    "1511" 4
    "1511" 4
    "1511" 4
    "1511" 4
    "1511" 4
    "1511" 4
    "1511" 4
    "1511" 4
    "1511" 4
    "1511" 4
    "1511" 4
    "1511" 4
    "1511" 4
    "1511" 4
    "1511" 4
    "1511" 4
    "1511" 4
    "1511" 4
    "1511" 4
    "1511" 4
    "1511" 4
    "1511" 4
    "1511" 4
    "1511" 4
    "1511" 4
    "122"  3
    "1511" 4
    "1511" 4
    "1511" 4
    "1511" 4
    "1511" 4
    "1511" 4
    "1511" 4
    "500"  3
    "500"  3
    "500"  3
    "500"  3
    "500"  3
    "500"  3
    "500"  3
    "500"  3
    "500"  3
    "500"  3
    "500"  3
    "500"  3
    "500"  3
    "500"  3
    "500"  3
    "500"  3
    "500"  3
    "500"  3
    "500"  3
    "500"  3
    "500"  3
    "500"  3
    "500"  3
    "500"  3
    "500"  3
    "1512" 4
    "1512" 4
    "1512" 4
    "1512" 4
    "1512" 4
    end

    Comment


    • #3
      Yes that does the trick! Thanks.

      Comment


      • #4
        Code:
        * Example generated by -dataex-. For more info, type help dataex
        clear
        input int isic3_4d
         121
         121
         121
         121
         121
         122
         122
         122
         121
         121
         122
         122
         122
         122
         122
         122
         122
        1511
        1511
        1511
        1511
        1511
        1511
        1511
        1511
        1511
        1511
        1511
        1511
        1511
        1511
        1511
        1511
        1511
        1511
        1511
        1511
        1511
        1511
        1511
        1511
        1511
        1511
        1511
        1511
        1511
        1511
        1511
        1511
        1511
        1511
        1511
        1511
        1511
        1511
        1511
        1511
        1511
        1511
        1511
        1511
        1511
         122
        1511
        1511
        1511
        1511
        1511
        1511
        1511
         500
         500
         500
         500
         500
         500
         500
         500
         500
         500
         500
         500
         500
         500
         500
         500
         500
         500
         500
         500
         500
         500
         500
         500
         500
        1512
        1512
        1512
        1512
        1512
        end 
        
        gen wanted = ceil(log10(isic3_4d))
        
        * Stata Journal 
        groups isic3_4d wanted 
        
          +-------------------------------------+
          | isic3_4d   wanted   Freq.   Percent |
          |-------------------------------------|
          |      121        3       7      7.00 |
          |      122        3      11     11.00 |
          |      500        3      25     25.00 |
          |     1511        4      52     52.00 |
          |     1512        4       5      5.00 |
          +-------------------------------------+
        
        .

        Comment

        Working...
        X