Announcement

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

  • Extract the last word from String variable

    Dear all

    How can I extract the last word from the variable EntrySector.

    For example

    "Bangkok - Paro"
    "Phuentsholing"
    "Phuentsholing"
    "Kolkata - Paro"

    I want to extract Paro from "Bangkok - Paro" , "Kolkata - Paro" and if its only one word ""Phuentsholing"" extract that word.

    An example of my data set

    ----------------------- copy starting from the next line -----------------------
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str28 EntrySector
    "Bangkok - Paro"  
    "Bangkok - Paro"  
    "Bangkok - Paro"  
    "Bangkok - Paro"  
    "Bangkok - Paro"  
    "Phuentsholing"   
    "Phuentsholing"   
    "Kolkata - Paro"  
    "Kolkata - Paro"  
    "Delhi - Paro"    
    "Bangkok - Paro"  
    "Kathmandu - Paro"
    "Kathmandu - Paro"
    "Phuentsholing"   
    "Samdrup Jongkhar"
    "Dhaka - Paro"    
    "Singapore - Paro"
    "Kathmandu - Paro"
    "Bangkok - Paro"  
    "Kathmandu - Paro"
    "Kathmandu - Paro"
    "Samdrup Jongkhar"
    "Hanoi - Paro"    
    "Delhi - Paro"    
    "Delhi - Paro"    
    "Bangkok - Paro"  
    "Delhi - Paro"    
    "Kathmandu - Paro"
    "Kathmandu - Paro"
    "Kathmandu - Paro"
    "Hanoi - Paro"    
    "Kathmandu - Paro"
    "Phuket - Paro"   
    "Delhi - Paro"    
    "Kathmandu - Paro"
    "Kolkata - Paro"  
    "Phuentsholing"   
    "Bangkok - Paro"  
    "Bangkok - Paro"  
    "Bangkok - Paro"  
    "Delhi - Paro"    
    "Kathmandu - Paro"
    "Kathmandu - Paro"
    "Delhi - Paro"    
    "Phuentsholing"   
    "Bangkok - Paro"  
    "Delhi - Paro"    
    "Bangkok - Paro"  
    "Kathmandu - Paro"
    "Kathmandu - Paro"
    "Bangkok - Paro"  
    "Bangkok - Paro"  
    "Bangkok - Paro"  
    "Samdrup Jongkhar"
    "Bangkok - Paro"  
    "Kolkata - Paro"  
    "Bangkok - Paro"  
    "Delhi - Paro"    
    "Kathmandu - Paro"
    "Kathmandu - Paro"
    "Kathmandu - Paro"
    "Singapore - Paro"
    "Kathmandu - Paro"
    "Kathmandu - Paro"
    "Bangkok - Paro"  
    "Kathmandu - Paro"
    "Kathmandu - Paro"
    "Samdrup Jongkhar"
    "Hongkong - Paro" 
    "Delhi - Paro"    
    "Phuentsholing"   
    "Kathmandu - Paro"
    "Kathmandu - Paro"
    "Kolkata - Paro"  
    "Kathmandu - Paro"
    "Samdrup Jongkhar"
    "Bangkok - Paro"  
    "Kathmandu - Paro"
    "Kathmandu - Paro"
    "Kathmandu - Paro"
    "Bangkok - Paro"  
    "Delhi - Paro"    
    "Kathmandu - Paro"
    "Bangkok - Paro"  
    "Delhi - Paro"    
    "Delhi - Paro"    
    "Kathmandu - Paro"
    "Kathmandu - Paro"
    "Singapore - Paro"
    "Kolkata - Paro"  
    "Kathmandu - Paro"
    "Kathmandu - Paro"
    "Kathmandu - Paro"
    "Bangkok - Paro"  
    "Kathmandu - Paro"
    "Bangkok - Paro"  
    "Kathmandu - Paro"
    "Bangkok - Paro"  
    "Kathmandu - Paro"
    "Bangkok - Paro"  
    end
    ------------------ copy up to and including the previous line ------------------

  • #2
    Anna:
    you may want to try:
    Code:
    . split EntrySector , p(-)
    variables created as string:
    EntrySector1  EntrySector2
    
    . list in 1/10
    
         +-------------------------------------------+
         |    EntrySector    EntrySector1   EntryS~2 |
         |-------------------------------------------|
      1. | Bangkok - Paro        Bangkok        Paro |
      2. | Bangkok - Paro        Bangkok        Paro |
      3. | Bangkok - Paro        Bangkok        Paro |
      4. | Bangkok - Paro        Bangkok        Paro |
      5. | Bangkok - Paro        Bangkok        Paro |
         |-------------------------------------------|
      6. |  Phuentsholing   Phuentsholing            |
      7. |  Phuentsholing   Phuentsholing            |
      8. | Kolkata - Paro        Kolkata        Paro |
      9. | Kolkata - Paro        Kolkata        Paro |
     10. |   Delhi - Paro          Delhi        Paro |
         +-------------------------------------------+
    
    .
    Kind regards,
    Carlo
    (StataNow 19.0)

    Comment


    • #3
      Code:
      * Example generated by -dataex-. To install: ssc install dataex
      clear
      input str28 EntrySector
      "Bangkok - Paro"  
      "Phuentsholing"   
      end 
      
      local es EntrySector 
      gen wanted = cond(strpos(`es', "-") == 0, `es', trim(substr(`es', strpos(`es', "-") + 1, .)))
      
      list
      
           +--------------------------------+
           |    EntrySector          wanted |
           |--------------------------------|
        1. | Bangkok - Paro            Paro |
        2. |  Phuentsholing   Phuentsholing |
           +--------------------------------+
      See https://www.stata-journal.com/articl...article=dm0058 Section 5.1 on the best string quartet outside Vienna.

      Comment


      • #4
        Nick:
        "...the best string quartet outside Vienna..." actually took out the best of your sense of humour!
        Kind regards,
        Carlo
        (StataNow 19.0)

        Comment

        Working...
        X