Announcement

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

  • Problem with egen rownonmiss, strok

    Hello,

    I am trying to create a variable that will count how many nonmissing responses across several string variables exist per observation. I'm using Stata 15.1 on a Mac.

    This is what my code looks like:

    Code:
    egen activ_count = rownonmiss(activ_ac-activ_other), strok

    This is what I am trying to get it to do:

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str41 activ_ac str58 activ_soc str22 activ_other float activ_count
    " "        " "      " "     0
    " "        " "      " "     0
    " "        " "      " "     0
    " "        " "      " "     0
    "Academic" " "      " "     1
    "Academic" " "      " "     1
    "Academic" "Social" "Other" 3
    " "        " "      " "     0
    "Academic" " "      " "     1
    " "        " "      " "     0
    end

    But this is what the code is actually doing:

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str41 activ_ac str58 activ_soc str22 activ_other float activ_count
    " "        " "      " "     3
    " "        " "      " "     3
    " "        " "      " "     3
    " "        " "      " "     3
    "Academic" " "      " "     3
    "Academic" " "      " "     3
    "Academic" "Social" "Other" 3
    " "        " "      " "     3
    "Academic" " "      " "     3
    " "        " "      " "     3
    end

    I saw a similar post that illustrates that this code should work. I can't figure out what I did differently to make it not work. What is the error that I am making?

    Thank you!
    Sheilagh

  • #2
    The code is fine. But the data is not. " " is not a missing value. It is a string containing a blank. You have to fix the data:
    Code:
    foreach v of varlist activ_* {
        replace `v' = "" if `v' == " "
    }
    Then your code will do what you want.

    Comment


    • #3
      The reason it's not working is because the 'empty' strings are not actually empty, they're spaces. Instead of "", which is counted as containing missing values by rownonmiss with the strok option, the 'empty' values are " ", which is not counted as containing missing values. The solution is to replace the variables with "" if they contain " ", like so:

      Code:
      replace activ_ac = "" if activ_ac == " "
      edit: posted without refreshing to see @Clyde Schechter's answer. His loop is more efficient than doing it one at a time.

      Comment


      • #4
        It's working! That was definitely the problem. Thank you both for your help, Clyde Schechter and Ali Atia!

        Comment

        Working...
        X