Announcement

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

  • Split numeric variables depending on the total number of digit

    Hi folks, I am running into a problem where I have to split a long numeric variable into parts. However, this variable has different total digits, for instance, some have 12 digits and some have 14 digits, etc. What I need is to split the variable at different digit depending on the total number of digit. For instance, if the variable is 14 digit, I need to cut at the 6th and 10th digit whereas if the variable is 12 digit, I need to cut at the 4th and 7th digit. Anyone knows how to achieve this? Thanks!

  • #2
    Are they all either 12 or 14 digits? If not, what are the split points for numbers of other lengths? Is there a general formula?

    Also, I think you've been on Statalist long enough to know that if you want help with code you should show example data, and do that with the -dataex- command.

    Comment


    • #3
      This is old and Man Yang (hopefully) has already gotten an answer, but I thought I would post how to do this for others coming along afterwards.

      Some toy data:
      Code:
      * Example generated by -dataex-. To install: ssc install dataex
      clear
      input double number
      21245678910112
      55588878941540
      88845678949888
      80165478941854
      97245679010035
        385456788792
        214456789498
        801822245373
        123456789498
      end
      Code:
      format number %14.0f
      gen num_string = string(number, "%15.0f")  // I had to use string() rather than tostring because the number had more than 11 digits
      gen len = strlen(num_string)
      
      * Actually splitting the 14-digit number
      gen part1 = substr(num_string, 1, 5)  if len==14  
      gen part2 = substr(num_string, 6, 4)  if len==14  // digits 6, 7, 8, and 9
      gen part3 = substr(num_string, 10, .) if len==14  // digits 10-14.  The "." at end means, "until the end of the string"
      
      * Splitting the 12-digit number
      replace part1 = substr(num_string, 1, 3) if len==12
      replace part2 = substr(num_string, 4, 3) if len==12
      replace part3 = substr(num_string, 7, .) if len==12
      
      . format number %15.0fc  // just to make it easier to read that splitting turned out
      . list
      
           +-----------------------------------------------------------------+
           |          number       num_string   len   part1   part2    part3 |
           |-----------------------------------------------------------------|
        1. |  21245678910112   21245678910112    14   21245    6789    10112 |
        2. |  55588878941540   55588878941540    14   55588    8789    41540 |
        3. |  88845678949888   88845678949888    14   88845    6789    49888 |
        4. |  80165478941854   80165478941854    14   80165    4789    41854 |
        5. |  97245679010035   97245679010035    14   97245    6790    10035 |
           |-----------------------------------------------------------------|
        6. | 385,456,788,792     385456788792    12     385     456   788792 |
        7. | 214,456,789,498     214456789498    12     214     456   789498 |
        8. | 801,822,245,373     801822245373    12     801     822   245373 |
        9. | 123,456,789,498     123456789498    12     123     456   789498 |
           +-----------------------------------------------------------------+
      Last edited by David Benson; 15 Dec 2018, 00:37.

      Comment


      • #4
        Just a note that tostring with the format() option will also do the necessary conversion.
        Code:
        . tostring number, generate(num_string) format(%15.0f)
        num_string generated as str14
        
        . format number %15.0f
        
        . list, clean
        
                       number       num_string  
          1.   21245678910112   21245678910112  
          2.   55588878941540   55588878941540  
          3.   88845678949888   88845678949888  
          4.   80165478941854   80165478941854  
          5.   97245679010035   97245679010035  
          6.     385456788792     385456788792  
          7.     214456789498     214456789498  
          8.     801822245373     801822245373  
          9.     123456789498     123456789498

        Comment

        Working...
        X