Announcement

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

  • calculate mean by group with condition.

    Suppose that I have the following variable, hhid, age, and x.
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input float(hhid age x mean_x mean_x50)
    1 45 1 2.5 2.5
    1 51 2 2.5 2.5
    1 65 3 2.5 2.5
    1 33 4 2.5 2.5
    2 30 5 6.5 7.5
    2 45 6 6.5 7.5
    2 60 7 6.5 7.5
    2 62 8 6.5 7.5
    end
    I calculate the mean by group (hhid) with
    HTML Code:
    bys hhid: egen mean_x = mean(x)
    and I also want to do that again, but only for age > 50. The desired results are shown as "mean_x50". Any suggestions? Thanks.
    Ho-Chuan (River) Huang
    Stata 19.0, MP(4)

  • #2
    Code:
    bys hhid: egen mean_x50_1 = mean(x) if age > 50  
    bys hhid: egen mean_x50_2 = mean(x / (age > 50))    
    bys hhid: egen mean_x50_3 = mean(cond(age > 50, x, .))
    The second and third have identical consequences, but the first only puts results in observations it uses.

    See http://www.stata-journal.com/sjpdf.h...iclenum=dm0055 Sections 9 and 10.

    Comment


    • #3
      Originally posted by Nick Cox View Post
      Code:
      bys hhid: egen mean_x50_1 = mean(x) if age > 50
      bys hhid: egen mean_x50_2 = mean(x / (age > 50))
      bys hhid: egen mean_x50_3 = mean(cond(age > 50, x, .))
      The second and third have identical consequences, but the first only puts results in observations it uses.

      See http://www.stata-journal.com/sjpdf.h...iclenum=dm0055 Sections 9 and 10.
      Nick, many thanks for the help.
      Ho-Chuan (River) Huang
      Stata 19.0, MP(4)

      Comment


      • #4
        Hey Nick,
        i have a following question
        please help..
        Last edited by Ridwan Sheikh; 22 Apr 2021, 12:36.

        Comment


        • #5
          I have 4-dimensional panel data of imports of country (j) from country (i) in sector (s) at time (t). The number of importing countries is 88 (j=1 to 88) and exporters is 24 (i = 1 to 24). In this data, i have a variable called ''distance_ij" which gives bilateral distance between (i) and (j) . I want to generate two variables , for example:
          (1) mean distance of country (j) from all the countries in (i) in each sector .
          (2) mean distance of country (i) from all the countries in (j) for each sector. i am running the following codes ;
          egen x1 = mean(distance_ij), by (importer sector)
          egen x2 = mean(distance_ij), by (exporter sector)
          i want to know whether these are right codes to generate these mean distance.
          in my case i am using Baier and Bergstrand, (2009) approach of gravity model .
          please help..
          thanks,
          Ridwan

          Comment

          Working...
          X