Announcement

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

  • computing mean of a variable per group, per year

    Hi all,

    I would like to compute the mean of variable ''X'' per ''year'' for every observation that has a value 1 for group Y

    As implied, my data is a panel data.

    Does someone have an idea that could help me out?


  • #2
    Code:
    help egen

    Comment


    • #3
      Originally posted by Nick Cox View Post
      Code:
      help egen
      I had the following:

      Code:
       
       bys year: egen mean_X = mean(X) if Y = 1
      But it did give me an error about the way I had written
      Code:
        if Y = 1

      Comment


      • #4
        Code:
        if Y == 1

        Comment


        • #5
          Originally posted by Nick Cox View Post
          Code:
          if Y == 1
          Is it correct? I doubt if I can also do one of the following:

          Code:
           
            bys year Y: egen mean_X = mean(X) if Y = 1
          Code:
             
            bys year (Y): egen mean_X = mean(X)
          Code:
             
            bys Y (year): egen mean_X = mean(X)
          Code:
             
            bys Y year: egen mean_X = mean(X)

          Comment


          • #6
            Bram:
            as Nick pointed out, the original sin rests on:
            Code:
            bys year Y: egen mean_X = mean(X) if Y = 1///which is illegal
            instead of:
            Code:
            bys year Y: egen mean_X = mean(X) if Y == 1///which is perfectly legal
            As as aside, please note that providing excerpt/example of your dataset (posted via -dataex-; -search dataex- to install before using it) will increase your chances of getting (more) helpful replies. Thanks.
            Last edited by Carlo Lazzaro; 08 May 2017, 06:43.
            Kind regards,
            Carlo
            (Stata 19.0)

            Comment


            • #7
              I don't understand the question. My point (too briefly made, evidently) was that you need == not =.

              Comment

              Working...
              X