Stata新手求助:基于现有虚拟变量生成新偏好变量的语法问题
Alright, let's tackle this Stata variable creation problem together. First, I need to lay out some reasonable assumptions about your existing variables since you didn't specify their exact coding—just tweak these if they don't align with your data:
Let’s assume:
Sex_at_birth = 1= Assigned male at birth,Sex_at_birth = 0= Assigned female at birthisHE = 1= Heterosexual (异性恋),isBI = 1= Bisexual (双性恋),isHO = 1= Homosexual (同性恋)- These orientation variables are mutually exclusive (only one is 1 per observation; if not, you’ll want to clean that first to avoid conflicting logic)
Based on standard understandings of sexual orientation and attraction, here’s the logic for each new variable:
- likesMEN: Equals 1 if the person is attracted to men
- Assigned male + homosexual → attracted to men
- Assigned female + heterosexual → attracted to men
- Bisexual (regardless of assigned sex) → attracted to men
- likesWOMEN: Equals 1 if the person is attracted to women
- Assigned male + heterosexual → attracted to women
- Assigned female + homosexual → attracted to women
- Bisexual (regardless of assigned sex) → attracted to women
- likesBOTH: Equals 1 only if the person is bisexual (since bisexuality implies attraction to both genders)
You can use either explicit replace commands (great for readability) or concise conditional expressions. Here’s both options:
Option 1: Readable Step-by-Step Code
* Initialize all new variables to 0 first gen likesMEN = 0 gen likesWOMEN = 0 gen likesBOTH = 0 * Update likesMEN based on attraction logic replace likesMEN = 1 if (Sex_at_birth == 1 & isHO == 1) | (Sex_at_birth == 0 & isHE == 1) | (isBI == 1) * Set to missing if any key variable is missing replace likesMEN = . if missing(Sex_at_birth, isHE, isBI, isHO) * Update likesWOMEN based on attraction logic replace likesWOMEN = 1 if (Sex_at_birth == 1 & isHE == 1) | (Sex_at_birth == 0 & isHO == 1) | (isBI == 1) replace likesWOMEN = . if missing(Sex_at_birth, isHE, isBI, isHO) * Update likesBOTH (only bisexuals) replace likesBOTH = 1 if isBI == 1 replace likesBOTH = . if missing(isBI)
Option 2: Concise Conditional Expression Code
* Generate likesMEN directly with conditionals gen likesMEN = ( (Sex_at_birth == 1 & isHO == 1) | (Sex_at_birth == 0 & isHE == 1) | isBI == 1 ) replace likesMEN = . if missing(Sex_at_birth, isHE, isBI, isHO) * Generate likesWOMEN directly with conditionals gen likesWOMEN = ( (Sex_at_birth == 1 & isHE == 1) | (Sex_at_birth == 0 & isHO == 1) | isBI == 1 ) replace likesWOMEN = . if missing(Sex_at_birth, isHE, isBI, isHO) * Generate likesBOTH directly gen likesBOTH = (isBI == 1) replace likesBOTH = . if missing(isBI)
Pro Tip
If your variable coding is different (e.g., Sex_at_birth = 1 is female instead of male), just flip the corresponding conditions. For example, if Sex_at_birth = 1 = female, the likesMEN logic would become (Sex_at_birth == 1 & isHE == 1) | (Sex_at_birth == 0 & isHO == 1) | isBI == 1.
内容的提问来源于stack exchange,提问作者Mandarc

