Announcement

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

  • removing the first character of a string variable if it is zero

    Hi everyone,
    How can I remove the first character(s) of values if they are zero? For example, I would like to convert 0003 to 3 and 019 to 19. Obviously, my variable of interest is a string var.
    Thanks,
    Nader

  • #2
    there are a number of ways - if all values are really numbers, I would first -destring- the variable; if you really want it as a string variable, you can then -tostring- it; if there are also non-numeric values just make sure you destring into a new variable; if you want to then -tostring- and -replace-, that is also simple enough - if you provide a dataex example (see the FAQ), exact code can easily be provided; see
    Code:
    help destring

    Comment


    • #3
      Rich Goldstein gives good advice and I have no bias against destring and tostring. The manual tells you that I wrote them, which is generous but not outrageous.

      But if the problem is purely wanting to lose leading zeros and otherwise you have integer characters only, then direct use of functions suffices. Here is that made concrete.

      Code:
      . clear
      
      . set obs 9
      number of observations (_N) was 0, now 9
      
      . gen problem = _n * "0" + string(_n)
      
      . list
      
           +------------+
           |    problem |
           |------------|
        1. |         01 |
        2. |        002 |
        3. |       0003 |
        4. |      00004 |
        5. |     000005 |
           |------------|
        6. |    0000006 |
        7. |   00000007 |
        8. |  000000008 |
        9. | 0000000009 |
           +------------+
      
      . gen solution1 = string(real(problem))
      
      . gen solution2 = real(problem)
      
      . l
      
           +----------------------------------+
           |    problem   soluti~1   soluti~2 |
           |----------------------------------|
        1. |         01          1          1 |
        2. |        002          2          2 |
        3. |       0003          3          3 |
        4. |      00004          4          4 |
        5. |     000005          5          5 |
           |----------------------------------|
        6. |    0000006          6          6 |
        7. |   00000007          7          7 |
        8. |  000000008          8          8 |
        9. | 0000000009          9          9 |
           +----------------------------------+
      
      .

      Comment


      • #4
        If your string variable contains non-numeric characters, a different approach will be needed. I find the use of regular expressions convenient in this case; I always use the newer unicode functions because the regular expression syntax is more powerful for these than for the older functions.
        Code:
        * Example generated by -dataex-. To install: ssc install dataex
        clear
        input str13 problem
        "01A"          
        "002B"         
        "0003C"        
        "00004D"       
        "000005E"      
        "0000006F"     
        "010" 
        "10G"
        end
        generate solution1 = string(real(problem))
        generate solution2 = real(problem)
        generate solution3 = ustrregexrf(problem,"^0+","")
        list, clean abbreviate(12)
        Code:
        . list, clean abbreviate(12)
        
                problem   solution1   solution2   solution3  
          1.        01A           .           .          1A  
          2.       002B           .           .          2B  
          3.      0003C           .           .          3C  
          4.     00004D           .           .          4D  
          5.    000005E           .           .          5E  
          6.   0000006F           .           .          6F  
          7.        010          10          10          10  
          8.        10G           .           .         10G

        Comment


        • #5
          Thank you so much!

          Comment

          Working...
          X