Announcement

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

  • Linking parents with children in Stata

    Dear All,

    I'm trying to connect parents attributes like education and BMI to child on Stata 16. My commands are as follows:

    Code:
    by hhid (pid), sort: gen pid1=_n
    gen empl_f=.
    gen empl_m=.
    gen educ_f=.
    gen educ_m=.
    gen wt_f=.
    gen wt_m=.
    sum pid1, meanonly
    forval i =  1/`r(max)' {
    However, when I get to
    Code:
    forval i =  1/`r(max)' {
    command, I get an r(612) error message stating the following

    unexpected end of file.

    Please advise if there is something that I'm missing. I read the meaning of the error message and tried the suggestions of closing the bracket, but this is not correct.

    Thanks
    Nthato

  • #2
    What comes on the subsequent lines of your do-file after -forval i = 1/`r(max)' {-?

    If you are running these commands out of a do-file and that is the last command in the do-file, and this is all there is (or this is all that has been selected) then the error message is appropriate: the end of the do-file has been reached at a point where there should be more code following, but instead the end of file is found.

    That said, in Stata, when you find yourself writing a loop over observations, as here, you are probably heading down the wrong path. Occasionally this is needed in Stata, but it is very uncommon. And usually when the issue is linking data values between parents and children or other household members, there are ways to do this using linked frames or merging data sets. If you post back showing example data that contains observations on some corresponding parents and children, we could show you better approaches to this. When showing example data, please be sure to use the -dataex- command. 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 Clyde.

      My dataex results are as follows:

      Code:
      * Example generated by -dataex-. To install: ssc install dataex
      clear
      input long(hhid pid) float(empl_f empl_m educ_f educ_m wt_f wt_m)
      101012 314585 . . . . . .
      101013 314544 . . . . . .
      101013 314550 . . . . . .
      101013 406295 . . . . . .
      101013 406297 . . . . . .
      101014 314575 . . . . . .
      101014 314581 . . . . . .
      101014 314582 . . . . . .
      101015 314570 . . . . . .
      101015 406352 . . . . . .
      101016 314110 . . . . . .
      101017 314529 . . . . . .
      101017 314531 . . . . . .
      101020 314543 . . . . . .
      101020 406291 . . . . . .
      101020 406293 . . . . . .
      101020 407218 . . . . . .
      101021 314567 . . . . . .
      101021 314568 . . . . . .
      101021 406324 . . . . . .
      101021 406325 . . . . . .
      101021 406330 . . . . . .
      101022 314558 . . . . . .
      101022 314559 . . . . . .
      101022 403744 . . . . . .
      101022 406326 . . . . . .
      101022 406327 . . . . . .
      101022 406329 . . . . . .
      101023 314523 . . . . . .
      101023 314569 . . . . . .
      101023 314573 . . . . . .
      101023 406331 . . . . . .
      101023 406332 . . . . . .
      101023 406334 . . . . . .
      101023 406335 . . . . . .
      101024 314555 . . . . . .
      101024 314556 . . . . . .
      101024 314572 . . . . . .
      101024 406343 . . . . . .
      101025 314512 . . . . . .
      101025 314517 . . . . . .
      101025 314527 . . . . . .
      101025 314528 . . . . . .
      101025 314586 . . . . . .
      101025 314587 . . . . . .
      101025 406289 . . . . . .
      101025 406345 . . . . . .
      101025 406353 . . . . . .
      101025 406371 . . . . . .
      101025 406372 . . . . . .
      101028 313832 . . . . . .
      101028 313837 . . . . . .
      101028 314553 . . . . . .
      101028 314564 . . . . . .
      101028 314565 . . . . . .
      101028 314566 . . . . . .
      101028 406306 . . . . . .
      101028 406361 . . . . . .
      101028 406362 . . . . . .
      101028 406363 . . . . . .
      101028 406364 . . . . . .
      101028 406365 . . . . . .
      101028 407235 . . . . . .
      101028 410018 . . . . . .
      101029 314557 . . . . . .
      101029 406339 . . . . . .
      101029 406340 . . . . . .
      101030 314505 . . . . . .
      101030 406342 . . . . . .
      101030 406348 . . . . . .
      101030 407203 . . . . . .
      101033 314157 . . . . . .
      101033 314158 . . . . . .
      101033 406344 . . . . . .
      101034 314174 . . . . . .
      101034 314186 . . . . . .
      101034 314521 . . . . . .
      101034 314522 . . . . . .
      101034 407257 . . . . . .
      101034 407258 . . . . . .
      101034 407259 . . . . . .
      101036 314133 . . . . . .
      101036 314134 . . . . . .
      101036 314135 . . . . . .
      101036 407219 . . . . . .
      101036 407220 . . . . . .
      101036 407221 . . . . . .
      101036 407223 . . . . . .
      101037 314131 . . . . . .
      101037 314137 . . . . . .
      101037 314138 . . . . . .
      101037 314141 . . . . . .
      101037 407222 . . . . . .
      101037 407225 . . . . . .
      101037 407239 . . . . . .
      101037 407240 . . . . . .
      101037 407241 . . . . . .
      101037 407248 . . . . . .
      101038 314122 . . . . . .
      101038 407255 . . . . . .
      end
      Regards
      Nthato

      Comment


      • #4
        The commands that should follow are s follows:

        Code:
        by hhid: replace empl_f=employ_stat[`i'] if fthpid==pid[`i'] & !missing(best_fthpid)
        by hhid: replace empl_m=empl_stat[`i'] if  mthpid==pid[`i'] & !missing(best_mthpid)
        
        by hhid: replace educ_f=education[`i'] if fthpid==pid[`i'] & !missing(best_fthpid)
        by hhid: replace educ_m=education[`i'] if  mthpid==pid[`i'] & !missing(best_mthpid)
        
        by hhid: replace wt_f=bmi[`i'] if fthpid==pid[`i'] & !missing(best_fthpid)
        by hhid: replace wt_m=bmi[`i'] if  mthpid==pid[`i'] & !missing(best_mthpid)

        Comment


        • #5
          Thank you for responding. But the problem cannot be solved with the data example you provided. In that example data set, there is no way to know who, within a household, is the father and who is the mother. From the commands you show in #4 (which, by the way, will not give correct results in any case) it appears that the full data set actually contains variables fthpid and mthpid that provide this information. Please post a new data example that contains those variables (or other variables that designate who is the father and who is the mother of whom.) Also, for the data example to be usable, it has to include the variables empl_stat, educ, and bmi.

          Comment


          • #6
            Hi Nick,

            fthpid and mthpid are given in the data set. I just need to connect mothers education, employment status and BMI to child.

            So to include all variables I said

            Code:
            by hhid (pid) fthpid mthpid empl_stat education bmi , sort: gen pid2=_n
            The dataex results are

            Code:
            * Example generated by -dataex-. To install: ssc install dataex
            clear
            input long(hhid pid fthpid mthpid) byte(empl_stat education) float bmi
            101012 314585      .      . 0 25  30.86253
            101013 314544      .      . 0 25 19.287205
            101013 314550      .      . 0 25 23.121824
            101013 406295 314544 314550 .  2         .
            101013 406297 314544 314550 .  3         .
            101014 314575 314582 314580 .  4         .
            101014 314581 314582 314580 0  8  25.71166
            101014 314582      .      . 0 25 22.857143
            101015 314570      .      . 0  5   19.7551
            101015 406352      . 314570 .  2         .
            101016 314110      .      . 3 25 31.221025
            101017 314529      .      . 1 25 19.959356
            101017 314531      .      . 1 25 17.892626
            101020 314543      .      . 3  4 29.136316
            101020 406291      . 314543 . 25         .
            101020 406293      . 314543 .  1         .
            101020 407218      . 314543 .  1         .
            101021 314567      .      . 3  6  27.47957
            101021 314568      . 314567 0  9 22.337494
            101021 406324      . 314567 .  1         .
            101021 406325      . 720007 .  0         .
            101021 406330      . 720007 . 25         .
            101022 314558      .      . 3 25  19.43852
            101022 314559 314558 314560 0  8  23.51697
            101022 403744 314558 314560 .  6         .
            101022 406326 314558 314560 .  0         .
            101022 406327 314558 314560 . 25         .
            101022 406329 314558 314560 .  4         .
            101023 314523      . 314573 3 25  30.11152
            101023 314569      .      . 3 25   24.5632
            101023 314573      .      . 0 25 37.109375
            101023 406331 314523 314569 .  3         .
            101023 406332 314523 314569 .  2         .
            101023 406334 314523 314569 . 25         .
            101023 406335 314523 314569 . 25         .
            101024 314555      .      . 0  6  21.49346
            101024 314556      .      . 3  6 20.218273
            101024 314572      .      . 1  6  20.99592
            101024 406343      .      . .  4         .
            101025 314512      .      . 1  6  22.76985
            101025 314517      .      . 0 25 33.547276
            101025 314527      .      . 3  7 24.609734
            101025 314528 314515 314517 0  8  22.67265
            101025 314586 314515      . 1  5 18.982437
            101025 314587 314515      . 3  3 19.979753
            101025 406289      .      . .  1         .
            101025 406345 314515 314517 .  3         .
            101025 406353      . 314512 .  6         .
            101025 406371 314515 314517 .  6         .
            101025 406372      .      . .  1         .
            101028 313832      .      . 0  8 17.149416
            101028 313837      .      . 0 25  23.05878
            101028 314553      .      . 3  9  20.90263
            101028 314564 776516 788147 0  9 24.012947
            101028 314565      . 720013 3  8  20.49311
            101028 314566      .      . 0 25  23.73349
            101028 406306      . 724583 .  0         .
            101028 406361      . 720013 .  6         .
            101028 406362      . 720013 .  4         .
            101028 406363      .      . .  1         .
            101028 406364      . 720013 . 25         .
            101028 406365      . 724583 . 25         .
            101028 407235      .      . . 25         .
            101028 410018 314552 314564 . 25         .
            101029 314557 314520 314541 0  7 24.609375
            101029 406339 314544      . . 25         .
            101029 406340      . 314557 . 25         .
            101030 314505      . 314504 0 11 19.693207
            101030 406342      . 314505 . 25         .
            101030 406348      . 314505 .  8         .
            101030 407203      . 314505 .  0         .
            101033 314157      . 314158 0  8  20.36029
            101033 314158      .      . 0 25 16.527958
            101033 406344      . 314119 . 25         .
            101034 314174      . 314522 0  6  26.19754
            101034 314186      . 314522 0 19      27.6
            101034 314521      . 314522 2 12 36.837288
            101034 314522      .      . 3  2  33.84577
            101034 407257      . 314522 .  4         .
            101034 407258 765705 314186 .  0         .
            101034 407259      . 314522 .  6         .
            101036 314133      . 314135 1 11 30.915577
            101036 314134      . 314135 2 12  28.76397
            101036 314135      .      . 0  4  32.05128
            101036 407219      . 314134 . 25         .
            101036 407220      . 314134 .  1         .
            101036 407221      . 314133 . 25         .
            101036 407223      . 314133 .  0         .
            101037 314131 314137 314141 1 10 26.859505
            101037 314137      .      . 3  9  25.71101
            101037 314138 314137 314141 0  7 21.644444
            101037 314141      .      . 0  7   38.7702
            101037 407222      . 314131 .  0         .
            101037 407225      . 314131 .  0         .
            101037 407239 314137 314141 .  3         .
            101037 407240 314137 314141 .  6         .
            101037 407241 314137 314141 .  3         .
            101037 407248 314137 314141 .  0         .
            101038 314122      .      . 0  8  26.52501
            101038 407255      . 314122 .  3         .
            end
            label values mthpid w5_id_codes
            label values fthpid w5_id_codes
            label values empl_stat state_fine_ap
            label def state_fine_ap 0 "0. Not Economically Active", modify
            label def state_fine_ap 1 "1. Unemployed_Discouraged", modify
            label def state_fine_ap 2 "2. Unemployed_Strict", modify
            label def state_fine_ap 3 "3. Employed", modify
            label values education w5_edgrd
            label def w5_edgrd 0 "0. Grade R/0", modify
            label def w5_edgrd 1 "1. Grade 1 (Previously Sub A/Class 1)", modify
            label def w5_edgrd 2 "2. Grade 2 (Previously Sub B/Class 2)", modify
            label def w5_edgrd 3 "3. Grade 3 (Std. 1)", modify
            label def w5_edgrd 4 "4. Grade 4 (Std. 2)", modify
            label def w5_edgrd 5 "5. Grade 5 (Std. 3)", modify
            label def w5_edgrd 6 "6. Grade 6 (Std. 4)", modify
            label def w5_edgrd 7 "7. Grade 7 (Std. 5)", modify
            label def w5_edgrd 8 "8. Grade 8 (Std. 6/Form 1)", modify
            label def w5_edgrd 9 "9. Grade 9 (Std. 7/Form 2)", modify
            label def w5_edgrd 10 "10. Grade 10 (Std. 8/Form 3)", modify
            label def w5_edgrd 11 "11. Grade 11 (Std. 9/Form 4)", modify
            label def w5_edgrd 12 "12. Grade 12 (Std. 10/Matric/Senior Certificate/Form 5)", modify
            label def w5_edgrd 19 "19. Diploma requiring Grade 12/Std. 10", modify
            label def w5_edgrd 25 "25. No Schooling", modify



            Comment


            • #7
              Code:
              frame put hhid pid empl_stat education bmi, into(parents)
              
              frame parents: isid hhid pid, sort
              
              frlink m:1 hhid fthpid, frame(parents hhid pid)
              foreach v of varlist empl_stat education bmi {
                  frget fth_`v' = `v', from(parents)
              }
              
              drop parents
              frlink m:1 hhid mthpid, frame(parents hhid pid)
              foreach v of varlist empl_stat education bmi {
                  frget mth_`v' = `v', from(parents)
              }
              drop parents
              frame drop parents

              Comment


              • #8
                Hi Nick,

                Thanks very much for response #7.

                However, I get an error message when I try the 2nd command, [frame parents:isid pid hhid, sort]. The message states

                variables pid hhid should never be missing


                When I check my data, pids are full, and there's missing numbers in hhid. Could this be the reason or is it something else?
                Last edited by Nthato Ras; 10 Jun 2022, 12:04. Reason: Note: Editing because the message somehow repeated #6. Maybe it's because I was posting the same data.

                Comment


                • #9
                  [quote]Could this be the reason or is it something else?[/code]
                  Yes, that would be the reason for it.

                  Now, when I wrote the code, I assumed that, as in many household surveys, the pid numbers do not identify unique people but can be reused in different households. That is, I assumed that the combination of hhid and pid was required to identify a unique person. If that is not true: if pid alone is enough to completely identify one and only one person, then you can work around this by just removing hhid from the code in every command in that code.

                  That said, it is very strange indeed to have an observation in a household survey data set where the hhid is missing. This is very likely to represent an error in your data set. So you should investigate how this happened, and fix whatever problems led to it, before proceeding with any further data management or analysis.

                  By the way, Nick didn't respond on this thread. He and I tend to think alike and often give similar answers (though I am much less succinct in my wording). But we are two different people.

                  Comment


                  • #10
                    Thank you so much Clyde. Let me explore this. I will give you an update soon.

                    Sorry to call you Nick

                    Comment


                    • #11
                      Thank you Clyde. The commands were very helpful!

                      Comment

                      Working...
                      X