Announcement

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

  • Substring matching function?

    Is there a function that can look for a string within a larger string variable, and generate a 1 if it finds a match and 0 if it does not?

    Thanks

    Paul

  • #2
    A good thing to try in Stata is to search for help using some of your keywords. It can be hard to know the right search terms, and it doesn't always lead to something useful, but in this case it does:
    Code:
    search string function
    yields a listing of topics, of which the fourth one from the top is termed "string functions."

    Among those string functions, the easiest one to use in your case is strpos(), as in:
    Code:
    gen found = strpos("Is there something here", "there") > 0

    Comment


    • #3
      In addition to Mike's advice, you should also be aware of the Unicode-aware version of the string functions. As well, it may be of use to to consider -strmatch()- or the regex family to look for substrings. The former may be easiest to write, and looks like this.

      Code:
      gen byte found = strmatch("Is there something here", "*there*")

      Comment

      Working...
      X