Announcement

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

  • Combine 10 variables

    Hi everyone!

    I am an undergraduate student who is relatively new to stata. I am stuck processing my data for my dissertation and would really appreciate your help!

    In my dataset there are a range of variables where the participants of the survey were asked about their qualification. W8QUALB0A, for example, decribes whether the individuals have a higher degree and postgraduate qualifications, to which 98 out of 7707 answered "yes", and it correponds to "1". W8QUALB0B asks whether people have a first degree, to which 1218 people answered "yes", which also corresponds to "1". The variables continues from W8QUALB0A all the way until W8QUALB0K, which is 11 variables in total.

    I want to combine the "yes" answeres into a new variable called highest qualification, where postgraduate qualification = 11 and has 98 observations, and first degree = 10 and has 1218 observations and so on and so fourth.

    How would I do this?

    I have read up on stack command but not sure if it is what I want to do.

    Many thanks!
    Lynn

  • #2
    In a simple form, it's like this:

    Code:
    generate highest_ed = .
    replace highest_ed = 11 if W8QUALB0A == 1
    replace highest_ed = 10 if W8QUALB0B == 1
    * So on so forth...
    replace highest_ed = 1 if W8QUALB0K == 1
    These 11 lines can also be shortened into a loop, like this. Whichever you're more comfortable.

    Code:
    gen highest_ed = .
    local counter = 11
    foreach x in A B C D E F G H I J K{
        replace highest_ed = `counter' if W8QUALB0`x' == 1
        local counter = `counter' - 1
    }
    Results:
    Code:
         +-----------------------------------------------------------------------------------------------------------------------------------+
         | W8QUA~0A   W8QUA~0B   W8QUA~0C   W8QUA~0D   W8QUA~0E   W8QUA~0F   W8QUA~0G   W8QUA~0H   W8QUA~0I   W8QUA~0J   W8QUA~0K   highes~d |
         |-----------------------------------------------------------------------------------------------------------------------------------|
      1. |        1          0          0          0          0          0          0          0          0          0          0         11 |
      2. |        0          1          0          0          0          0          0          0          0          0          0         10 |
      3. |        0          0          1          0          0          0          0          0          0          0          0          9 |
      4. |        0          0          0          1          0          0          0          0          0          0          0          8 |
      5. |        0          0          0          0          1          0          0          0          0          0          0          7 |
      6. |        0          0          0          0          0          1          0          0          0          0          0          6 |
      7. |        0          0          0          0          0          0          1          0          0          0          0          5 |
      8. |        0          0          0          0          0          0          0          1          0          0          0          4 |
      9. |        0          0          0          0          0          0          0          0          1          0          0          3 |
     10. |        0          0          0          0          0          0          0          0          0          1          0          2 |
     11. |        0          0          0          0          0          0          0          0          0          0          1          1 |
         +-----------------------------------------------------------------------------------------------------------------------------------+
    A word of warning, please double check how the answers are structured. Having post-graduate requires having first degree... so, it is possible that the person may have "1" in BOTH W8QUALB0A and W8QUALB0B. If that is the case, your recoded values will be very off. In addition, if this question is from a "check all that applies" multiple choice, I think a better approach would be to code from "K" back to "A", so that you are indeed capturing the highest, and not the lowest.

    In future, posting example data will be very helpful to users here. Please take some time to read up on Part 12 of the FAQ here at: https://www.statalist.org/forums/help
    Last edited by Ken Chui; 04 Jan 2023, 09:00.

    Comment


    • #3
      Many thanks, Ken, that is very helpful! I have successfully coded as you have outlined, will double check whether these answeres are mutually exclusive.

      Comment

      Working...
      X