Announcement

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

  • display mean number of observations per year

    Hi there

    Sorry for this very basic question. I want to display mean number of observation per year in my results window. My data are set up as follows. In this dummy example, the answer would be "8.333333"

    Code:
    clear
    input float(year count)
    2010 1
    2010 1
    2010 1
    2010 1
    2010 1
    2011 1
    2011 1
    2011 1
    2011 1
    2011 1
    2011 1
    2011 1
    2011 1
    2012 1
    2012 1
    2012 1
    2012 1
    2012 1
    2012 1
    2013 1
    2013 1
    2013 1
    2013 1
    2013 1
    2014 1
    2014 1
    2014 1
    2014 1
    2014 1
    2014 1
    2014 1
    2014 1
    2014 1
    2014 1
    2015 1
    2015 1
    2015 1
    2015 1
    2015 1
    2015 1
    2015 1
    2015 1
    2015 1
    2015 1
    2015 1
    2015 1
    2015 1
    2015 1
    2015 1
    2015 1
    end

    Any advice much appreciated. Thanks.

  • #2
    Code:
    . egen yrcount = total(count), by(year)
    
    . egen tag = tag(year)
    
    . su yrcount if tag 
    
        Variable |        Obs        Mean    Std. dev.       Min        Max
    -------------+---------------------------------------------------------
         yrcount |          6    8.333333    4.226898          5         16

    Comment


    • #3
      With the community-contributed command distinct (from SSC), you can also do:
      Code:
      distinct year
      dis "The mean no. of observations per year is `=r(N)/r(ndistinct)'."
      which produces:
      Code:
      The mean no. of observations per year is 8.333333333333334.

      Comment


      • #4
        Good to hear recommendation of distinct, which is (please) better cited as coming from the Stata Journal (another extension will be published in early 2023 I hope).

        The original paper at https://www.stata-journal.com/articl...article=dm0042 also explains ways of getting the number of distinct values without using distinct and just using official commands.

        Comment


        • #5
          Great suggestions, thank you very much.

          Comment

          Working...
          X