Announcement

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

  • How to calculate average based on to conditions?

    I have a panel dataset as below and I want to calculate the average:


    Code:
    clear
    input float year1 long TYPE2 float(treat1 FAT_w1 post33 post33treat1)
    2016 2 0 . 0 0
    2016 3 . . 0 .
    2017 3 . 105.86112 0 .
    2018 3 . 169.16325 0 .
    2021 3 . . 1 .
    2019 4 1 . 1 1
    2020 4 1 6.876883 1 1
    2017 6 1 . 0 0
    2018 6 1 2.2230647 0 0
    2019 6 1 2.0798314 1 1
    2020 6 1 1.665593 1 1
    2021 6 1 1.4405484 1 1
    2016 7 1 . 0 0
    2017 7 1 9.328573 0 0
    2018 7 1 14.143867 0 0
    2019 7 1 16.777643 1 1
    2020 7 1 19.836254 1 1
    2021 7 1 13.363945 1 1
    2016 10 0 . 0 0
    2019 10 0 . 1 0
    2020 10 0 10.358336 1 0
    2016 11 0 . 0 0
    2017 11 0 .6820889 0 0
    2018 11 0 .8348646 0 0
    2019 11 0 1.1720647 1 0
    2020 11 0 1.3943592 1 0
    2021 11 0 1.464206 1 0
    2017 13 0 . 0 0
    2018 13 0 14.170573 0 0
    2019 13 0 17.122456 1 0
    2020 13 0 10.125214 1 0
    end
    label values TYPE2 TYPE2
    label def TYPE2 2 "2557XG", modify
    label def TYPE2 3 "2563UY", modify
    label def TYPE2 4 "2563UZ", modify
    label def TYPE2 6 "2580PG", modify
    label def TYPE2 7 "25846A", modify
    label def TYPE2 10 "2621N5", modify
    label def TYPE2 11 "2622UU", modify
    label def TYPE2 13 "2625KH", modify
    I want to calculate the average of FAT_w1 for each year 2016,2017,2018,2019,2020,2021 for each of group treat1=1 and treat1=0.

    For the dataset above, the results should be:
    Average for treat1=1 and year1=2016 is .
    Average for treat1=1 and year1=2017 is 9.328
    Average for treat1=1 and year1=2018 is 8.183
    ...
    ...
    Average for treat1=1 and year1=2021 is 7.4
    Average for treat1=0 and year1=2016 is .
    Average for treat1=0 and year1=2017 is 0.682
    ...
    Average for treat1=0 and year1=2021 is 1.464


    Many thanks and best regards.


  • #2
    Phuc:
    do you mean something along the following lines?

    Code:
    . tabstat FAT_w1 if treat==1, by(year)
    
    Summary for variables: FAT_w1
    Group variable: year1
    
       year1 |      Mean
    ---------+----------
        2016 |         .
        2017 |  9.328573
        2018 |  8.183466
        2019 |  9.428737
        2020 |  9.459577
        2021 |  7.402247
    ---------+----------
       Total |   8.77362
    --------------------
    
    . tabstat FAT_w1 if treat==0, by(year)
    
    Summary for variables: FAT_w1
    Group variable: year1
    
       year1 |      Mean
    ---------+----------
        2016 |         .
        2017 |  .6820889
        2018 |  7.502719
        2019 |   9.14726
        2020 |  7.292636
        2021 |  1.464206
    ---------+----------
       Total |  6.369351
    --------------------
    
    .
    Last edited by Carlo Lazzaro; 16 Sep 2022, 10:00.
    Kind regards,
    Carlo
    (Stata 19.0)

    Comment


    • #3
      Also (Stata 17)

      Code:
      . table year treat1, stat(mean FAT_w1)
      
      -----------------------------------------
              |              treat1            
              |         0          1      Total
      --------+--------------------------------
      year1   |                                
        2016  |         .          .          .
        2017  |  .6820889   9.328573   5.005331
        2018  |  7.502719   8.183466   7.843092
        2019  |   9.14726   9.428737   9.287999
        2020  |  7.292636   9.459577   8.376107
        2021  |  1.464206   7.402247     5.4229
        Total |  6.369351    8.77362   7.634756
      -----------------------------------------
      In Stata 16 and earlier, the syntax is different, so see the help for your version

      Comment


      • #4
        Dear Nick Cox and Carlo Lazzaro

        The codes work perfectly now. Thanks so much.

        Best regards.

        Comment

        Working...
        X