Announcement

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

  • Combing Multiple Variables

    Hi, I am very new to Stata, so I apologize if this a basic question or has been answered elsewhere. I am working with survey data that included a "select all that apply" question with 4 different options. When the responses were imported, each option was generated as a separate variable. For example:
    What race are you (White) What race are you (Hispanic) What race are you (African American) What race are you(Asian)
    White . . .
    . . African American .
    . Hispanic . .
    White . . Asian
    Each row corresponds to an individual survey respondent. Is there a way to collapse all of these answers into one column, so it looks something like this:
    What race are you?
    White
    African American
    Hispanic
    White, Asian
    My goal is to run simple demographics on the survey responses based on the race/ethnicity of the participant. The answers are encoded as bytes.

    Thank you in advance.



  • #2
    If you have not yet imported your data into Stata, it is premature to ask for help with code. Since you say they are encoded as bytes, I presume you actually have a Stata data set already. In that case, it is not possible to help you unless you show an example of your data. At the very least, the bytes you refer to clearly do not resemble the tableau you have drawn, and your variable names must be different than you show so that they are legal in Stata. So use the -dataex- command to show us an actual representative example of your actual Stata data set.


    If you are running version 16 or a fully updated version 15.1 or 14.2, -dataex- 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-.
    Last edited by Clyde Schechter; 19 Apr 2020, 20:59.

    Comment


    • #3
      I apologize, thank you for clarifying. Here is a sample of my data using -dataex-

      Code:
      * Example generated by -dataex-. To install: ssc install dataex
      clear
      input byte(Q6_2 Q6_3 Q6_4)
      . 1 .
      . . 1
      . 1 1
      . . 1
      1 . 1
      end
      label values Q6_2 A_V
      label def A_V 1 "Hispanic, Latino, or Spanish origin", modify
      label values Q6_3 A_W
      label def A_W 1 "White", modify
      label values Q6_4 A_X
      label def A_X 1 "Asian", modify

      Comment


      • #4
        Thanks for your example. As it includes people who identify as Hispanic and Asian and as White and Asian, it is not clear to me that it can be combined as one variable in any way but to 8 possible categories:

        Code:
        gen race = (Q6_2 == 1) + 2 * (Q6_2 == 1) + 4 * (Q6_2 == 1) 
        label def race 0 "none given" 1 "Hispanic" 2 "White" 3 "Hispanic and White" 4 "Asian " 5 "Hispanic and Asian" 6 "White and Asian" 7 "Hispanic, White and Asian" 
        label val race race

        Comment


        • #5
          Sorry; typos in #4.

          Code:
           
           (Q6_2 == 1) + 2 * (Q6_3 == 1) + 4 * (Q6_4 == 1)

          Comment


          • #6
            Thank you! This code worked for me.

            Comment

            Working...
            X