Announcement

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

  • Combining similar variables

    Hey everyone. I am a rookie stata user, and unfortunately cant seem to find a solution to my problem online that i understand.

    I have 9 variables that all concern the gender of members of the respondants' household. So the first variable is the gender of the respondant, second variable of the second person in the household's gender, third about the third person, etc.

    I want to combine all the variables into 1 gender variable, so i can see the gender makeup of all the respondant's households combined.

    Is there an easy way to do this, since all the variables are completely similar? Hope it makes sense. Thanks for any advice.

  • #2
    Well, you haven't given a very clear specification of what you want this variable to tell you. Here are a couple of possibilities:

    Code:
    egen household_genders = group(gender1-gender9), miss
    will give you a variable whose values designate all possible 39 combinations of genders (including missing value) of the up to 9 gender variables. That means that M F F M (for the first four) would be treated as different from M F M F. It provides the most detailed possible combination.

    At the other extreme, maybe you just want to know how many males and how many females there are. If the data are coded 1 for female, 0 for male, and missing value for no such person in the household:

    Code:
    egen females = rowtotal(gender1-gender9)
    egen persons = rownonmiss(gender1-gender9)
    gen males = persons - females
    which actually gives you three variables. But if you wanted to make that into a single variable that looks like "3/2" for 3 males and 2 females, you could follow that with:

    Code:
    egen household_genders = concat(males females), punct("/")
    Anyway, as you can see, your question is not sufficiently defined to give you a concrete answer.

    Also, once you figure out exactly what you really want, the details of the code might well depend on details of the data. For example, all of the above codes assume that there are 9 gender variables, named gender1 through gender9, occurring as consecutive variables in the data set, and all of them coded 0/1. If your data are otherwise set up, none of these will work properly. So you will also need to show an example of your data to get working code. To do that, please use the -dataex- command to do so. If you are running version 15.1 or a fully updated version 14.2, it is already part of your official Stata installation. If not, run -ssc install dataex- to get it. Either way, run -help dataex- to read the simple instructions for using it. -dataex- will save you time; it is easier and quicker than typing out tables. It includes complete information about aspects of the data that are often critical to answering your question but cannot be seen from tabular displays or screenshots. It also makes it possible for those who want to help you to create a faithful representation of your example to try out their code, which in turn makes it more likely that their answer will actually work in your data.



    When asking for help with code, always show example data. When showing example data, always use -dataex-.

    Comment

    Working...
    X