Announcement

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

  • removing numbers from string

    Hi, I want to create three separate variables with the numbers between the letters. For instance from c101r10r145 I want to create three variables namely 'district' with value=145 , state=10 and country=101, how do I separate them?


    Code:
    ----------------------- copy starting from the next line -----------------------
    
    
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str12 ID_adm2
    "c100r0r0"   
    "c101r10r144"
    "c101r10r145"
    "c101r10r146"
    "c101r10r147"
    "c101r10r148"
    "c101r10r149"
    "c101r10r150"
    "c101r10r151"
    "c101r10r152"
    "c101r10r153"
    "c101r10r154"
    "c101r10r155"
    "c101r10r156"
    "c101r10r157"
    "c101r10r158"
    "c101r10r159"
    "c101r10r160"
    "c101r11r161"
    "c101r11r162"
    "c101r11r163"
    "c101r11r164"
    "c101r12r165"
    "c101r12r166"
    "c101r12r167"
    "c101r12r168"
    "c101r12r169"
    "c101r12r170"
    "c101r12r171"
    "c101r12r172"
    "c101r12r173"
    "c101r12r174"
    "c101r12r175"
    "c101r12r176"
    "c101r12r177"
    "c101r12r178"
    "c101r12r179"
    "c101r12r180"
    "c101r12r181"
    "c101r12r182"
    "c101r12r183"
    "c101r13r184"
    "c101r13r185"
    "c101r13r186"
    "c101r13r187"
    "c101r13r188"
    "c101r13r189"
    "c101r13r190"
    "c101r13r191"
    "c101r13r192"
    "c101r13r193"
    "c101r13r194"
    "c101r13r195"
    "c101r13r196"
    "c101r13r197"
    "c101r13r198"
    "c101r13r199"
    "c101r13r200"
    "c101r13r201"
    "c101r13r202"
    "c101r13r203"
    "c101r13r204"
    "c101r13r205"
    "c101r13r206"
    "c101r13r207"
    "c101r13r208"
    "c101r13r209"
    "c101r13r210"
    "c101r13r211"
    "c101r14r212"
    "c101r14r213"
    "c101r14r214"
    "c101r14r215"
    "c101r14r216"
    "c101r14r217"
    "c101r14r218"
    "c101r14r219"
    "c101r14r220"
    "c101r14r221"
    "c101r14r222"
    "c101r14r223"
    "c101r14r224"
    "c101r14r225"
    "c101r14r226"
    "c101r14r227"
    "c101r15r228"
    "c101r15r229"
    "c101r15r230"
    "c101r15r231"
    "c101r15r232"
    "c101r15r233"
    "c101r15r234"
    "c101r15r235"
    "c101r15r236"
    "c101r15r237"
    "c101r15r238"
    "c101r15r239"
    "c101r15r240"
    "c101r15r241"
    "c101r15r242"
    end
    --------

  • #2
    With just a basic understanding of regular expressions, you can accomplish a great deal.

    Code:
    replace ID_adm2= ustrregexra(ID_adm2, "[\d]", "")

    Comment


    • #3
      I'm responding to your title in #2.

      For instance from c101r10r145 I want to create three variables namely 'district' with value=145 , state=10 and country=101, how do I separate them?
      Code:
      forval i=1/3{
          gen wanted`i'=ustrregexra(ID_adm2, "c(.*)r(.*)r(.*)", "$`i'")
      }

      Comment


      • #4
        Or possibly

        Code:
        gen x=substr(ID_adm2,2,3)
        gen y=substr(ID_adm2,6,2)
        gen z=substr(ID_adm2,9,3)
        if the all the values are as regular as they appear in the sample. See https://www.stata.com/manuals/m-5substr.pdf for -substr-.

        Comment


        • #5
          Also

          Code:
          . split ID_adm2, parse(r c) destring 
          variables born as string: 
          ID_adm21  ID_adm22  ID_adm23  ID_adm24
          ID_adm21: all characters numeric; replaced as byte
          (100 missing values generated)
          ID_adm22: all characters numeric; replaced as int
          ID_adm23: all characters numeric; replaced as byte
          ID_adm24: all characters numeric; replaced as int
          
          . 
          . su ID_adm2? 
          
              Variable |        Obs        Mean    Std. dev.       Min        Max
          -------------+---------------------------------------------------------
              ID_adm21 |          0
              ID_adm22 |        100      100.99          .1        100        101
              ID_adm23 |        100       12.55     2.04186          0         15
              ID_adm24 |        100      191.07    34.48415          0        242

          Comment

          Working...
          X