0
votes

I am not well versed in Stata, and I am struggling to find the right syntax to generate a new variable based on the properties of several other variables.

I have one dummy variable indicating sex (Sex_at_birth), and 3 dummy variables indicating sexual orientation (isHE, isBI, isHO). I want to create 3 dummy variables indicating sexual preferences (likesMEN, likesWOMEN, likesBOTH).

An example logical statement I'd like to use would be:

likesMEN = 1 if ((Sex_at_birth = 1) & (isHE = 1)) | ((Sex_at_birth = 0) & (isHO = 1)), 
else likesMEN =0

It would be analogous for the other variables.

I am at loss on how to code it in Stata, and I believe it will be like 2 seconds for someone who knows the program well.

Note: I know that I need n-1 number of dummy variables to code a variable that has 3 categories, and I will have that in mind when conducting analyses. However for my dataset I want to have all of the categories dummy coded (so I can chose which one to omit for my analyses).

1

1 Answers

2
votes

Please always provide a Minimal, Complete, and Verifiable example with your question.

You just need to use == instead of = in your expression:

clear
set obs 20

local varlist Sex_at_birth isHE isBI isHO

foreach var of local varlist {
    generate random = runiform()
    generate `var' = 0
    replace `var' = 1 in 1/10
    sort random
    drop random
}

generate likesMEN = ( (Sex_at_birth == 1) & (isHE == 1) ) | ///
                    ( (Sex_at_birth == 0) & (isHO == 1) )

You then get:

. list

    +------------------------------------------+
    | Sex_at~h   isHE   isBI   isHO   likesMEN |
    |------------------------------------------|
 1. |        0      1      1      0          0 |
 2. |        0      0      0      1          1 |
 3. |        0      1      0      1          1 |
 4. |        1      0      0      1          0 |
 5. |        0      1      1      1          1 |
    |------------------------------------------|
 6. |        1      1      1      1          1 |
 7. |        1      0      1      0          0 |
 8. |        1      0      1      0          0 |
 9. |        1      0      0      1          0 |
10. |        0      1      1      0          0 |
    |------------------------------------------|
11. |        1      0      0      0          0 |
12. |        0      1      1      0          0 |
13. |        0      0      0      1          1 |
14. |        0      1      0      1          1 |
15. |        1      0      0      0          0 |
    |------------------------------------------|
16. |        1      1      0      1          1 |
17. |        1      1      1      0          1 |
18. |        1      1      1      0          1 |
19. |        0      0      0      0          0 |
20. |        0      0      1      1          1 |
    +------------------------------------------+