Announcement

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

  • Giving a group of observations the trait of one

    Hi Statalist;

    I am working with a dataset that has households identified by a household id # and then family members uniquely identified by that household id # and a line number for their place in the household.

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input double tucaseid byte(tulineno teage)
    20030100013848 1 36
    20030100013848 2 39
    20030100013848 3 11
    20030100013848 4  9
    20030100014165 1 51
    20030100014165 2 50
    20030100014165 3 14
    20030100014165 4 14
    20030100014550 1 33
    20030100014550 2 39
    20030100014550 3 15
    20030100014550 4 14
    20030100014550 5  3
    end
    My question is: if some number of individuals in my whole dataset have a problem, and I want to flag their whole household as containing this issue so I can create a dataset of all individuals of a household where a member has this issue, what is the fastest way to do this?

    In other words, after I generate problem = 1 for person 2 in household x, how do I mark everyone in household x as having problem = 1? (And, simultaneously, do that for every relevant household in this large dataset). So far I've tried using merge and then I tried collapse and merge, but with both attempts I just got turned around by having so many merges as my problem also originates in a merge... surely there's an easy way to do this?

    Many thanks for your help!

  • #2
    there are several ways to do this; here is one:
    Code:
    gen byte problem=1 if ...
    egen maxprob=max(problem), by(household)
    you will need to use your variable names (which were not clear to me) and you will need to fill in the definition of how you id the problem where i have jut left "..."
    Last edited by Rich Goldstein; 07 Apr 2017, 08:34. Reason: edit to fix type

    Comment


    • #3
      FAQ http://www.stata.com/support/faqs/da...ble-recording/

      Specifically

      any <-> max over logical or indicator variables

      all <-> min over such

      Code:
      clear
      input double tucaseid byte(tulineno teage)
      20030100013848 1 36
      20030100013848 2 39
      20030100013848 3 11
      20030100013848 4  9
      20030100014165 1 51
      20030100014165 2 50
      20030100014165 3 14
      20030100014165 4 14
      20030100014550 1 33
      20030100014550 2 39
      20030100014550 3 15
      20030100014550 4 14
      20030100014550 5  3
      end
      
      gen problem = 1 in 2
      egen hhproblem = max(problem), by(tucaseid)
      
      list , sepby(tucaseid)
      
      
           +---------------------------------------------------+
           |  tucaseid   tulineno   teage   problem   hhprob~m |
           |---------------------------------------------------|
        1. | 2.003e+13          1      36         .          1 |
        2. | 2.003e+13          2      39         1          1 |
        3. | 2.003e+13          3      11         .          1 |
        4. | 2.003e+13          4       9         .          1 |
           |---------------------------------------------------|
        5. | 2.003e+13          1      51         .          . |
        6. | 2.003e+13          2      50         .          . |
        7. | 2.003e+13          3      14         .          . |
        8. | 2.003e+13          4      14         .          . |
           |---------------------------------------------------|
        9. | 2.003e+13          1      33         .          . |
       10. | 2.003e+13          2      39         .          . |
       11. | 2.003e+13          3      15         .          . |
       12. | 2.003e+13          4      14         .          . |
       13. | 2.003e+13          5       3         .          . |
           +---------------------------------------------------+
      Don't be distracted by the poor default format for the identifiers. Presumably in your real data you have a sensible one.

      EDIT: The Rich beat the poor. So, what else is new? (Don't worry, we are friends and even co-authors. (The two often don't go together.))

      Comment


      • #4
        Thank you to you both!! A huge help.

        Comment

        Working...
        X