Announcement

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

  • Changing last character in string variable

    I have a string variable, which varies in length from 3 to 4 characters.
    For each case that the last character of this variable variable is 0, I would like to change so that it ends in 9.
    Any help with this would be fantastic, thank you.

  • #2
    Here are two ways to do this:

    Code:
    replace strvar =substr(strvar, 1, length(strvar)-1) + "9"
    Code:
    replace strvar = regexr(strvar, "[0-9]$", "9")

    where you replace "strvar" with the name of your string variable.
    Last edited by Andrew Musau; 14 May 2020, 23:28.

    Comment


    • #3
      Thanks Andrew.
      Do you know how I could change the last character to 9 only when it ends in 0? (not change every last character to 9?)

      Comment


      • #4
        Code:
        replace strvar = regexr(strvar, "[0]$", "9")

        Comment


        • #5
          Yes! Thanks very much Andrew

          Comment

          Working...
          X