Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Assign value of a variable according to the values of other variables within a group

    Hi,

    I face a problem in assigning values of a variable within a group. This is a study about historical human fertility analysis.

    The births are grouped by mothers' id. Births of the same mother are in the same group. There is a variable denoting their birth orders.

    The births are denoted as sons or daughters. The births are also denoted whether they are dead or not. There is a variable identifying whether a newborn is dead. I want to study the effect of existing births on the likelihood of later births.

    The variable of existing births is a categorical variable: 0=surviving children of both sex; 1= only girl alive, no boy; 2= only boy alive, no girl; 3= no surving children of either sex

    The following table is an example of the full list:
    MOTHER_ID PERSON_ID boy girl birth_order died existing_births
    1 1 1 0 1 0 3
    1 2 1 0 2 0 2
    2 3 1 0 1 1 3
    3 4 0 1 1 1 3
    3 5 1 0 2 0 3
    3 6 1 0 3 0 2
    3 7 0 1 4 0 2
    4 8 1 0 1 0 3
    4 9 0 1 2 0 2
    5 10 1 0 1 0 3
    5 11 1 0 2 1 2
    5 12 0 1 3 0 2
    5 13 0 1 4 0 0
    5 14 1 0 5 0 0
    6 15 1 0 1 1 3
    7 16 0 1 1 0 3
    7 17 0 1 2 1 1
    7 18 0 1 3 0 1

    I wonder how to generate the values of "existing_births". This is a variable based on other three or four variables within a group. I did not figure out how to code it.

    Thank you very much for your kind and generous help!







  • #2
    Code:
    assert inlist(boy, 0, 1) & inlist(girl, 0, 1) & inlist(died, 0, 1)
    xtset mother_id birth_order
    by mother_id (birth_order), sort: gen boy_survived = sum(L.boy & !L.died)
    by mother_id (birth_order): gen girl_survived = sum(L.girl & !L.died)
    replace boy_survived = min(boy_survived, 1)
    replace girl_survived = min(girl_survived, 1)
    
    
    gen wanted = 2*boy_survived + girl_survived
    recode wanted (0 = 3) (3 = 0)
    In the future, when showing data examples, please use the -dataex- command to do so. If you are running version 17, 16 or a fully updated version 15.1 or 14.2, -dataex- is already part of your official Stata installation. If not, run -ssc install dataex- to get it. Either way, run -help dataex- to read the simple instructions for using it. -dataex- will save you time; it is easier and quicker than typing out tables. It includes complete information about aspects of the data that are often critical to answering your question but cannot be seen from tabular displays or screenshots. It also makes it possible for those who want to help you to create a faithful representation of your example to try out their code, which in turn makes it more likely that their answer will actually work in your data.

    Comment


    • #3
      Thank you very much, Prof. Schechter! Your solution is very useful!

      Comment

      Working...
      X