Announcement

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

  • generating a new variable based on 2 conditions

    hello there
    new stata user

    i have a dataset that look like this

    date_m com date com_ret Date com_cap smb_group groupmean
    2018m12 Comp710 12/31/2018 .0148 12/31/2018 52757 1 .0102609
    2018m12 Comp712 12/31/2018 .0675 12/31/2018 45732 1 .0102609
    2018m12 Comp664 12/31/2018 -.0437 12/31/2018 74315 1 .0102609
    2018m12 Comp682 12/31/2018 -.1154 12/31/2018 37400 1 .0102609
    2018m12 Comp708 12/31/2018 -.0226 12/31/2018 64900 1 .0102609
    2018m12 Comp535 12/31/2018 .0716 12/31/2018 69545 1 .0102609
    2018m12 Comp399 12/31/2018 -.0428 12/31/2018 62014 1 .0102609
    2018m12 Comp735 12/31/2018 .0709 12/31/2018 36048 1 .0102609
    2018m12 Comp679 12/31/2018 .1521 12/31/2018 64328 1 .0102609
    2018m12 Comp652 12/31/2018 .0615 12/31/2018 40797 1 .0102609
    2018m12 Comp709 12/31/2018 .1286 12/31/2018 61920 1 .0102609
    2018m12 Comp651 12/31/2018 -.017 12/31/2018 74000 1 .0102609
    2018m12 Comp581 12/31/2018 -.0494 12/31/2018 40706 1 .0102609
    2018m12 Comp562 12/31/2018 .1139 12/31/2018 36962 1 .0102609
    2018m12 Comp419 12/31/2018 .0445 12/31/2018 44685 1 .0102609
    2018m12 Comp744 12/31/2018 -.0351 12/31/2018 23202 1 .0102609
    2018m12 Comp592 12/31/2018 .0172 12/31/2018 78455 1 .0102609
    2018m12 Comp696 12/31/2018 .5328 12/31/2018 75600 1 .0102609
    2018m12 Comp753 12/31/2018 .0586 12/31/2018 27768 1 .0102609
    2018m12 Comp691 12/31/2018 .0142 12/31/2018 50693 1 .0102609
    2018m12 Comp588 12/31/2018 .1562 12/31/2018 28186 1 .0102609
    2018m12 Comp688 12/31/2018 -.045 12/31/2018 67505 1 .0102609
    2018m12 Comp620 12/31/2018 -.0985 12/31/2018 63988 1 .0102609
    2018m12 Comp742 12/31/2018 .2919 12/31/2018 49545 1 .0102609
    2018m12 Comp332 12/31/2018 -.024 12/31/2018 80563 1 .0102609
    2018m12 Comp571 12/31/2018 -.0778 12/31/2018 38241 1 .0102609
    2018m12 Comp613 12/31/2018 .3841 12/31/2018 69165 1 .0102609
    2018m12 Comp724 12/31/2018 .0464 12/31/2018 62542 1 .0102609
    2018m12 Comp431 12/31/2018 .015 12/31/2018 80312 1 .0102609
    2018m12 Comp668 12/31/2018 .0361 12/31/2018 74600 1 .0102609


    i`ve generated the mean of returns based on groups (5 groups)
    now I want to get the difference of groupmean of group 1 and group 5 only
    but I can`t comp up with the code for this


    here`s what should be done
    1- sorted data by date (monthly)
    2- keep the data sorted by groups as well (1 to 5)
    3- get the difference of two numbers. let`s call these two numbers A and B
    => A is the groupmean for group 1 for the first month
    => B is the groupmean for group 5 for the first month
    => A-B is the same for the first month

    repeat this process for the whole data (Monthly)

    I hope I explained well

    TIA

  • #2
    You say you've generated the means based on 5 groups, but you seem to mean you've generated the means for each combination of group and month, and you want the differenence in each month of the means for the first and fifth groups.

    Perhaps this untested code will start you in a useful direction.
    Code:
    sort date_m group
    by date_m: egen mean1 = max(cond(smb_group==1,groupmean,.)
    by date_m: egen mean5 = max(cond(smb_group==5,groupmean,.)
    by date_m: generate diff15 = mean1-mean5

    Comment

    Working...
    X