Announcement

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

  • subinstr brainfart

    I have a string variable that needs a digit added.

    The first six values shown below are what this variable "should" look like, but for some reason, about 200 records that should end with a "1" had the 1 removed.

    I have attempted this:
    Code:
    replace aparticipant_id = subinstr(aparticipant_id, "*-0", "*-01", 1)
    and this (assuming maybe that the "1" was replaced with an extra space " " at the end? Should I use trim?):
    Code:
    replace aparticipant_id = subinstr(aparticipant_id, "*-0 ", "*-01", 1)
    and this:
    Code:
    gen tempx = subinstr(aparticipant_id, "*-0 ", "*-01", 1)
    replace aparticipant_id = tempx if aparticipant_id != tempx
    And neither seemed to work. Can someone kindly explain this to me like I'm a little kid? Crazymaking, lol.

    Here's a listing of the variable. The first six records are the correct format, the remaining records are a subset of those missing the final "1"
    Code:
    aparticipant_id
    SLE-KEN-GOU-001-01
    SLE-KEN-GOU-001-02
    SLE-KEN-GOU-002-01
    SLE-KEN-GOU-002-02
    SLE-KEN-GOU-002-03
    SLE-KEN-GOU-002-04
    SLE-KEN-HAN-001-0
    SLE-KEN-HAN-002-0
    SLE-KEN-HAN-003-0
    SLE-KEN-HAN-004-0
    SLE-KEN-HAN-005-0
    SLE-KEN-HAN-006-0
    SLE-KEN-KOI-001-0
    SLE-KEN-KOI-002-0
    SLE-KEN-KOI-003-0
    SLE-KEN-KOI-004-0
    SLE-KEN-KOI-005-0
    SLE-KEN-KOI-006-0
    SLE-KEN-KOI-007-0
    SLE-KEN-KOI-008-0
    SLE-KEN-KOI-009-0
    SLE-KEN-KOI-010-0
    SLE-KEN-KOI-011-0
    SLE-KEN-KOI-012-0
    SLE-KEN-KOI-013-0
    SLE-KEN-KOI-014-0
    SLE-KEN-KOI-015-0
    SLE-KEN-KOI-016-0
    SLE-KEN-KOI-017-0
    SLE-KEN-KOL-001-0
    SLE-KEN-KOL-002-0
    SLE-KEN-KOL-003-0
    SLE-KEN-KOL-004-0
    SLE-KEN-KOL-005-0
    SLE-KEN-KOL-006-0
    SLE-KEN-KOL-007-0
    SLE-KEN-KOL-008-0
    SLE-KEN-KOL-009-0
    SLE-KEN-KOL-010-0
    SLE-KEN-KOL-011-0
    SLE-KEN-KOL-012-0
    SLE-KEN-KOL-013-0
    SLE-KEN-KOL-014-0
    SLE-KEN-KOL-015-0
    SLE-KEN-KOL-016-0
    SLE-KEN-KOL-017-0
    SLE-KEN-KOL-018-0
    SLE-KEN-KOL-019-0
    SLE-KEN-KOL-020-0

  • #2
    Try something like
    Code:
    replace aparticipant_id = aparticipant_id + "1" if strlen(aparticipant_id) == strlen("SLE-KEN-KOL-020-0")

    Comment


    • #3
      By gosh, that worked.

      But what is wrong with my subinstr code above? I swear what I wrote was what was supposed to happen with your code, Joseph.

      Either way, thanks very much for helping me with this!

      Comment


      • #4
        The three code attempts you show in #1 all fail because -subinstr()- does not have the ability to interpret wildcards or regular expressions. The second and third arguments of -subinstr()- can only be interpreted as literal strings.

        You probably could have done what you wanted using the -ustrregexra()- function, which works with regular expressions. But -subinstr()- simply takes strings literally.

        Comment


        • #5
          Originally posted by Clyde Schechter View Post
          The three code attempts you show in #1 all fail because -subinstr()- does not have the ability to interpret wildcards or regular expressions. The second and third arguments of -subinstr()- can only be interpreted as literal strings.

          You probably could have done what you wanted using the -ustrregexra()- function, which works with regular expressions. But -subinstr()- simply takes strings literally.
          Aha, I did not see that. That makes a lot of sense. Thank you Clyde!

          Comment

          Working...
          X