Announcement

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

  • Create a new variable based on observations within a group

    Hello,

    my dataset is structered the following:

    clear
    input float (group_id won_close_dummy loss_close_dummy)
    1 0 1
    2 1 0
    2 1 0
    3 1 0
    6 1 0
    7 0 1
    9 0 0
    13 0 1
    13 1 0
    13 0 1
    13 0 1
    17 1 0
    17 0 1
    18 0 1
    20 1 0
    21 1 0
    24 1 0
    24 1 0
    25 0 1
    28 0 1
    33 1 0
    34 1 0
    35 1 0
    38 0 0
    42 1 0
    43 1 0
    43 1 0
    43 1 0
    43 1 0
    43 1 0
    45 1 0
    45 0 1
    46 0 1
    46 1 0
    47 1 0
    47 0 1

    I now want to create a new binary variable "donated" which takes the value of 0 if within a group there is at least one 1 for both "won_close_dummy" and "loss_close_dummy". This means if within a group (eg. group_id ==13 or group_id==17) both variables "won_close_dummy" and "loss_close_dummy" have at least one 1 the new variable "donated" should get a 0 for the whole group.
    If within a group (eg. group_id==2 or group_id==43) only one variable has a 1 the new variable "donated" should get a 1 for the hole group.
    This means it should look like this in the end:

    input float(group_id won_close_dummy loss_close_dummy donated)
    1 0 1 1
    2 1 0 1
    2 1 0 1
    3 1 0 1
    6 1 0 1
    7 0 1 1
    9 0 0 1
    13 0 1 0
    13 1 0 0
    13 0 1 0
    13 0 1 0
    17 1 0 0
    17 0 1 0
    18 0 1 1
    20 1 0 1
    21 1 0 1
    24 1 0 1
    24 1 0 1
    25 0 1 1
    28 0 1 1
    33 1 0 1
    34 1 0 1
    35 1 0 1
    38 0 0 1
    42 1 0 1
    43 1 0 1
    43 1 0 1
    43 1 0 1
    43 1 0 1
    43 1 0 1
    45 1 0 0
    45 0 1 0
    46 0 1 0
    46 1 0 0
    47 1 0 0
    47 0 1 0

    I hope you can help me solving this problem. Thanks in advance!

    Greetings
    Marcel

  • #2
    Thanks for your data example. This should help.

    Each indicator (dummy in your terminology, which I don't favour) being 1 at least once corresponds to a total being at least 1, and so non-zero. So you can combine criteria to get what you want by negating the logical combination (criterion1) & (criterion2) evaluated as 1 (true) if and only if both arguments are true (non-zero).

    Code:
    clear 
    input float(group_id won_close_dummy loss_close_dummy donated)
    1 0 1 1
    2 1 0 1
    2 1 0 1
    3 1 0 1
    6 1 0 1
    7 0 1 1
    9 0 0 1
    13 0 1 0
    13 1 0 0
    13 0 1 0
    13 0 1 0
    17 1 0 0
    17 0 1 0
    18 0 1 1
    20 1 0 1
    21 1 0 1
    24 1 0 1
    24 1 0 1
    25 0 1 1
    28 0 1 1
    33 1 0 1
    34 1 0 1
    35 1 0 1
    38 0 0 1
    42 1 0 1
    43 1 0 1
    43 1 0 1
    43 1 0 1
    43 1 0 1
    43 1 0 1
    45 1 0 0
    45 0 1 0
    46 0 1 0
    46 1 0 0
    47 1 0 0
    47 0 1 0
    end 
    
    egen criterion1 = total( won_close_dummy), by(group_id) 
    egen criterion2 = total(loss_close_dummy), by(group_id) 
    
    gen wanted = !(criterion1 & criterion2) 
    assert wanted == donated
    For more on true and false see https://www.stata.com/support/faqs/d...rue-and-false/

    For why indicator is a better term see https://www.stata-journal.com/articl...article=dm0099

    Comment

    Working...
    X