Announcement

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

  • Replacing/removing punctuation in string variable?

    Hi all,

    I'm stuck on what I think is a very simple issue of replacing certain parts of a string with some other character. I have a bunch of punctuation like ",","/" etc and I'm trying to remove them or replace them. Please consider the following code:

    Code:
    foreach var of varlist A B C D E{
    replace `var' = subinstr(`var', ".", "",.)
    replace `var' = subinstr(`var', "-", "",.)
    replace `var' = subinstr(`var', ";", "",.)
    replace `var' = subinstr(`var', "(", "",.)
    replace `var' = subinstr(`var', ")", "",.)
    replace `var' = subinstr(`var', ",", "",.)
    replace `var' = subinstr(`var', ".", "",.)
    replace `var' = subinstr(`var', "/", " OR ",.)
    replace `var' = subinstr(`var', ">", "GREATER THAN ",.)
    replace `var' = subinstr(`var', "=>", "GREATER THAN OR EQUAL TO ",.)
    replace `var' = subinstr(`var', "K/", "KGs PER",.)
    }
    Rest of the loop works fine, but when it comes to
    Code:
    replace `var' = subinstr(`var', "=>", "GREATER THAN OR EQUAL TO ",.)
    the result becomes weird because of
    Code:
    replace `var' = subinstr(`var', ">", "GREATER THAN ",.)
    where "=" part of "=>" is worked by the latter code.

    Similar problem for
    Code:
    replace `var' = subinstr(`var', "/", " OR ",.)
    and
    Code:
    replace `var' = subinstr(`var', "K/", "KGs PER",.)
    where "/" part of "K/" is worked by former code.

    Any way to resolve this issue?

    Thanks,

  • #2
    Hereabouts ">=" is "greater than or equal to", but replace that with words before you try to replace ">". And work on "K/" before "/".

    Comment


    • #3
      Originally posted by Nick Cox View Post
      Hereabouts ">=" is "greater than or equal to", but replace that with words before you try to replace ">". And work on "K/" before "/".
      thanks this worked

      Comment

      Working...
      X