Announcement

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

  • Creating "male fraction conditional on birth order", using dummies for gender

    Dear All,

    1)The child having "_1" expression is the youngest (or last-born) child. By the same logic, the child having "_2" and "_3" expression is the oldest (or first born) child. It depends on the total number of children. It is also given in "birth order".
    2)For sex, 1 stands for "males" and 2 stands for "females".
    3) Age is given in months.

    In my analyses, the dependent variable is only available for the last-born (the youngest) child. My dependent variable is "duration of breastfeeding given in months". In the regressions, I noticed a gender gap and I am trying to understand "sibling composition". To that extent, here is what I would like to do:

    I would like to construct "male fraction" which should stands for the fraction of older siblings of child i that are male, conditional on birth order. Normally, it is very easy to generate a variable that says to me the total fraction of malebirths in the household. However, I would like to do it conditional on birth order. Yet, I am confused how to do this.

    If anybody can help me, I will be really appreiciated ! Thank you in advance.
    ID ChildSex_1 ChildSex_2 ChildSex_3 ChildAge_1 ChildAge_2 ChildAge_3 ChildBirthOrder_1 ChildBirthOrder_2 ChildBirthOrder_3 Total Male Births Total Female Birtsh Total Births
    1 1 1 2 6 19 46 3 2 1 2 1 3
    2 2 2 2 3 25 50 3 2 1 0 3 3
    3 2 2 . 4 17 . 2 1 . 0 2 2
    4 1 . . 7 . . 1 . . 1 0 1

  • #2
    You will get nowhere slowly trying to do this with the data organized this way. It's almost a no-brainer, however, if you reorganize the data into long layout.

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input byte(id childsex_1 childsex_2 childsex_3 childage_1 childage_2 childage_3 childbirthorder_1 childbirthorder_2 childbirthorder_3 totalmalebirths totalfemalebirtsh totalbirths)
    1 1 1 2 6 19 46 3 2 1 2 1 3
    2 2 2 2 3 25 50 3 2 1 0 3 3
    3 2 2 . 4 17  . 2 1 . 0 2 2
    4 1 . . 7  .  . 1 . . 1 0 1
    end
    
    reshape long childsex childage childbirthorder, i(id) j(_j) string
    destring _j, ignore("_") replace
    
    levelsof childbirthorder, local(bos)
    foreach bo of local bos {
        summ totalmalebirths if childbirthorder == `bo', meanonly
        local numerator `r(sum)'
        summ totalbirths if childbirthorder == `bo', meanonly
        local denominator `r(sum)'
        display "Male fraction for birth order " `bo' " = " `numerator'/`denominator'
    }
    If you want to, you can restore the wide layout after this is done just by running -reshape wide-. But probably whatever else you are going to do to analyze this data will similarly be easy in long layout and difficult to impossible in wide. So, I suggest you leave the data long unless you specifically know you will be using some of the few Stata commands that call for, or work best with, wide data.

    By the way, it's even simpler if instead of listing out the male fractions by birth order you want to create a variable in your data set containing the male fractions: After the -destring- command it's just one line of code:

    Code:
    by childbirthorder, sort: egen male_fraction = mean(childsex == 1)
    In the future, when showing data examples, please use the -dataex- command to do so, as I have here. 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.
    Last edited by Clyde Schechter; 03 May 2021, 12:41.

    Comment


    • #3
      Dear Clyde Schecther,

      I am very thankful for your detailed explanation. I am going to use dataex for my future requests.

      I am just curious about one thing. As I indicated before, my dependent variable is only available for the youngest child (whose birth order is highest).

      When I run the regression,how should I tell stata to just consider last-born children? I guess, I should do nothing because the "y" only appears for the last-born. Am I right? Should I specifcy any if condition before running my model?

      Originally posted by Clyde Schechter View Post
      You will get nowhere slowly trying to do this with the data organized this way. It's almost a no-brainer, however, if you reorganize the data into long layout.

      Code:
      * Example generated by -dataex-. For more info, type help dataex
      clear
      input byte(id childsex_1 childsex_2 childsex_3 childage_1 childage_2 childage_3 childbirthorder_1 childbirthorder_2 childbirthorder_3 totalmalebirths totalfemalebirtsh totalbirths)
      1 1 1 2 6 19 46 3 2 1 2 1 3
      2 2 2 2 3 25 50 3 2 1 0 3 3
      3 2 2 . 4 17 . 2 1 . 0 2 2
      4 1 . . 7 . . 1 . . 1 0 1
      end
      
      reshape long childsex childage childbirthorder, i(id) j(_j) string
      destring _j, ignore("_") replace
      
      levelsof childbirthorder, local(bos)
      foreach bo of local bos {
      summ totalmalebirths if childbirthorder == `bo', meanonly
      local numerator `r(sum)'
      summ totalbirths if childbirthorder == `bo', meanonly
      local denominator `r(sum)'
      display "Male fraction for birth order " `bo' " = " `numerator'/`denominator'
      }
      If you want to, you can restore the wide layout after this is done just by running -reshape wide-. But probably whatever else you are going to do to analyze this data will similarly be easy in long layout and difficult to impossible in wide. So, I suggest you leave the data long unless you specifically know you will be using some of the few Stata commands that call for, or work best with, wide data.

      By the way, it's even simpler if instead of listing out the male fractions by birth order you want to create a variable in your data set containing the male fractions: After the -destring- command it's just one line of code:

      Code:
      by childbirthorder, sort: egen male_fraction = mean(childsex == 1)
      In the future, when showing data examples, please use the -dataex- command to do so, as I have here. 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


      • #4
        When I run the regression,how should I tell stata to just consider last-born children? I guess, I should do nothing because the "y" only appears for the last-born. Am I right? Should I specifcy any if condition before running my model?
        In your particular circumstance, this is mostly a matter of taste.

        Given that the outcome variable is missing for all but the last-born children, all observations on non-last-born children will be omitted automatically. The main drawback to this approach is that when you revisit this code sometime in the future when it is no longer fresh in your memory, you might or might not remember that fact and it may or may not otherwise be apparent that the analysis is being carried out only on last-born children. So you could put in an -if- condition (that will not actually affect the calculations) to make it clear. Or perhaps, more simply, before the regression command(s) insert a comment into the code pointing out that the dependent variable is only available in last-born children, hence these analyses include last-born children only.

        Comment


        • #5
          Dear Clyde Schecther,

          You are right about the fact that all observations will be omitted due to absence of older siblings. I will keep in mind the other issue you have just mentioned. Thank you very much !!! You made my day !!
          Originally posted by Clyde Schechter View Post
          In your particular circumstance, this is mostly a matter of taste.

          Given that the outcome variable is missing for all but the last-born children, all observations on non-last-born children will be omitted automatically. The main drawback to this approach is that when you revisit this code sometime in the future when it is no longer fresh in your memory, you might or might not remember that fact and it may or may not otherwise be apparent that the analysis is being carried out only on last-born children. So you could put in an -if- condition (that will not actually affect the calculations) to make it clear. Or perhaps, more simply, before the regression command(s) insert a comment into the code pointing out that the dependent variable is only available in last-born children, hence these analyses include last-born children only.

          Comment


          • #6
            Dear Clyde Schecther,

            I have tried to your code but, I could not figure out what is going on (tbh). I run the following code (HW1_ = age in months; BORD_ = birth order; B4_ = sex of children)

            reshape long HW1_ B4_ BORD_ , i(CASEID) j(_j) string
            destring _j, ignore("_") replace

            When I run this code, (before running the other) my dependent variable (i.e., bfduration2) appears each observation rather than just for last-born child. I also attached a small portion of my data. When you run the code above, something confusing occurs. How can I overcome this problem?


            Thank you so much !

            Originally posted by Cansu Oymak View Post
            Dear Clyde Schecther,

            You are right about the fact that all observations will be omitted due to absence of older siblings. I will keep in mind the other issue you have just mentioned. Thank you very much !!! You made my day !!
            Attached Files

            Comment


            • #7
              I need an example of your data to work with. I'm sure that was your intent in attaching example.dta, but that is not a good way to do this. I am among those who, for safety reasons, do not download attachments from people I do not know. As already pointed out in #2, and in the Forum FAQ, which all users have been asked to read before posting, that should be done using the -dataex- command.

              Please post back and use the -dataex- command to show example data that exhibits your problem, and I will be happy to troubleshoot it.

              Comment


              • #8
                This is certainly understandable, and it is my thoughlessness. I am sorry for that. I have just tried dataex. I hope it works

                Code:
                * Example generated by -dataex-. To install: ssc install dataex
                clear
                input str15 CASEID double(BORD_01 BORD_02 B4_01 B4_02 HW1_1 HW1_2) float(survey_year mother_age completionof8years mode_of_delivery region5 region12 rural_urban wealth_index bfduration2) byte(femalebirths malebirths) float totalbirths
                "    01010004 02" 1 . 2 . 49  . 2018 28 0 0 1 1 1 4 30 1 0 1
                "    01010007 02" 1 . 2 . 33  . 2018 30 1 0 1 1 1 5 12 1 0 1
                "    01010011 02" 1 . 1 .  8  . 2018 29 1 0 1 1 1 5  8 0 1 1
                "    01010021 02" 1 . 1 . 56  . 2018 35 1 0 1 1 1 5 18 0 1 1
                "    01050003 02" 2 1 2 2 55  . 2018 30 1 0 1 1 1 4 19 2 0 2
                "    01060002 02" 4 3 1 1 54  . 2018 41 1 1 1 1 2 3 18 1 3 4
                "    01060005 02" 5 4 2 1 56  . 2018 35 0 0 1 1 2 1 18 3 2 5
                "    01060006 03" 2 1 2 1 41 56 2018 26 1 0 1 1 2 1  3 1 1 2
                "    01060007 06" 4 3 2 2 27 50 2018 43 0 1 1 1 2 1 10 3 1 4
                "    01060013 02" 2 1 2 1  2  . 2018 32 0 0 1 1 2 2  2 1 1 2
                "    01060017 02" 3 2 2 2 48  . 2018 37 0 1 1 1 2 2 36 2 1 3
                "    01060020 02" 1 . 2 . 59  . 2018 23 1 0 1 1 2 3 18 1 0 1
                "    01070002 02" 2 1 1 1 39  . 2018 42 0 0 1 1 1 3 24 0 2 2
                "    01070003 02" 2 1 1 1 24 45 2018 22 1 0 1 1 1 5 24 0 2 2
                "    01070005 02" 4 3 1 2 54  . 2018 28 1 0 1 1 1 1  8 1 3 4
                "    01070008 02" 3 2 1 1  1  1 2018 32 1 1 1 1 1 3  1 0 3 3
                "    01070009 02" 2 1 2 1 32  . 2018 44 1 1 1 1 1 5 20 1 1 2
                "    01070020 02" 2 1 2 1 10  . 2018 31 1 1 1 1 1 4 10 1 1 2
                "    01080005 02" 1 . 1 . 22  . 2018 32 1 1 1 1 1 5  7 0 1 1
                "    01080008 02" 1 . 2 . 57  . 2018 35 1 0 1 1 1 5 24 1 0 1
                "    01090009 02" 1 . 2 .  6  . 2018 24 1 1 1 1 1 3  6 1 0 1
                "    01090020 03" 2 1 2 1 14 55 2018 29 1 0 1 1 1 2 14 1 1 2
                "    01100017 02" 4 3 1 2 38 56 2018 29 1 0 1 1 1 4 27 3 1 4
                "    01110019 02" 2 1 1 1 26  . 2018 35 1 1 1 1 1 5 26 0 2 2
                "    01120004 02" 2 1 2 1 39  . 2018 26 1 0 1 1 1 2 18 1 1 2
                "    01120020 02" 5 4 2 1 18  . 2018 33 0 0 1 1 1 2  9 1 4 5
                "    01140021 02" 2 1 2 1 19  . 2018 36 1 1 1 1 1 5 15 1 1 2
                "    01170008 02" 1 . 2 . 21  . 2018 26 1 1 1 1 1 5 21 1 0 1
                "    01170009 02" 1 . 2 . 13  . 2018 32 1 1 1 1 1 4 13 1 0 1
                "    01180002 02" 1 . 2 . 13  . 2018 34 1 1 1 1 1 5 13 1 0 1
                "    01180017 02" 1 . 2 . 35  . 2018 32 1 1 1 1 1 5 35 1 0 1
                "    01190004 02" 1 . 2 .  9  . 2018 33 1 0 1 1 1 3  6 1 0 1
                "    01190008 02" 1 . 2 .  3  . 2018 27 1 1 1 1 1 4  3 1 0 1
                "    01190018 01" 1 . 2 . 57  . 2018 41 0 1 1 1 1 4  2 1 0 1
                "    01200001 02" 2 1 1 1 17  . 2018 41 0 1 1 1 2 4  7 0 2 2
                "    01200002 02" 1 . 2 . 52  . 2018 28 1 0 1 1 2 2 21 1 0 1
                "    01200008 02" 2 1 2 2  8  . 2018 35 1 1 1 1 2 3  8 2 0 2
                "    01210004 02" 1 . 1 . 10  . 2018 19 1 1 1 1 1 1  1 0 1 1
                "    01210005 02" 3 2 2 1  5 59 2018 23 0 0 1 1 1 1  5 2 1 3
                "    01210007 02" 2 1 1 2 12 43 2018 22 1 1 1 1 1 1 12 1 1 2
                "    01210010 02" 2 1 2 2 19  . 2018 26 1 0 1 1 1 5 12 2 0 2
                "    01210014 02" 2 1 2 2 27  . 2018 22 0 0 1 1 1 1 18 2 0 2
                "    01230016 02" 1 . 2 . 45  . 2018 35 1 1 1 1 1 5 10 1 0 1
                "    01230017 02" 2 1 1 2 52  . 2018 41 1 1 1 1 1 5 36 1 1 2
                "    01240015 01" 1 . 1 . 50  . 2018 37 1 1 1 1 1 5 18 0 1 1
                "    01240019 01" 1 . 2 .  4  . 2018 33 1 1 1 1 1 5  4 1 0 1
                "    01250004 02" 2 1 2 2  3  . 2018 35 1 0 1 1 1 5  3 2 0 2
                "    01250006 02" 2 1 2 1 10 34 2018 21 1 1 1 1 1 4 10 1 1 2
                "    01250011 02" 2 1 1 2  7 29 2018 21 1 0 1 1 1 4  5 1 1 2
                "    01250021 02" 3 2 1 1 57  . 2018 45 0 1 1 1 1 4 24 0 3 3
                "    01260006 02" 2 1 2 1 46  . 2018 33 0 0 1 1 1 3 46 1 1 2
                "    01270006 02" 1 . 1 . 47  . 2018 25 1 0 1 1 1 4  2 0 1 1
                "    01270008 02" 3 2 2 2 41  . 2018 36 1 0 1 1 1 5 24 3 0 3
                "    01280004 02" 4 3 1 1 28  . 2018 31 0 1 1 1 1 2 20 2 2 4
                "    01280005 02" 2 1 1 2 22  . 2018 33 0 1 1 1 1 5 22 1 1 2
                "    01280019 01" 1 . 1 .  3  . 2018 33 1 0 1 1 1 5  3 0 1 1
                "    01280020 02" 1 . 1 . 35  . 2018 29 1 1 1 1 1 5  6 0 1 1
                "    01300006 02" 2 1 2 2 34 54 2018 25 1 1 1 1 1 4  4 2 0 2
                "    01300009 02" 3 2 2 2 59  . 2018 38 0 1 1 1 1 3 24 3 0 3
                "    01300010 02" 2 1 1 1 36  . 2018 29 1 1 1 1 1 4 18 0 2 2
                "    01300015 02" 1 . 2 .  5  . 2018 25 1 0 1 1 1 4  5 1 0 1
                "    01310014 02" 1 . 1 . 13  . 2018 24 1 0 1 1 1 4  5 0 1 1
                "    01310015 02" 3 2 2 2  0 24 2018 30 0 0 1 1 1 3  1 2 1 3
                "    01310019 04" 1 . 1 . 45  . 2018 28 0 1 1 1 1 2 12 0 1 1
                "    01320011 02" 2 1 1 1 41  . 2018 35 1 1 1 1 1 5 24 0 2 2
                "    01330003 02" 1 . 2 . 35  . 2018 29 1 1 1 1 1 3 11 1 0 1
                "    01330014 02" 2 1 2 1  8 54 2018 31 1 1 1 1 1 3  8 1 1 2
                "    01330016 02" 4 3 1 1 20 52 2018 24 0 0 1 1 1 1 20 0 4 4
                "    01340006 02" 3 2 2 2 59  . 2018 30 1 1 1 1 1 5 24 2 1 3
                "    01340009 02" 2 1 2 2  6 42 2018 23 1 1 1 1 1 4  6 2 0 2
                "    01340010 02" 2 1 1 2 48  . 2018 32 0 0 1 1 1 3 18 1 1 2
                "    01340011 02" 2 1 2 1 37  . 2018 28 1 0 1 1 1 5 24 1 1 2
                "    01340012 02" 3 2 2 1 26 43 2018 26 0 1 1 1 1 2 26 1 2 3
                "    01360001 02" 1 . 1 . 34  . 2018 23 1 1 1 1 1 3 18 0 1 1
                "    01360002 02" 4 3 2 2  8 46 2018 28 0 0 1 1 1 3  8 3 1 4
                "    01360007 02" 2 1 1 1 39  . 2018 38 1 1 1 1 1 3 18 0 2 2
                "    01360009 02" 1 . 2 . 26  . 2018 21 0 0 1 1 1 2  2 1 0 1
                "    01360011 02" 1 . 2 . 10  . 2018 28 1 0 1 1 1 3 10 1 0 1
                "    01360021 02" 2 1 1 2 33  . 2018 34 1 0 1 1 1 3  6 1 1 2
                "    01370016 02" 3 2 2 2  2 18 2018 40 1 1 1 1 1 5  2 2 1 3
                "    01380008 02" 2 1 1 1 18  . 2018 35 1 1 1 1 1 5 18 0 2 2
                "    01380009 02" 2 1 1 2 55  . 2018 33 1 1 1 1 1 5 24 1 1 2
                "    01380012 01" 3 2 1 1 21  . 2018 37 0 1 1 1 1 5  7 1 2 3
                "    01380021 01" 3 2 1 1  4  . 2018 34 1 0 1 1 1 5  4 1 2 3
                "    01390012 02" 4 3 2 2 28  . 2018 32 0 0 1 1 1 2 20 4 0 4
                "    01400021 02" 3 2 1 2 26  . 2018 37 0 1 1 1 1 3 26 2 1 3
                "    01430012 02" 2 1 1 1 56  . 2018 38 0 1 1 1 1 4 24 0 2 2
                "    01430014 04" 1 . 1 . 23  . 2018 23 1 1 1 1 1 3  6 0 1 1
                "    01440009 02" 1 . 1 .  2  . 2018 37 1 0 1 1 1 4  2 0 1 1
                "    01440015 02" 3 2 2 2 30  . 2018 41 1 1 1 1 1 5 24 3 0 3
                "    01440016 02" 4 3 1 1  1  . 2018 37 1 1 1 1 1 4  1 1 3 4
                "    01440017 02" 3 2 1 2  7 29 2018 28 1 1 1 1 1 4  7 2 1 3
                "    01450021 02" 2 1 1 2 29  . 2018 26 1 1 1 1 1 3 29 1 1 2
                "    01460002 02" 2 1 1 2 28 54 2018 25 0 0 1 1 1 3 12 1 1 2
                "    01460003 02" 2 1 2 1 56  . 2018 35 1 0 1 1 1 5 27 1 1 2
                "    01460005 02" 3 2 2 2 54  . 2018 41 0 0 1 1 1 3 24 2 1 3
                "    01460009 02" 1 . 2 . 34  . 2018 25 1 0 1 1 1 3 24 1 0 1
                "    01460015 02" 3 2 1 2 39  . 2018 31 0 0 1 1 1 5 30 2 1 3
                "    01480011 02" 2 1 2 2 49  . 2018 40 1 1 1 1 1 5  6 2 0 2
                "    01490005 04" 2 1 1 2  0 14 2018 18 0 1 1 1 1 2  1 1 1 2
                end
                label values B4_01 B4_01
                label def B4_01 1 "Male", modify
                label def B4_01 2 "Female", modify
                label values B4_02 B4_02
                label def B4_02 1 "Male", modify
                label def B4_02 2 "Female", modify
                label values completionof8years completionof8years
                label def completionof8years 0 "Not Completed", modify
                label def completionof8years 1 "Completed", modify
                label values mode_of_delivery mode_of_delivery
                label def mode_of_delivery 0 "Vaginal Birth", modify
                label def mode_of_delivery 1 "Ceasarean", modify
                label values region5 region5
                label def region5 1 " West", modify
                label values region12 region12
                label def region12 1 "Istanbul", modify
                label values rural_urban rural_urban
                label def rural_urban 1 "Urban", modify
                label def rural_urban 2 "Rural", modify
                label values wealth_index wealth_index
                label def wealth_index 1 "Poorest", modify
                label def wealth_index 2 "Poorer", modify
                label def wealth_index 3 "Middle", modify
                label def wealth_index 4 "Rich", modify
                label def wealth_index 5 "Richest", modify

                Originally posted by Clyde Schechter View Post
                I need an example of your data to work with. I'm sure that was your intent in attaching example.dta, but that is not a good way to do this. I am among those who, for safety reasons, do not download attachments from people I do not know. As already pointed out in #2, and in the Forum FAQ, which all users have been asked to read before posting, that should be done using the -dataex- command.

                Please post back and use the -dataex- command to show example data that exhibits your problem, and I will be happy to troubleshoot it.

                Comment


                • #9
                  Code:
                  * Example generated by -dataex-. To install: ssc install dataex
                  clear
                  input str15 CASEID double(BORD_01 BORD_02 B4_01 B4_02 HW1_1 HW1_2) float(survey_year mother_age completionof8years mode_of_delivery region5 region12 rural_urban wealth_index bfduration2) byte(femalebirths malebirths) float totalbirths
                  "    01010004 02" 1 . 2 . 49  . 2018 28 0 0 1 1 1 4 30 1 0 1
                  "    01010007 02" 1 . 2 . 33  . 2018 30 1 0 1 1 1 5 12 1 0 1
                  "    01010011 02" 1 . 1 .  8  . 2018 29 1 0 1 1 1 5  8 0 1 1
                  "    01010021 02" 1 . 1 . 56  . 2018 35 1 0 1 1 1 5 18 0 1 1
                  "    01050003 02" 2 1 2 2 55  . 2018 30 1 0 1 1 1 4 19 2 0 2
                  "    01060002 02" 4 3 1 1 54  . 2018 41 1 1 1 1 2 3 18 1 3 4
                  "    01060005 02" 5 4 2 1 56  . 2018 35 0 0 1 1 2 1 18 3 2 5
                  "    01060006 03" 2 1 2 1 41 56 2018 26 1 0 1 1 2 1  3 1 1 2
                  "    01060007 06" 4 3 2 2 27 50 2018 43 0 1 1 1 2 1 10 3 1 4
                  "    01060013 02" 2 1 2 1  2  . 2018 32 0 0 1 1 2 2  2 1 1 2
                  "    01060017 02" 3 2 2 2 48  . 2018 37 0 1 1 1 2 2 36 2 1 3
                  "    01060020 02" 1 . 2 . 59  . 2018 23 1 0 1 1 2 3 18 1 0 1
                  "    01070002 02" 2 1 1 1 39  . 2018 42 0 0 1 1 1 3 24 0 2 2
                  "    01070003 02" 2 1 1 1 24 45 2018 22 1 0 1 1 1 5 24 0 2 2
                  "    01070005 02" 4 3 1 2 54  . 2018 28 1 0 1 1 1 1  8 1 3 4
                  "    01070008 02" 3 2 1 1  1  1 2018 32 1 1 1 1 1 3  1 0 3 3
                  "    01070009 02" 2 1 2 1 32  . 2018 44 1 1 1 1 1 5 20 1 1 2
                  "    01070020 02" 2 1 2 1 10  . 2018 31 1 1 1 1 1 4 10 1 1 2
                  "    01080005 02" 1 . 1 . 22  . 2018 32 1 1 1 1 1 5  7 0 1 1
                  "    01080008 02" 1 . 2 . 57  . 2018 35 1 0 1 1 1 5 24 1 0 1
                  "    01090009 02" 1 . 2 .  6  . 2018 24 1 1 1 1 1 3  6 1 0 1
                  "    01090020 03" 2 1 2 1 14 55 2018 29 1 0 1 1 1 2 14 1 1 2
                  "    01100017 02" 4 3 1 2 38 56 2018 29 1 0 1 1 1 4 27 3 1 4
                  "    01110019 02" 2 1 1 1 26  . 2018 35 1 1 1 1 1 5 26 0 2 2
                  "    01120004 02" 2 1 2 1 39  . 2018 26 1 0 1 1 1 2 18 1 1 2
                  "    01120020 02" 5 4 2 1 18  . 2018 33 0 0 1 1 1 2  9 1 4 5
                  "    01140021 02" 2 1 2 1 19  . 2018 36 1 1 1 1 1 5 15 1 1 2
                  "    01170008 02" 1 . 2 . 21  . 2018 26 1 1 1 1 1 5 21 1 0 1
                  "    01170009 02" 1 . 2 . 13  . 2018 32 1 1 1 1 1 4 13 1 0 1
                  "    01180002 02" 1 . 2 . 13  . 2018 34 1 1 1 1 1 5 13 1 0 1
                  "    01180017 02" 1 . 2 . 35  . 2018 32 1 1 1 1 1 5 35 1 0 1
                  "    01190004 02" 1 . 2 .  9  . 2018 33 1 0 1 1 1 3  6 1 0 1
                  "    01190008 02" 1 . 2 .  3  . 2018 27 1 1 1 1 1 4  3 1 0 1
                  "    01190018 01" 1 . 2 . 57  . 2018 41 0 1 1 1 1 4  2 1 0 1
                  "    01200001 02" 2 1 1 1 17  . 2018 41 0 1 1 1 2 4  7 0 2 2
                  "    01200002 02" 1 . 2 . 52  . 2018 28 1 0 1 1 2 2 21 1 0 1
                  "    01200008 02" 2 1 2 2  8  . 2018 35 1 1 1 1 2 3  8 2 0 2
                  "    01210004 02" 1 . 1 . 10  . 2018 19 1 1 1 1 1 1  1 0 1 1
                  "    01210005 02" 3 2 2 1  5 59 2018 23 0 0 1 1 1 1  5 2 1 3
                  "    01210007 02" 2 1 1 2 12 43 2018 22 1 1 1 1 1 1 12 1 1 2
                  "    01210010 02" 2 1 2 2 19  . 2018 26 1 0 1 1 1 5 12 2 0 2
                  "    01210014 02" 2 1 2 2 27  . 2018 22 0 0 1 1 1 1 18 2 0 2
                  "    01230016 02" 1 . 2 . 45  . 2018 35 1 1 1 1 1 5 10 1 0 1
                  "    01230017 02" 2 1 1 2 52  . 2018 41 1 1 1 1 1 5 36 1 1 2
                  "    01240015 01" 1 . 1 . 50  . 2018 37 1 1 1 1 1 5 18 0 1 1
                  "    01240019 01" 1 . 2 .  4  . 2018 33 1 1 1 1 1 5  4 1 0 1
                  "    01250004 02" 2 1 2 2  3  . 2018 35 1 0 1 1 1 5  3 2 0 2
                  "    01250006 02" 2 1 2 1 10 34 2018 21 1 1 1 1 1 4 10 1 1 2
                  "    01250011 02" 2 1 1 2  7 29 2018 21 1 0 1 1 1 4  5 1 1 2
                  "    01250021 02" 3 2 1 1 57  . 2018 45 0 1 1 1 1 4 24 0 3 3
                  "    01260006 02" 2 1 2 1 46  . 2018 33 0 0 1 1 1 3 46 1 1 2
                  "    01270006 02" 1 . 1 . 47  . 2018 25 1 0 1 1 1 4  2 0 1 1
                  "    01270008 02" 3 2 2 2 41  . 2018 36 1 0 1 1 1 5 24 3 0 3
                  "    01280004 02" 4 3 1 1 28  . 2018 31 0 1 1 1 1 2 20 2 2 4
                  "    01280005 02" 2 1 1 2 22  . 2018 33 0 1 1 1 1 5 22 1 1 2
                  "    01280019 01" 1 . 1 .  3  . 2018 33 1 0 1 1 1 5  3 0 1 1
                  "    01280020 02" 1 . 1 . 35  . 2018 29 1 1 1 1 1 5  6 0 1 1
                  "    01300006 02" 2 1 2 2 34 54 2018 25 1 1 1 1 1 4  4 2 0 2
                  "    01300009 02" 3 2 2 2 59  . 2018 38 0 1 1 1 1 3 24 3 0 3
                  "    01300010 02" 2 1 1 1 36  . 2018 29 1 1 1 1 1 4 18 0 2 2
                  "    01300015 02" 1 . 2 .  5  . 2018 25 1 0 1 1 1 4  5 1 0 1
                  "    01310014 02" 1 . 1 . 13  . 2018 24 1 0 1 1 1 4  5 0 1 1
                  "    01310015 02" 3 2 2 2  0 24 2018 30 0 0 1 1 1 3  1 2 1 3
                  "    01310019 04" 1 . 1 . 45  . 2018 28 0 1 1 1 1 2 12 0 1 1
                  "    01320011 02" 2 1 1 1 41  . 2018 35 1 1 1 1 1 5 24 0 2 2
                  "    01330003 02" 1 . 2 . 35  . 2018 29 1 1 1 1 1 3 11 1 0 1
                  "    01330014 02" 2 1 2 1  8 54 2018 31 1 1 1 1 1 3  8 1 1 2
                  "    01330016 02" 4 3 1 1 20 52 2018 24 0 0 1 1 1 1 20 0 4 4
                  "    01340006 02" 3 2 2 2 59  . 2018 30 1 1 1 1 1 5 24 2 1 3
                  "    01340009 02" 2 1 2 2  6 42 2018 23 1 1 1 1 1 4  6 2 0 2
                  "    01340010 02" 2 1 1 2 48  . 2018 32 0 0 1 1 1 3 18 1 1 2
                  "    01340011 02" 2 1 2 1 37  . 2018 28 1 0 1 1 1 5 24 1 1 2
                  "    01340012 02" 3 2 2 1 26 43 2018 26 0 1 1 1 1 2 26 1 2 3
                  "    01360001 02" 1 . 1 . 34  . 2018 23 1 1 1 1 1 3 18 0 1 1
                  "    01360002 02" 4 3 2 2  8 46 2018 28 0 0 1 1 1 3  8 3 1 4
                  "    01360007 02" 2 1 1 1 39  . 2018 38 1 1 1 1 1 3 18 0 2 2
                  "    01360009 02" 1 . 2 . 26  . 2018 21 0 0 1 1 1 2  2 1 0 1
                  "    01360011 02" 1 . 2 . 10  . 2018 28 1 0 1 1 1 3 10 1 0 1
                  "    01360021 02" 2 1 1 2 33  . 2018 34 1 0 1 1 1 3  6 1 1 2
                  "    01370016 02" 3 2 2 2  2 18 2018 40 1 1 1 1 1 5  2 2 1 3
                  "    01380008 02" 2 1 1 1 18  . 2018 35 1 1 1 1 1 5 18 0 2 2
                  "    01380009 02" 2 1 1 2 55  . 2018 33 1 1 1 1 1 5 24 1 1 2
                  "    01380012 01" 3 2 1 1 21  . 2018 37 0 1 1 1 1 5  7 1 2 3
                  "    01380021 01" 3 2 1 1  4  . 2018 34 1 0 1 1 1 5  4 1 2 3
                  "    01390012 02" 4 3 2 2 28  . 2018 32 0 0 1 1 1 2 20 4 0 4
                  "    01400021 02" 3 2 1 2 26  . 2018 37 0 1 1 1 1 3 26 2 1 3
                  "    01430012 02" 2 1 1 1 56  . 2018 38 0 1 1 1 1 4 24 0 2 2
                  "    01430014 04" 1 . 1 . 23  . 2018 23 1 1 1 1 1 3  6 0 1 1
                  "    01440009 02" 1 . 1 .  2  . 2018 37 1 0 1 1 1 4  2 0 1 1
                  "    01440015 02" 3 2 2 2 30  . 2018 41 1 1 1 1 1 5 24 3 0 3
                  "    01440016 02" 4 3 1 1  1  . 2018 37 1 1 1 1 1 4  1 1 3 4
                  "    01440017 02" 3 2 1 2  7 29 2018 28 1 1 1 1 1 4  7 2 1 3
                  "    01450021 02" 2 1 1 2 29  . 2018 26 1 1 1 1 1 3 29 1 1 2
                  "    01460002 02" 2 1 1 2 28 54 2018 25 0 0 1 1 1 3 12 1 1 2
                  "    01460003 02" 2 1 2 1 56  . 2018 35 1 0 1 1 1 5 27 1 1 2
                  "    01460005 02" 3 2 2 2 54  . 2018 41 0 0 1 1 1 3 24 2 1 3
                  "    01460009 02" 1 . 2 . 34  . 2018 25 1 0 1 1 1 3 24 1 0 1
                  "    01460015 02" 3 2 1 2 39  . 2018 31 0 0 1 1 1 5 30 2 1 3
                  "    01480011 02" 2 1 2 2 49  . 2018 40 1 1 1 1 1 5  6 2 0 2
                  "    01490005 04" 2 1 1 2  0 14 2018 18 0 1 1 1 1 2  1 1 1 2
                  end
                  label values B4_01 B4_01
                  label def B4_01 1 "Male", modify
                  label def B4_01 2 "Female", modify
                  label values B4_02 B4_02
                  label def B4_02 1 "Male", modify
                  label def B4_02 2 "Female", modify
                  label values completionof8years completionof8years
                  label def completionof8years 0 "Not Completed", modify
                  label def completionof8years 1 "Completed", modify
                  label values mode_of_delivery mode_of_delivery
                  label def mode_of_delivery 0 "Vaginal Birth", modify
                  label def mode_of_delivery 1 "Ceasarean", modify
                  label values region5 region5
                  label def region5 1 " West", modify
                  label values region12 region12
                  label def region12 1 "Istanbul", modify
                  label values rural_urban rural_urban
                  label def rural_urban 1 "Urban", modify
                  label def rural_urban 2 "Rural", modify
                  label values wealth_index wealth_index
                  label def wealth_index 1 "Poorest", modify
                  label def wealth_index 2 "Poorer", modify
                  label def wealth_index 3 "Middle", modify
                  label def wealth_index 4 "Rich", modify
                  label def wealth_index 5 "Richest", modify
                  
                  
                  
                  reshape long childsex HW1  B4 BORD, i(CASEID) j(_j) string
                  destring _j, ignore("_") replace
                  
                  levelsof BORD, local(bos)
                  foreach bo of local bos {
                      summ malebirths if BORD == `bo', meanonly
                      local numerator `r(sum)'
                      summ totalbirths if BORD == `bo', meanonly
                      local denominator `r(sum)'
                      display "Male fraction for birth order " `bo' " = " `numerator'/`denominator'
                  }
                  
                  by BORD, sort: egen male_fraction = mean(HW1 == 1)
                  
                  // NOW REMOVE bfduration2 FROM OBSERVATIONS NOT PURTAINING TO YOUNGEST CHILD
                  by CASEID (HW1): replace bfduration2 = . if _n > 1
                  This is essentially the same code as before, with the names of the variables changed to reflect your -dataex- example's. The only thing truly new is the final command which eliminates the values of bfduration2 for all but the youngest child. (It would also have been possible to revise the code so that those extra copies of bfduration2 never arose in the first place, but that way required slightly longer, more complicated code.)

                  Comment


                  • #10
                    Now it worked for me. Thank you so much dear Professor. You saved my master's




                    Originally posted by Clyde Schechter View Post
                    Code:
                    * Example generated by -dataex-. To install: ssc install dataex
                    clear
                    input str15 CASEID double(BORD_01 BORD_02 B4_01 B4_02 HW1_1 HW1_2) float(survey_year mother_age completionof8years mode_of_delivery region5 region12 rural_urban wealth_index bfduration2) byte(femalebirths malebirths) float totalbirths
                    " 01010004 02" 1 . 2 . 49 . 2018 28 0 0 1 1 1 4 30 1 0 1
                    " 01010007 02" 1 . 2 . 33 . 2018 30 1 0 1 1 1 5 12 1 0 1
                    " 01010011 02" 1 . 1 . 8 . 2018 29 1 0 1 1 1 5 8 0 1 1
                    " 01010021 02" 1 . 1 . 56 . 2018 35 1 0 1 1 1 5 18 0 1 1
                    " 01050003 02" 2 1 2 2 55 . 2018 30 1 0 1 1 1 4 19 2 0 2
                    " 01060002 02" 4 3 1 1 54 . 2018 41 1 1 1 1 2 3 18 1 3 4
                    " 01060005 02" 5 4 2 1 56 . 2018 35 0 0 1 1 2 1 18 3 2 5
                    " 01060006 03" 2 1 2 1 41 56 2018 26 1 0 1 1 2 1 3 1 1 2
                    " 01060007 06" 4 3 2 2 27 50 2018 43 0 1 1 1 2 1 10 3 1 4
                    " 01060013 02" 2 1 2 1 2 . 2018 32 0 0 1 1 2 2 2 1 1 2
                    " 01060017 02" 3 2 2 2 48 . 2018 37 0 1 1 1 2 2 36 2 1 3
                    " 01060020 02" 1 . 2 . 59 . 2018 23 1 0 1 1 2 3 18 1 0 1
                    " 01070002 02" 2 1 1 1 39 . 2018 42 0 0 1 1 1 3 24 0 2 2
                    " 01070003 02" 2 1 1 1 24 45 2018 22 1 0 1 1 1 5 24 0 2 2
                    " 01070005 02" 4 3 1 2 54 . 2018 28 1 0 1 1 1 1 8 1 3 4
                    " 01070008 02" 3 2 1 1 1 1 2018 32 1 1 1 1 1 3 1 0 3 3
                    " 01070009 02" 2 1 2 1 32 . 2018 44 1 1 1 1 1 5 20 1 1 2
                    " 01070020 02" 2 1 2 1 10 . 2018 31 1 1 1 1 1 4 10 1 1 2
                    " 01080005 02" 1 . 1 . 22 . 2018 32 1 1 1 1 1 5 7 0 1 1
                    " 01080008 02" 1 . 2 . 57 . 2018 35 1 0 1 1 1 5 24 1 0 1
                    " 01090009 02" 1 . 2 . 6 . 2018 24 1 1 1 1 1 3 6 1 0 1
                    " 01090020 03" 2 1 2 1 14 55 2018 29 1 0 1 1 1 2 14 1 1 2
                    " 01100017 02" 4 3 1 2 38 56 2018 29 1 0 1 1 1 4 27 3 1 4
                    " 01110019 02" 2 1 1 1 26 . 2018 35 1 1 1 1 1 5 26 0 2 2
                    " 01120004 02" 2 1 2 1 39 . 2018 26 1 0 1 1 1 2 18 1 1 2
                    " 01120020 02" 5 4 2 1 18 . 2018 33 0 0 1 1 1 2 9 1 4 5
                    " 01140021 02" 2 1 2 1 19 . 2018 36 1 1 1 1 1 5 15 1 1 2
                    " 01170008 02" 1 . 2 . 21 . 2018 26 1 1 1 1 1 5 21 1 0 1
                    " 01170009 02" 1 . 2 . 13 . 2018 32 1 1 1 1 1 4 13 1 0 1
                    " 01180002 02" 1 . 2 . 13 . 2018 34 1 1 1 1 1 5 13 1 0 1
                    " 01180017 02" 1 . 2 . 35 . 2018 32 1 1 1 1 1 5 35 1 0 1
                    " 01190004 02" 1 . 2 . 9 . 2018 33 1 0 1 1 1 3 6 1 0 1
                    " 01190008 02" 1 . 2 . 3 . 2018 27 1 1 1 1 1 4 3 1 0 1
                    " 01190018 01" 1 . 2 . 57 . 2018 41 0 1 1 1 1 4 2 1 0 1
                    " 01200001 02" 2 1 1 1 17 . 2018 41 0 1 1 1 2 4 7 0 2 2
                    " 01200002 02" 1 . 2 . 52 . 2018 28 1 0 1 1 2 2 21 1 0 1
                    " 01200008 02" 2 1 2 2 8 . 2018 35 1 1 1 1 2 3 8 2 0 2
                    " 01210004 02" 1 . 1 . 10 . 2018 19 1 1 1 1 1 1 1 0 1 1
                    " 01210005 02" 3 2 2 1 5 59 2018 23 0 0 1 1 1 1 5 2 1 3
                    " 01210007 02" 2 1 1 2 12 43 2018 22 1 1 1 1 1 1 12 1 1 2
                    " 01210010 02" 2 1 2 2 19 . 2018 26 1 0 1 1 1 5 12 2 0 2
                    " 01210014 02" 2 1 2 2 27 . 2018 22 0 0 1 1 1 1 18 2 0 2
                    " 01230016 02" 1 . 2 . 45 . 2018 35 1 1 1 1 1 5 10 1 0 1
                    " 01230017 02" 2 1 1 2 52 . 2018 41 1 1 1 1 1 5 36 1 1 2
                    " 01240015 01" 1 . 1 . 50 . 2018 37 1 1 1 1 1 5 18 0 1 1
                    " 01240019 01" 1 . 2 . 4 . 2018 33 1 1 1 1 1 5 4 1 0 1
                    " 01250004 02" 2 1 2 2 3 . 2018 35 1 0 1 1 1 5 3 2 0 2
                    " 01250006 02" 2 1 2 1 10 34 2018 21 1 1 1 1 1 4 10 1 1 2
                    " 01250011 02" 2 1 1 2 7 29 2018 21 1 0 1 1 1 4 5 1 1 2
                    " 01250021 02" 3 2 1 1 57 . 2018 45 0 1 1 1 1 4 24 0 3 3
                    " 01260006 02" 2 1 2 1 46 . 2018 33 0 0 1 1 1 3 46 1 1 2
                    " 01270006 02" 1 . 1 . 47 . 2018 25 1 0 1 1 1 4 2 0 1 1
                    " 01270008 02" 3 2 2 2 41 . 2018 36 1 0 1 1 1 5 24 3 0 3
                    " 01280004 02" 4 3 1 1 28 . 2018 31 0 1 1 1 1 2 20 2 2 4
                    " 01280005 02" 2 1 1 2 22 . 2018 33 0 1 1 1 1 5 22 1 1 2
                    " 01280019 01" 1 . 1 . 3 . 2018 33 1 0 1 1 1 5 3 0 1 1
                    " 01280020 02" 1 . 1 . 35 . 2018 29 1 1 1 1 1 5 6 0 1 1
                    " 01300006 02" 2 1 2 2 34 54 2018 25 1 1 1 1 1 4 4 2 0 2
                    " 01300009 02" 3 2 2 2 59 . 2018 38 0 1 1 1 1 3 24 3 0 3
                    " 01300010 02" 2 1 1 1 36 . 2018 29 1 1 1 1 1 4 18 0 2 2
                    " 01300015 02" 1 . 2 . 5 . 2018 25 1 0 1 1 1 4 5 1 0 1
                    " 01310014 02" 1 . 1 . 13 . 2018 24 1 0 1 1 1 4 5 0 1 1
                    " 01310015 02" 3 2 2 2 0 24 2018 30 0 0 1 1 1 3 1 2 1 3
                    " 01310019 04" 1 . 1 . 45 . 2018 28 0 1 1 1 1 2 12 0 1 1
                    " 01320011 02" 2 1 1 1 41 . 2018 35 1 1 1 1 1 5 24 0 2 2
                    " 01330003 02" 1 . 2 . 35 . 2018 29 1 1 1 1 1 3 11 1 0 1
                    " 01330014 02" 2 1 2 1 8 54 2018 31 1 1 1 1 1 3 8 1 1 2
                    " 01330016 02" 4 3 1 1 20 52 2018 24 0 0 1 1 1 1 20 0 4 4
                    " 01340006 02" 3 2 2 2 59 . 2018 30 1 1 1 1 1 5 24 2 1 3
                    " 01340009 02" 2 1 2 2 6 42 2018 23 1 1 1 1 1 4 6 2 0 2
                    " 01340010 02" 2 1 1 2 48 . 2018 32 0 0 1 1 1 3 18 1 1 2
                    " 01340011 02" 2 1 2 1 37 . 2018 28 1 0 1 1 1 5 24 1 1 2
                    " 01340012 02" 3 2 2 1 26 43 2018 26 0 1 1 1 1 2 26 1 2 3
                    " 01360001 02" 1 . 1 . 34 . 2018 23 1 1 1 1 1 3 18 0 1 1
                    " 01360002 02" 4 3 2 2 8 46 2018 28 0 0 1 1 1 3 8 3 1 4
                    " 01360007 02" 2 1 1 1 39 . 2018 38 1 1 1 1 1 3 18 0 2 2
                    " 01360009 02" 1 . 2 . 26 . 2018 21 0 0 1 1 1 2 2 1 0 1
                    " 01360011 02" 1 . 2 . 10 . 2018 28 1 0 1 1 1 3 10 1 0 1
                    " 01360021 02" 2 1 1 2 33 . 2018 34 1 0 1 1 1 3 6 1 1 2
                    " 01370016 02" 3 2 2 2 2 18 2018 40 1 1 1 1 1 5 2 2 1 3
                    " 01380008 02" 2 1 1 1 18 . 2018 35 1 1 1 1 1 5 18 0 2 2
                    " 01380009 02" 2 1 1 2 55 . 2018 33 1 1 1 1 1 5 24 1 1 2
                    " 01380012 01" 3 2 1 1 21 . 2018 37 0 1 1 1 1 5 7 1 2 3
                    " 01380021 01" 3 2 1 1 4 . 2018 34 1 0 1 1 1 5 4 1 2 3
                    " 01390012 02" 4 3 2 2 28 . 2018 32 0 0 1 1 1 2 20 4 0 4
                    " 01400021 02" 3 2 1 2 26 . 2018 37 0 1 1 1 1 3 26 2 1 3
                    " 01430012 02" 2 1 1 1 56 . 2018 38 0 1 1 1 1 4 24 0 2 2
                    " 01430014 04" 1 . 1 . 23 . 2018 23 1 1 1 1 1 3 6 0 1 1
                    " 01440009 02" 1 . 1 . 2 . 2018 37 1 0 1 1 1 4 2 0 1 1
                    " 01440015 02" 3 2 2 2 30 . 2018 41 1 1 1 1 1 5 24 3 0 3
                    " 01440016 02" 4 3 1 1 1 . 2018 37 1 1 1 1 1 4 1 1 3 4
                    " 01440017 02" 3 2 1 2 7 29 2018 28 1 1 1 1 1 4 7 2 1 3
                    " 01450021 02" 2 1 1 2 29 . 2018 26 1 1 1 1 1 3 29 1 1 2
                    " 01460002 02" 2 1 1 2 28 54 2018 25 0 0 1 1 1 3 12 1 1 2
                    " 01460003 02" 2 1 2 1 56 . 2018 35 1 0 1 1 1 5 27 1 1 2
                    " 01460005 02" 3 2 2 2 54 . 2018 41 0 0 1 1 1 3 24 2 1 3
                    " 01460009 02" 1 . 2 . 34 . 2018 25 1 0 1 1 1 3 24 1 0 1
                    " 01460015 02" 3 2 1 2 39 . 2018 31 0 0 1 1 1 5 30 2 1 3
                    " 01480011 02" 2 1 2 2 49 . 2018 40 1 1 1 1 1 5 6 2 0 2
                    " 01490005 04" 2 1 1 2 0 14 2018 18 0 1 1 1 1 2 1 1 1 2
                    end
                    label values B4_01 B4_01
                    label def B4_01 1 "Male", modify
                    label def B4_01 2 "Female", modify
                    label values B4_02 B4_02
                    label def B4_02 1 "Male", modify
                    label def B4_02 2 "Female", modify
                    label values completionof8years completionof8years
                    label def completionof8years 0 "Not Completed", modify
                    label def completionof8years 1 "Completed", modify
                    label values mode_of_delivery mode_of_delivery
                    label def mode_of_delivery 0 "Vaginal Birth", modify
                    label def mode_of_delivery 1 "Ceasarean", modify
                    label values region5 region5
                    label def region5 1 " West", modify
                    label values region12 region12
                    label def region12 1 "Istanbul", modify
                    label values rural_urban rural_urban
                    label def rural_urban 1 "Urban", modify
                    label def rural_urban 2 "Rural", modify
                    label values wealth_index wealth_index
                    label def wealth_index 1 "Poorest", modify
                    label def wealth_index 2 "Poorer", modify
                    label def wealth_index 3 "Middle", modify
                    label def wealth_index 4 "Rich", modify
                    label def wealth_index 5 "Richest", modify
                    
                    
                    
                    reshape long childsex HW1 B4 BORD, i(CASEID) j(_j) string
                    destring _j, ignore("_") replace
                    
                    levelsof BORD, local(bos)
                    foreach bo of local bos {
                    summ malebirths if BORD == `bo', meanonly
                    local numerator `r(sum)'
                    summ totalbirths if BORD == `bo', meanonly
                    local denominator `r(sum)'
                    display "Male fraction for birth order " `bo' " = " `numerator'/`denominator'
                    }
                    
                    by BORD, sort: egen male_fraction = mean(HW1 == 1)
                    
                    // NOW REMOVE bfduration2 FROM OBSERVATIONS NOT PURTAINING TO YOUNGEST CHILD
                    by CASEID (HW1): replace bfduration2 = . if _n > 1
                    This is essentially the same code as before, with the names of the variables changed to reflect your -dataex- example's. The only thing truly new is the final command which eliminates the values of bfduration2 for all but the youngest child. (It would also have been possible to revise the code so that those extra copies of bfduration2 never arose in the first place, but that way required slightly longer, more complicated code.)

                    Comment

                    Working...
                    X