Suppose I have this data set:
The first three variables uniquely identify a household (hhid) in a given country and in a given year. The hhinfo identifies the family member inside the household. The age variable presentes the age of each respondent and the childc variable counts the number of children within each household. In household 111 there's 1 child, in household 222 there are no children, and in the third household 2 children live there.
What I'd like is for each mother and father row to have the age of each child. For example, in our toy example above there's a maximum of 2 children within all households. Then I'd like to create two variables, one for the first child and another for the second (possible) child. The first child's variable should have the first child's age repeated within the household, the second child's variable should have the second child's age repeated within the household. Naturally, if a family only has one child, the second variable should be missing, and if the family doesn't have any children both variables should be missing. The desired output should be like this:
I believe some of this can be obtained fairly easily with egen. However, I'm not sure how to integrate it in a for loop so that in each new variable creation the name includes the number of the child.
I feel that I'm complicating myself too much and that there's probably an easy solution for this. Any help is appreciated.
Thanks a lot.
| year | country | hhid | hhinfo | age | childc |
| 2010 | AT | 111 | Mother | 56 | 1 |
| 2010 | AT | 111 | Father | 54 | 1 |
| 2010 | AT | 111 | Son | 17 | 1 |
| 2010 | GER | 222 | Spouse | 32 | 0 |
| 2010 | GER | 222 | Husband | 32 | 0 |
| 2011 | PRT | 333 | Mother | 54 | 2 |
| 2011 | PRT | 333 | Father | 55 | 2 |
| 2011 | PRT | 333 | Son | 12 | 2 |
| 2011 | PRT | 333 | Daughter | 13 | 2 |
What I'd like is for each mother and father row to have the age of each child. For example, in our toy example above there's a maximum of 2 children within all households. Then I'd like to create two variables, one for the first child and another for the second (possible) child. The first child's variable should have the first child's age repeated within the household, the second child's variable should have the second child's age repeated within the household. Naturally, if a family only has one child, the second variable should be missing, and if the family doesn't have any children both variables should be missing. The desired output should be like this:
| year | country | hhid | hhinfo | age | childc | child_1 | child_2 |
| 2010 | AT | 111 | Mother | 56 | 1 | 17 | . |
| 2010 | AT | 111 | Father | 54 | 1 | 17 | . |
| 2010 | AT | 111 | Son | 17 | 1 | 17 | . |
| 2010 | GER | 222 | Spouse | 32 | 0 | . | . |
| 2010 | GER | 222 | Husband | 32 | 0 | . | . |
| 2011 | PRT | 333 | Mother | 54 | 2 | 12 | 13 |
| 2011 | PRT | 333 | Father | 55 | 2 | 12 | 13 |
| 2011 | PRT | 333 | Son | 12 | 2 | 12 | 13 |
| 2011 | PRT | 333 | Daughter | 13 | 2 | 12 | 13 |
I feel that I'm complicating myself too much and that there's probably an easy solution for this. Any help is appreciated.
Thanks a lot.

Comment