I have a dataset with group ID, individual ID, age, and sex of the individuals, as well as their relationship. Relationship may take several values but can only be the value "1" once in a group. This is not met in my data. I have flagged my data with a variable "flag" that denotes if within a group this rule is not met.
Within the subset of the "flagged" I would want to turn all "1" values into missing except for that of :
1) the oldest male,
2) the oldest female if there are no males in the group,
3) the lowest inner group ID number if age so far not unique.
As an example I would like to turn this:
Group | ID | Age | Sex | Relation | Flag |
1 | 1 | 60 | M | 1 | 0 |
1 | 2 | 20 | F | 2 | 0 |
1 | 3 | 20 | M | 3 | 0 |
1 | 4 | 20 | F | 3 | 0 |
2 | 1 | 60 | M | 1 | 1 |
2 | 2 | 20 | F | 1 | 1 |
2 | 3 | 20 | M | 3 | 1 |
2 | 4 | 20 | F | 3 | 1 |
3 | 1 | 60 | F | 1 | 1 |
3 | 2 | 60 | F | 1 | 1 |
3 | 3 | 20 | M | 3 | 1 |
3 | 4 | 20 | F | 3 | 1 |
Into this:
Group | ID | Age | Sex | Relation | Flag |
1 | 1 | 60 | M | 1 | 0 |
1 | 2 | 20 | F | 2 | 0 |
1 | 3 | 20 | M | 3 | 0 |
1 | 4 | 20 | F | 3 | 0 |
2 | 1 | 60 | M | 1 | 0 |
2 | 2 | 20 | F | . | 0 |
2 | 3 | 20 | M | 3 | 0 |
2 | 4 | 20 | F | 3 | 0 |
3 | 1 | 60 | F | 1 | 0 |
3 | 2 | 60 | F | . | 0 |
3 | 3 | 20 | M | 3 | 0 |
3 | 4 | 20 | F | 3 | 0 |
I am new to Stata and don't quite know how to operate without resorting to several data frames. My initial thought was to split into flagged and non flagged data. Save each and operate on flagged cases, then append together, but I wondered if there is a more straightforward way.
Thanks and best regards,
Mario
0 Response to Change values within groups based on conditions
Post a Comment