Announcement

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

  • Taking Average by id

    Hi Everyone,
    I have a dataset looking like this:

    Date id var1 var2 var3 var4...
    2010 1 ... ... ... ...
    2010 1 ... ... ... ...
    2010 1 ... ... ... ...
    2010 2 ... ... ... ...
    2010 2 ... ... ... ...
    2010 2 ... ... ... ...
    2010 3 ... ... ... ...
    2010 3 ... ... ... ...
    2010 3 ... ... ... ...

    I'd like to find mean of each variable by id. That is, at the end, I want to achieve something like this:


    Date id mean_var1 mean_var2 mean_var3 mean_var4...
    2010 1 ... ... ... ...
    2010 2 ... ... ... ...
    2010 3 ... ... ... ...

    It would be great if you can help. Thank you in advance.
    Best,

  • #2
    Code:
    bysort id: egen meanvar = mean(var)
    help egen
    help collapse
    Last edited by Felix Bittmann; 09 Dec 2019, 05:08.
    Best wishes

    (Stata 16.1 MP)

    Comment


    • #3
      Felix Bittmann gives the flavour, but I guess that something like

      Code:
      by id Date : egen meanvar = mean(var)
      may be closer to what is needed. The thread title and the desired example don't seem consistent, however.

      Comment


      • #4
        Huseyin:
        most depends on what you're after, as the following toy-example try to highlight:
        Code:
        . use "http://www.stata-press.com/data/r15/nlswork.dta"
        (National Longitudinal Survey.  Young Women 14-26 years of age in 1968)
        
        . foreach var of varlist birth_yr-ln_wage {
          2. bysort idcode year: egen mean_`var'=mean(`var')
          3.  }
        
        . list idcode year age mean_age in 1/3
        
             +--------------------------------+
             | idcode   year   age   mean_age |
             |--------------------------------|
          1. |      1     70    18         18 |
          2. |      1     71    19         19 |
          3. |      1     72    20         20 |
             +--------------------------------+
        
        . foreach var of varlist birth_yr-ln_wage {
          2. bysort idcode: egen bis_mean_`var'=mean(`var')
          3.  }
        
        . list idcode year age bis_mean_age in 1/3
        
             +--------------------------------+
             | idcode   year   age   bis~_age |
             |--------------------------------|
          1. |      1     70    18   26.33333 |
          2. |      1     71    19   26.33333 |
          3. |      1     72    20   26.33333 |
             +--------------------------------+
        
        .
        Obbviously, I would discard my first code, since (as expected), it gives back exactly the same value per id year of the original variable,
        Kind regards,
        Carlo
        (Stata 19.0)

        Comment

        Working...
        X