Announcement

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

  • problem with substr

    I am trying to match two different data sets via the variable name. The problem that occurs is that some of the names in data set 1 end with a letter "s", e.g. SMEDSTORPS or TOSTERUPS while in data set 2 the S at the end is missing.
    Data set 1:
    Code:
    name
    SMEDSTORPS
    STIBY
    TOSTERUPS
    Data set 2:
    Code:
    name
    SMEDSTORP
    STIBY
    TOSTERUP
    When I use the following
    Code:
    replace name = cond(substr(name,-1,.)=="S",subinstr(name,"S","",.),name)
    not only the S at the end is removed but also all other S in the name.
    Code:
    name
    MEDTORP
    STIBY
    TOTERUP
    Does someone have an idea how to fix this problem?
    Thanks in advance!

  • #2
    This is an example where fussiness over fonts -- as shown in the Stata documentation -- helps. You say

    I am trying to match two different data sets via the variable name

    where you mean, as a second reading makes clear,

    I am trying to match two different data sets via the variable name

    or

    I am trying to match two different data sets using a variable called name

    Any way, I suggest

    Code:
     
     replace name = substr(name,1,length(name)-1)if substr(name,-1,1)=="S"


    Comment


    • #3
      Thanks a lot, it works!!

      Yes and you were right
      I am trying to match two different data sets using a variable called name

      Comment

      Working...
      X