Announcement

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

  • Assign same value on variable as mother

    Hi!
    I am terribly sorry about my inability to perform this task, but I cannot get it to work or find a reasonable solution online.

    I am performing a regression discontinuity design where I study the effect of growing up with parents on a full vs. reduced unemployment benefit scheem. I have identified parents in the treatment and control group and created the variable treatment (0=control, 1=treatment). Each child has a personID, a motherID and a fatherID. I wish to assign the child the treatment=1, if one of the parents is located in the treatment group (treatment=1) and treatment=0 if both parents are assigned to the control group (treatment=0).
    PersonID MotherID FatherID Treatment
    1 1
    2 0
    3 1
    4 1 2 - (1)
    5 3 - - (1)
    I have highligthed the values that I want stata 15.1 to calculate. I am not able to load additional packaged as my job does not allow it for safety reasons.

    Also, I would like to create a new variable family-ID that links children to parents where only one ID (either mother or father) is needed to be part of the family.
    Last edited by Line Bachmann; 31 Jul 2018, 10:47.

  • #2
    Here is a solution to your first problem:

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input byte(personid motherid fatherid treatment)
    1 . . 1
    2 . . 0
    3 . . 1
    4 1 2 .
    5 3 . .
    end
    list, noobs clean
    
    tempfile parents
    save `parents'
    list, noobs clean
    
    mvencode motherid fatherid, mv(9999)
    
    rangejoin personid motherid motherid using `parents', prefix(mom_) keepusing(treatment)
    rangejoin personid fatherid fatherid using `parents', prefix(dad_) keepusing(treatment)
    replace treatment = max(mom_treatment, dad_treatment) if missing(treatment)
    
    drop *_personid
    
    mvdecode motherid fatherid, mv(9999)
    Notes: To use this code you must install Robert Picard's -rangejoin- command, available from SSC. The little dance with missing values in motherid and fatherid is necessary to prevent -rangejoin- from making spurious matches (because inrange(personid, ., .) is always true.) I have used 9999 as the coded missing value here, but you should pick some number that is guaranteed not to occur as a real person id in your dataset. (You could -summ personid- and then use the maximum value plus 1, for example.)

    I do not have a solution to your other problem of creating a family id. I hope somebody else will provide one. It is complicated because of issues of transitivity.

    In the future, when showing data examples, please use the -dataex- command to do so. If you are running version 15.1 or a fully updated version 14.2, it 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.

    When asking for help with code, always show example data. When showing example data, always use -dataex-.

    Comment


    • #3
      Dear Clyde

      Thank you for your quick response. As mentioned before, I am not able to run additional ssc commands not included in stata 15.1 due to data safety restrictions at my workplace. Is there a way to do this without using -rangejoin-? (or -dataex- for that matter - both are blocked)

      Thank you! :-)
      Last edited by Line Bachmann; 31 Jul 2018, 12:11.

      Comment


      • #4
        Try this:

        Code:
        clear
        input byte(personid motherid fatherid treatment)
        1 . . 1
        2 . . 0
        3 . . 1
        4 1 2 .
        5 3 . .
        end
        list, noobs clean
        
        preserve
        keep personid treatment
        rename personid motherid
        rename treatment mom_treat
        save mom_temp.dta, replace
        restore
        
        merge m:1 motherid using mom_temp.dta
        drop if _merge==2
        drop _merge
        
        preserve
        keep personid treatment
        rename personid fatherid
        rename treatment dad_treat
        save dad_temp, replace
        restore
        
        merge m:1 fatherid using dad_temp.dta
        drop if _merge==2
        drop _merge
        
        gen new_treatment=treatment
        replace new_treatment=1 if mom_treat==1 | dad_treat==1
        replace new_treatment=0 if mom_treat==0 & dad_treat==0
        
        sort personid
        list, noobs clean
        Stata/MP 14.1 (64-bit x86-64)
        Revision 19 May 2016
        Win 8.1

        Comment


        • #5
          Carole Wilson has shown you a good solution that can be done without -rangejoin-. As for -dataex-, if you are running version 15.1, it is part of your official Stata installation and does not need to be acquired from SSC.

          -rangejoin- and -rangestat- are extremely useful commands that are likely to prove helpful to you in other contexts as well. I recommend you approach your IT department and ask them to acquire and install those two for you. Most organizations will do that sort of thing.

          Comment


          • #6
            Here's an attempt to create a familyid variable. However, it has some strong assumptions about the data:
            1) mothers & fathers don't have mothers & fathers in the dataset
            2) a father/mother only has children with one mother/father or no mother/father
            (example: father cannot appear with a mother in one child record and appear with a
            different mother or no mother in another child's record)
            3) single parent families are fine, so long as the parents do not appear elsewhere in the dataset with a partner


            I've added a few extra cases so that I could check the conditions:
            personid=9 has parents that are not in the dataset;
            personid=8 & 10 have no motherid or fatherid
            personid=6 is a sibling of 4
            personid=7 is a sibling of 3

            Code:
            clear
            input byte(personid motherid fatherid)
            1 . . 
            2 . . 
            3 . . 
            4 1 2 
            5 3 . 
            6 1 2 
            7 3 . 
            8 . . 
            9 12 11 
            10 . . 
            end
            
            *group kids with same parentage
            egen famid=group(motherid fatherid), miss
            replace famid=. if mi(motherid) & mi(fatherid)
            list, noobs clean
            
            *take motherid and famid from kids
            preserve
            keep motherid famid
            drop if mi(motherid)
            egen tag=tag(motherid)
            drop if tag!=1
            drop tag
            rename motherid personid
            rename famid mfamid
            save mom_famid.dta, replace
            list
            restore
            
            *take fatherid and famid from kids
            preserve
            keep fatherid famid
            drop if mi(fatherid)
            egen tag=tag(fatherid)
            drop if tag!=1
            drop tag
            rename fatherid personid
            rename famid ffamid
            save dad_famid.dta, replace
            restore
            
            *merge motherid (now personid)
            *create mfamid with children's famid for mother
            merge 1:m personid using mom_famid.dta
            drop if _merge==2
            drop _merge
            
            *merge fatherid (now personid)
            *create ffamid with children's famid for father
            merge 1:1 personid using dad_famid.dta
            drop if _merge==2
            drop _merge
            
            *replace famid with mfamid and ffamid
            replace famid=mfamid if mi(famid)
            replace famid=ffamid if mi(famid)
            
            *replace "orphans/nokids" with unique famid
            sort famid personid
            replace famid=_n if mi(famid)
            
            sort famid personid
            list, sepby(famid)
            Stata/MP 14.1 (64-bit x86-64)
            Revision 19 May 2016
            Win 8.1

            Comment


            • #7
              Thank you so much to the both of you! I will give the codes a try and get back to you if it does not solve the problem. :-)

              Comment

              Working...
              X