Announcement

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

  • How to retrieve a part of a string, before a specific symbol?

    Does anyone know how to retrieve a part of a string, before a specific symbol?

    For example, in the data below,
    Code:
     
    Name
    thisisatesSGA123
    madSGA1
    IKNOWHssswSGA
    I would like to get the part before the SGA, and remove the rest:
    Code:
     
    Name
    thisisates
    mad
    IKNOWHsssw
    Can anyone help me?

  • #2
    Code:
    clear
    input str42 Name
    thisisatesSGA123
    madSGA1
    IKNOWHssswSGA
    end
    
    gen Wanted = substr(Name, 1, strpos(Name, "SGA") - 1)
    
    list
    
        +-------------------------------+
         |             Name       Wanted |
         |-------------------------------|
      1. | thisisatesSGA123   thisisates |
      2. |          madSGA1          mad |
      3. |    IKNOWHssswSGA   IKNOWHsssw |
         +-------------------------------+
    Please use dataex for data examples (as requested of you, e.g., in https://www.statalist.org/forums/for...ch-observation)
    Last edited by Nick Cox; 16 Jul 2018, 03:32.

    Comment


    • #3
      Thanks Nick, the -strops- command rocks! And sorry for not using -dataex-, I'll use them next time. Thanks for the kind reminder

      Comment


      • #4
        Thanks for the thanks, but strpos() is a function, not a command. For more, see e.g. https://www.stata-journal.com/sjpdf....iclenum=dm0058

        Comment

        Working...
        X