Announcement

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

  • how to include an if qualifier in an egen command mean() function

    I'm working with a time-series panel data set and am attempting to calculate a cross sectional mean for each month - but only for observations not containing a dummy ("dum") variable.

    to calculate a cross sectional average of the variable ff, I have used:
    egen ave_ff =mean(ff), by(month)
    but I am hoping someone could help me include an if qualifier so that I only average the observations not containing the dummy variable dum

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input float month long id float(ff dum)
    664 2708 -.0018369863 0
    665 2708   -.00494564 0
    666 2708  -.009506066 0
    664 2745    .00530777 0
    665 2745   -.05659205 0
    666 2745 -.0013513556 0
    664 3946   .011887698 1
    665 3946   .020265907 1
    666 3946    .00999126 1
    end
    format %tmYYMon month
    thank you, Dan

  • #2
    I assume you meant you want to average only those observations for which dum == 0, right? After all every observation contains every variable. Or perhaps you meant only those observations for which dum has a non-missing value?

    Any way, assuming you mean to include only those for which dum == 0:

    Code:
    by month, sort: egen ave_ff = mean(cond(dum == 0, ff, .))

    Comment


    • #3
      Clyde's approach and the problem more generally are discussed in Sections 9 and 10 of https://www.stata-journal.com/sjpdf....iclenum=dm0055

      Comment


      • #4
        Thank you for the guidance Clyde, yes, you correctly read my mind - I meant dum == 0, sorry, and thank you for the links Nick, much appreciated, Dan

        Comment

        Working...
        X