Announcement

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

  • Data manipulation: Calculating the mean of two or more numbers and putting it back into the data set

    Dear Statalists,

    i generally want to calculate the AAR from AR. In order to do so i have generated a Cluster_ID which match two firms with overlapping event days. Now i want Stata to take the value of AR_MM2 if AAR2_cluster matches and calculate the mean of this and then give it back in another variable. See table for the two different companies, both have the same cluster_id the model adds up their AR_MM2 values and gives back the average to both firms.
    co_id AR_MM2 Cluster_ID AAR2_MM
    1 5 1 3
    2 1 1 3
    How do i calculate this?

    My code did not work:
    Code:
    gen AAR_MM2=AR_MM2 if Cluster_ID_2==.
    by AAR2_cluster: replace AAR_MM2= (sum(AR_MM2)/2)
    Here the dataset for more infos.

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input float(id AR_MM2R AAR2_cluster)
    511            .  1
    113            .  1
    511            .  2
    113            .  2
    511            .  3
    113            .  3
    511            .  4
    113            .  4
    113            .  5
    511            .  5
    113            .  6
    511            .  6
    511            .  7
    113            .  7
    511            .  8
    113            .  8
    113   .005302138  9
    511  -.003760204  9
    113  -.008428667 10
    511  .0040853457 10
    113  -.032759704 11
    511 -.0007772278 11
    113   .014768788 12
    511   .004597272 12
    113  -.018418241 13
    511    .00586128 13
    113            . 14
    511            . 14
    113            . 15
    ...
    558            . 27
    575            . 28
    558            . 28
    575            . 29
    558            . 29
    558 -.0035255456 30
    575   .003658384 30
    575   .007337487 31
    558   .004893153 31
    558   .008426867 32
    575  -.016584055 32
    575   .006924237 33
    558  -.003346003 33
    558   .003431862 34
    575  -.006588891 34
    
    end
    Thank you for your help!
    Best,
    Max

  • #2
    Code:
    bys Cluster_ID: egen AAR2_MM= mean(AR_MM2)

    Comment


    • #3
      Worked perfectly! Thank you.

      Comment

      Working...
      X