Announcement

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

  • Remove leading "0"

    Dear all,

    I have observations entries that look like this:

    Click image for larger version

Name:	Annotation 2020-01-07 143548.png
Views:	1
Size:	5.4 KB
ID:	1531132


    I'd like to remove the leading "0"s from the school codes. I can't simply destring it because that are alphabets.

    I tried:

    Code:
     gen high_school_code1 = subinstr(high_school_code, "0", "", 1)
    but it removes 0 from "50018" also, so I ended up with "5018".

    Any help is very much appreciated!

    Best,
    Peter

  • #2
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str5 var1
    "A2000"
    "02555"
    "01010"
    "777"  
    end
    
    gen wanted=cond(!missing(real(var1)), string(real(var1)), var1)
    Res.:

    Code:
    . l
    
         +----------------+
         |  var1   wanted |
         |----------------|
      1. | A2000    A2000 |
      2. | 02555     2555 |
      3. | 01010     1010 |
      4. |   777      777 |
         +----------------+

    Comment


    • #3
      Andrew, this works great. thanks!

      In the meanwhile, I used the following command to create the same results.

      Code:
      gen high_school_code = subinstr(high_school_code, "0", "", 1) if substr(high_school_code, 1,1)=="0"

      Comment

      Working...
      X