Announcement

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

  • Within groups- identifying when the value of one variable is EVER equal to the value of another variable within that group.

    Hi,

    I have a data set which contains information on adults (PERSON = 1/9).

    Adults can be carers or non-carers (CARER = YES or NO).

    The carers are either caring for someone within their household (NEEDPER = 1/9) , or for a parent living outside the household (NEEDPER == 15).

    Carers can be caring for more than one person.

    Carers can be receiving care themselves.

    I am trying to generate a variable which will tell me if the person who is providing care is also receiving care.

    The data are set up something like this:

    HOUSEHOLD_ID PERSON NEEDPER CARER
    1 1 2 YES
    2 1 . NO
    3 1 2 YES
    3 1 3 YES
    4 1 2 YES
    4 2 1 YES
    5 1 2 YES
    5 2 15 YES
    5 1 15 YES

    So I want to know when within a household, does PERSON ever equal NEEDPER. This will tell me if the PERSON who gives care, is also receiving care.

    For example, in HOUSEHOLD_ID 4. PERSON 1 provides care to PERSON 2. But PERSON 2 also PROVIDES care to PERSON 1. Hence PERSON 1 is equal to NEEDPER within the household and vice versa.
    Also, in HOUSEHOLD_ID 5 PERSON 1 provides care to PERSON 2 and PERSON 2 provides care to PERSON 15 (a parent living outside the household). Hence, PERSON 2 appears as the PERSON and the NEEDPER in HOUUSEHOLD_ID 5.

    How can I generate a variable which will identify if the PERSON is ever the NEEDPER within that household?

    Any advice is much appreciated.
    Best wishes,

    Elizabeth

  • #2
    Try this code:
    Code:
    gen memhelp=0
    levelsof PERSON, local(levs)
    foreach l of local levs {
    bys HOUSEOLD_ID: gen pnh`l'=NEEDPER==`l'
    bys HOUSEOLD_ID: gen needhelp`l'=sum(pnh`l')
    replace memhelp=1 if PERSON==`l' & needhelp`l'>0
    drop pnh`l' needhelp`l'
    }

    Comment


    • #3
      We can follow up the idea that (1, 2) is the same pair of people as (2, 1) by realising that both sort to (1, 2). Hence we can just look for duplicates of that sorted pair within each household.

      Code:
      clear
      input HOUSEHOLD_ID PERSON NEEDPER str3 CARER
      1 1 2 YES
      2 1 . NO
      3 1 2 YES
      3 1 3 YES
      4 1 2 YES
      4 2 1 YES
      5 1 2 YES
      5 2 15 YES
      5 1 15 YES
      end
      gen pair = string(min(PERSON, NEEDPER)) + " " + string(max(PERSON, NEEDPER))
      bysort HOUSEHOLD_ID pair : gen same = _N > 1
      egen same_in_hh = max(same), by(HOUSEHOLD_ID)
      
      list, sepby(HOUSEHOLD_ID)
      
           +--------------------------------------------------------------+
           | HOUSEH~D   PERSON   NEEDPER   CARER   pair   same   same_i~h |
           |--------------------------------------------------------------|
        1. |        1        1         2     YES    1 2      0          0 |
           |--------------------------------------------------------------|
        2. |        2        1         .      NO    1 1      0          0 |
           |--------------------------------------------------------------|
        3. |        3        1         2     YES    1 2      0          0 |
        4. |        3        1         3     YES    1 3      0          0 |
           |--------------------------------------------------------------|
        5. |        4        2         1     YES    1 2      1          1 |
        6. |        4        1         2     YES    1 2      1          1 |
           |--------------------------------------------------------------|
        7. |        5        1        15     YES   1 15      0          0 |
        8. |        5        1         2     YES    1 2      0          0 |
        9. |        5        2        15     YES   2 15      0          0 |
           +--------------------------------------------------------------+
      See also

      http://www.stata-journal.com/sjpdf.h...iclenum=dm0043

      http://www.stata.com/support/faqs/da...ble-recording/

      for discussions of the main tricks here.

      Comment


      • #4
        I'm not entirely sure I understand you want. For example, in household 4, shouldn't both person 1 and person 2 be designated as a person who is also receiving care? I'm going to assume the answer is yes. So try this:

        Code:
        * Example generated by -dataex-. To install: ssc install dataex
        clear
        input byte(household_id person needper) str3 carer
        1 1  2 "YES"
        2 1  . "NO"
        3 1  2 "YES"
        3 1  3 "YES"
        4 1  2 "YES"
        4 2  1 "YES"
        5 1  2 "YES"
        5 2 15 "YES"
        5 1 15 "YES"
        end
        
        preserve
        
        tempfile recipients
        keep household_id needper
        drop if missing(needper)
        duplicates drop
        rename needper person
        save `recipients'
        
        restore
        merge m:1 household_id person using `recipients', keep(master match)
        gen byte wanted = (_merge == 3)
        For future reference, the preferred way to show example data is with the -dataex- command, as I have done above. If you do not already have it, run -ssc install dataex- and then follow the simple directions in -help dataex- to use it. By using -dataex- you will make it possible for those who want to work with your data to answer your question quickly and easily create a faithful replication of your example in full detail.

        Added: Crossed in cyberspace with #2 and #3.
        Last edited by Clyde Schechter; 10 Mar 2016, 10:51.

        Comment

        Working...
        X