Announcement

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

  • How to count observations if cell contains string (but not a specific string)

    Hey,

    I am working on a household-level panel data set, and we asked the name of each household member (string) and then the final household count (numeric). I want to create a new variable that counts the names of the household members and compare it to our household count variable. I want to check whether cells contain names so that I can kick out observations that have a mismatch between the reported household size and those that did not provide us with all the names of the household members. We counted up to 11 household members.

    I could only find the regexm function, but that requires that I specify the string variable (e.g. 'a' in name) but I only care that the cell is not empty. It's really just like Excel's CountA(X1:X10) function.

    Thanks so much for your help.

    Best, Claudia
    HH_member_1 HH_member_2 HH_member_3 … HH_member_11 HH_count HH_count_check
    Maria Jon Liz 3 Count of (Maria+Jon+Liz)

  • #2
    look at the egen functions for rowmiss and rownonmiss; see "help egen"

    Comment


    • #3
      Sorry, no idea what that Excel function does, but it sounds as if you want to count non-empty strings in a loop over variables. That is a standard egen function


      Code:
      * Example generated by -dataex-. To install: ssc install dataex
      clear
      input str4 var1 str6 var2 str7 var3
      "frog" "dragon" "phoenix"
      "toad" ""       ""      
      "newt" ""       ""      
      end
      
      egen present = rownonmiss(var*), strok
      
      l
      
           +------------------------------------+
           | var1     var2      var3    present |
           |------------------------------------|
        1. | frog   dragon   phoenix          3 |
        2. | toad                             1 |
        3. | newt                             1 |
           +------------------------------------+
      But watch out: a string with just some stray spaces counts as not missing. If that's a problem, there are easy solutions.
      Last edited by Nick Cox; 24 Mar 2016, 13:43.

      Comment


      • #4
        Dear Nick and Richard!


        Thanks so much for your help! It worked!

        Best, Claudia

        Comment

        Working...
        X