Announcement

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

  • Dropping observations in a variable with a shared suffix

    Very simple question but the suggestions from google (and this forum) have failed to provide a solution.
    I have a variable 'xxx' and I want to delete observations ending in "P2" within that variable.
    I have tried: 1) drop if xxx=="*P2"
    2) drop if xxx=="_P2"
    3) drop if xxx=="*_P2"
    4) drop if xxx=="_*P2"
    But to no avail.
    Any suggestions where I am going wrong? Perhaps some clarification on the correct truncation/wildcard.
    Many thanks.

  • #2
    Code:
    drop if regexm(xxx, "P2$") == 1

    Comment


    • #3
      an alternative:
      Code:
      drop if substr(xxx,-2,.)=="P2"

      Comment


      • #4
        Rich's solution is fine but you know you want the last two characters, then you can also write in terms of

        Code:
        substr(xxx, -2, 2)

        Comment


        • #5
          Thank you Andrew Musau, Rich Goldstein and Nick Cox!

          Comment


          • #6
            Originally posted by Nick Cox View Post
            Rich's solution is fine but you know you want the last two characters, then you can also write in terms of

            Code:
            substr(xxx, -2, 2)
            What if I want to delete observations starting (not ending) with P2?

            Comment


            • #7
              Code:
               drop if substr(xxx,1,2)=="P2"

              Comment

              Working...
              X