Announcement

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

  • Finding Max and Storing For Each Group

    My objective is to find the max of a certain variable for each group and then assign generate for every observation in a particular group a new variable that equals the max. So that means for every observation in the group, the max variable would have the same value.

    Code:
    unique_identifier group value
    56 1 2048
    12 1 5005
    78 1 7080
    46 2 9898
    82 2 6568
    97 2 7000
    
    //After the code the above should look like the following:
    
    unique_identifier group value num_max
    56 1 2048 7080
    12 1 5005 7080
    78 1 7080 7080
    46 2 9898 9898
    82 2 6568 9898
    97 2 7000 9898
    The code I have been trying to use is the following:

    Code:
    summarize counter
    bysort group: gen num_max = r(max)
    Of course the problem with this is that r(max) is still fixed at the value of summarize, and the summarize isn't being repeated for each group.

  • #2
    If there are no missings on your variable, this is the most direct way:

    Code:
     
    bysort group (counter) : gen num_max = counter[_N]
    To ignore missings, egen is helpful:

    Code:
    egen num_max = max(counter), by(group)

    Comment


    • #3
      family_no subject case mom dad first sibling last
      10096802 03 0 0 1 0 0 1
      10096802 01 1 0 0 1 0 0
      10197151 02 0 1 0 0 0 0
      10197151 01 1 0 0 1 0 0
      10197151 03 0 0 1 0 0 1
      I have my data in this format. I would like to generate a variable for dyads if only (case & mom) or (case and dad ) are present, a variable trio if case, mom and dad are present and a quad variable if case, mom, dad and sibling are present. The variables first and last indicate the eldset member of a family indicated by the family_no. For the variable subject 01 denotes a case, 02 a mom and 30 a dad. Please help.


      Comment


      • #4
        Code:
        * Example generated by -dataex-. For more info, type help dataex
        clear
        input long family_no byte(subject case mom dad first sibling last)
        10096802 3 0 0 1 0 0 1
        10096802 1 1 0 0 1 0 0
        10197151 2 0 1 0 0 0 0
        10197151 1 1 0 0 1 0 0
        10197151 3 0 0 1 0 0 1
        end
        
        bys family_no: egen wanted1= max((case&mom)|(case&dad))
        bys family_no: egen wanted2= max(case&mom&dad)
        and so on. For a 0/1 variable, the condition is evaluated true if 1 and false if 0. The operator for "and" is & and "or" is |. See

        Code:
        help operators
        Otherwise, also see https://www.stata.com/support/faqs/d...ble-recording/.


        Res,:

        Code:
        
        . l, sepby(fam)
        
             +------------------------------------------------------------------------------------+
             | family~o   subject   case   mom   dad   first   sibling   last   wanted1   wanted2 |
             |------------------------------------------------------------------------------------|
          1. | 10096802         3      0     0     1       0         0      1         0         0 |
          2. | 10096802         1      1     0     0       1         0      0         0         0 |
             |------------------------------------------------------------------------------------|
          3. | 10197151         2      0     1     0       0         0      0         0         0 |
          4. | 10197151         1      1     0     0       1         0      0         0         0 |
          5. | 10197151         3      0     0     1       0         0      1         0         0 |
             +------------------------------------------------------------------------------------+
        Last edited by Andrew Musau; 16 Sep 2021, 04:35.

        Comment

        Working...
        X