Announcement

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

  • River Huang
    started a topic remove last letters in a word

    remove last letters in a word

    Dear All, The data set is
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str10 var
    "ACAC"
    "BAC" 
    "GAC" 
    end
    How can I remove the last AC from all words to obtain
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str10 var
    "AC"
    "B" 
    "G" 
    end
    Thanks for your suggestions.

  • River Huang
    replied
    Dear Leonardo, Thanks for this suggestion. I also learned (from others) another approach
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str10 var
    "ACAC"
    "BAC" 
    "GAC" 
    "NASD"
    end
    
    gen wanted=ustrregexra(var,"AC$","")

    Leave a comment:


  • Leonardo Guizzetti
    replied
    This is actually well suited for regular expressions. The pattern looks to see if "AC" is at the end of the string, and returns the part before the match. The string is left untouched if there is no match. Here I'm using a case-insensitive match, as determined by the last argument to -ustrregexm()-.

    Code:
    clonevar want = have
    replace want = ustrregexs(1) if ustrregexm(want, "(.*)(AC)$", .)
    Result:

    Code:
         +-------------+
         | have   want |
         |-------------|
      1. | ACAC     AC |
      2. |  BAC      B |
      3. |  GAC      G |
      4. | NASD   NASD |
         +-------------+

    Leave a comment:


  • River Huang
    replied
    Dear Ali, Thanks a lot. It helps.

    Leave a comment:


  • Ali Atia
    replied
    Code:
    replace var = substr(var,1,strlen(var)-2) if substr(var,strlen(var)-1,2) == "AC"

    Leave a comment:

Working...
X