Announcement

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

  • 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.
    Ho-Chuan (River) Huang
    Stata 17.0, MP(4)

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

    Comment


    • #3
      Dear Ali, Thanks a lot. It helps.

      Ho-Chuan (River) Huang
      Stata 17.0, MP(4)

      Comment


      • #4
        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 |
             +-------------+

        Comment


        • #5
          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$","")
          Ho-Chuan (River) Huang
          Stata 17.0, MP(4)

          Comment

          Working...
          X