Announcement

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

  • Count how many times a certain character appear in a string variable

    Hi,

    I have been looking for a way to count the occurrence of a character in a variable, but have only found this https://www.stata.com/statalist/arch.../msg01131.html that does really answer my question.

    I have a sting variable (Disease) with a certain amount of ",". I would need to create a variable (Comas) that counts the number of "," for each value. How would you go about this?

    Thanks.
    Best regards
    Antonio

  • #2
    Hi, is there several Disease per cell? Like Cell [2;2] "Malaria, Pneumonia, HIV" ?

    Comment


    • #3
      The number of commas is the length of the string MINUS the length of the string should the commas be removed.

      Code:
      gen ncommas = length(Disease) - length(subinstr(Disease, ",", "", .))
      Written up here:

      SJ-11-2 dm0056 . . . . . . . Stata tip 98: Counting substrings within strings
      . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . N. J. Cox
      Q2/11 SJ 11(2):318--320 (no commands)
      tip on counting substrings within strings

      http://www.stata-journal.com/sjpdf.h...iclenum=dm0056

      Comment


      • #4
        Dear Nick,
        Thanks. It makes sense!

        Comment


        • #5
          thanks Nick, great idea

          Comment

          Working...
          X