Announcement

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

  • How to count the values of a set of binary variables

    Hi there,

    I have a set of binary variables (n=17) for health conditions and each of them stands for a certain disease. For instance, if variable he_diabetes = 1, it means that the respondent has diabetes. If variable he_heart = 0, it means that the respondent doesn't have heart disease. It is possible that some respondents have none or more health conditions.

    All these variables are included in a big database, which means it is possible that there are also other variables names begin with 'he_' but I only need to calculate with 17 health condition variables.

    I wonder:
    if I want to count for each respondent how many health conditions they have, is there a easier way to put the counts into one variable ? Or is it better to separate them from the dataset ?

    I tried to generate a new variable to count the number of health conditions for each respondent but found it could be laborious
    Code:
    gen count =.
    replace count = 0 if he_diabetes =  0 | he_heart = 0 | he_asthma = 0 | he_leg = 0
    replace count = 1 if he_diabetes = 1 & he_heart = 0 & he_asthma =0 & he_leg = 0 | he_diabetes = 0 & he_heart = 1 & he_asthma =0 & he_leg = 0 |
    ......
    Thanks in advance

  • #2
    Code:
    egen count = anycount(varlist), values(1)
    Or you could simply add up all of the variables. Since they're binary, they will sum to the amount of 1 values:

    Code:
    egen count = rowtotal(varlist)

    Comment


    • #3
      Originally posted by Ali Atia View Post
      Code:
      egen count = anycount(varlist), values(1)
      Or you could simply add up all of the variables. Since they're binary, they will sum to the amount of 1 values:

      Code:
      egen count = rowtotal(varlist)
      Thanks Ali! It works perfectly!

      Comment

      Working...
      X