Announcement

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

  • count the names in a variable

    Hi

    I have a variable that includes names with commas and I want to create a variable to represent the count of these names. Here is a dataex below

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str36 names
    "Tom"                       
    ""                                                     
    ""                             
    "John"                 
    "Fisher, Eddie, Tom"
    ""                             
    ""                                                       
    ""                             
    "Lacker"                       
    end
    ------------------ copy up to and including the previous line ------------------


  • #2
    You need two rules, it seems:

    the number of commas plus 1

    -- so long as the variable is not missing (empty)

    See https://www.stata-journal.com/articl...article=dm0056.

    Comment


    • #3
      Thanks Nick

      I tried the following code. It still needs to be modified to get the correct counts but I could not fix that.

      Code:
      gen comma = length(trim(names))-length(trim(subinstr(names,",","",.)))
      Code:
      * Example generated by -dataex-. To install: ssc install dataex
      clear
      input str36 names double comma
      "Tom"                        0
      ""                              0
      ""                              0
      ""                              0
      ""                              0
      "John"                  0
      "Fisher, Eddie, Tom" 2
      ""                              0
      ""                              0
      ""                              0
      ""                              0
      ""                              0
      "Lacker"                        0
      end

      Comment


      • #4
        NB in #2

        plus 1


        so long as the variable is not missing (empty)

        Comment


        • #5
          Thanks, but I tried that and it does not work because of the missing obs. so I am not sure how to condition for not missing within this command:

          Code:
          gen comma = length(trim(names))-length(trim(subinstr(names,",","",.)))+1

          Comment


          • #6
            Presumably you know about if qualifiers.

            With your code you could just follow up with


            Code:
            replace comma = 0 if trim(names) == ""

            Or you could get there directly with one line:


            Code:
              
             * Example generated by -dataex-. To install: ssc install dataex clear input str36 names double comma "Tom"                        0 ""                              0 ""                              0 ""                              0 ""                              0 "John"                  0 "Fisher, Eddie, Tom" 2 ""                              0 ""                              0 ""                              0 ""                              0 ""                              0 "Lacker"                        0 end 
            
            
            . gen wanted = cond(trim(names) == "", 0, 1 + (strlen(names) - strlen(subinstr(names, ",", "", .))))
            
            . sort wanted 
            
            . list, sepby(wanted)
            
                 +-------------------------------------+
                 |              names   comma   wanted |
                 |-------------------------------------|
              1. |                          0        0 |
              2. |                          0        0 |
              3. |                          0        0 |
              4. |                          0        0 |
              5. |                          0        0 |
              6. |                          0        0 |
              7. |                          0        0 |
              8. |                          0        0 |
              9. |                          0        0 |
                 |-------------------------------------|
             10. |                Tom       0        1 |
             11. |               John       0        1 |
             12. |             Lacker       0        1 |
                 |-------------------------------------|
             13. | Fisher, Eddie, Tom       2        3 |
                 +-------------------------------------+

            Comment

            Working...
            X