Announcement

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

  • strpos() for many cases in values of string variables

    I am cleaning survey data where I want to code a string variable (x) to a numerical variable (y). For example, I want to replace y == 1 if the x variable contains certain strings like: "fgh", "f g h", "pdr" etc. Instead of using the pipe "|", in the code below, I was wondering if I could code this more efficiently?

    Code:
    replace y = 1 if strpos(lower(x), "fgh") > 0 | strpos(lower(x), "f g h") >0 | strpos(lower(x), "pdr") > 0
    Many thanks,
    Mihir

  • #2
    > 0 is dispensable as non-zero counts as true regardless.

    Code:
     
     replace y = 1 if strpos(lower(x), "fgh")| strpos(lower(x), "f g h") | strpos(lower(x), "pdr")

    Comment

    Working...
    X