Announcement

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

  • Deleting String data

    Hi,

    i am pretty new to stata and would now like to find out how to remove string data by generating a new variable.

    The strings (pnumber) look like this:
    224242424(A)
    2456474(B1)
    7843535377(C)
    24256809(U1)
    087378935(B1)
    etc..

    I want to create a new variable that deletes the Brackets completey, leading to "224242424", "2456474", etc...
    So far I am only able to delete one of these brackets at a time, using the command "gen pauth=subinstr(pnumber,"(B1)","",.)"

    How can I delete all expression in brackets (A,B1,C,U1) at once?

    Thanks in advance!

  • #2
    Code:
    clear
    input str15 s1
    "224242424(A)"
    "2456474(B1)"
    "7843535377(C)"
    "24256809(U1)"
    "087378935(B1)"
    end
    
    gen len=strpos(s1,"(")
    gen s2=s1      // in case there is no parenthesis in every observation
    replace s2=substr(s1,1,len-1) if len>0
    
    . list
         +----------------------------------+
         |            s1   len           s2 |
         |----------------------------------|
      1. |  224242424(A)    10    224242424 |
      2. |   2456474(B1)     8      2456474 |
      3. | 7843535377(C)    11   7843535377 |
      4. |  24256809(U1)     9     24256809 |
      5. | 087378935(B1)    10    087378935 |
         +----------------------------------+

    Comment


    • #3
      Thank you, worked perfectly!

      Comment

      Working...
      X