Support #1124

A binary variable for children under the age of 7 in a given household

Added by Nico Ochmann about 5 years ago. Updated 6 months ago.

Stephanie Auty
Data management
Start date:
% Done:



Dear Alita,
I hope you had a good holiday season. When you get the chance please help me with this one. I would like to define a binary variable equal to one if there is a child or more than one under the age of seven in a given household. My main data set consists of female characteristics and the characteristics of her husband or spouse. Essentially, I have only wives in my main dataset with their characteristics plus their spouses'.
I guess I must look at the w_child files to get the pidp of the child, the birthy of the child and the mnpid, which is the identification of the mother. Now the fun starts. How do I match or merge the information from the w_child files to my main file with wives' pidp only?
Once again I would highly appreciate your help.

Best wishes.



Updated by Stephanie Auty about 5 years ago

  • Status changed from New to In Progress
  • % Done changed from 0 to 10
  • Private changed from Yes to No

Many thanks for your enquiry. The Understanding Society team is looking into it and we will get back to you as soon as we can.

Best wishes,
Stephanie Auty - Understanding Society User Support Officer


Updated by Stephanie Auty about 5 years ago

  • Status changed from In Progress to Feedback
  • Assignee changed from Alita Nandi to Stephanie Auty
  • % Done changed from 10 to 80

Dear Nico,

It would be better to start with the w_indall file as all individuals are listed there. Keep the variables you will need (w_hidp, w_age_dv) then create a flag which indicates whether an individual is in the age range you want (0-7):
gen w_age_u7=0
replace w_age_u7=1 if w_age_dv>=0 & w_age_dv<=7

Then use bysort w_hidp: to indicate whether each household contains a child of that age:
gen w_hh_u7=0
bysort w_hidp: replace w_hh_u7=1 if w_age_u7==1 | w_hh_u7[_n-1]==1

Then use another bysort to keep the last record for each household which will contain the correct flag for the household:
bysort w_hidp: keep if _n==_N

Just keep w_hidp and w_hh_u7, and merge this (m:1 or 1:m depending on the order of the files) with the other file you have created using w_hidp.

Best wishes,


Updated by Nico Ochmann about 5 years ago

Dear Stephanie,

thank you very much for your help, your step descriptions were very detailed and I think I understand what you were doing.
I now generated the variable and it works just fine.

Best wishes.



Updated by Alita Nandi almost 5 years ago

  • Status changed from Feedback to Resolved

Updated by Understanding Society User Support Team 6 months ago

  • Category changed from Data analysis to Data management

Updated by Understanding Society User Support Team 6 months ago

  • % Done changed from 80 to 100

Also available in: Atom PDF