Announcement

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

  • Calculate mean of Var1 if X==1 minus mean of Var1 if X==2 by date

    Hello everybody,

    I asked this question in a more complicated way. So I try it in an easier way. As the title says I want to calculate:

    mean of Var1 if X==1 minus mean of Var1 if X==2 by date

    I would do it like that but it does not work can you help me?

    by date: egen R_12=mean(Var1) if Var1==1 -(minus) mean(Var1) if Var1==2

    But I do not know how I can make it work.

    Hope this is a easier description.

    Thanks a lot for your help

    Simon

  • #2
    I assume that you want a single result for each date, not multiple copies of it dispersed in the observations. Try this:
    Code:
    collapse (mean) meanvar1=var1 , by(date x)
    reshape wide meanvar1 , i(date) j(x)
    gen diffvar1 = meanvar11-meanvar12
    list
    Last edited by Svend Juul; 03 Dec 2014, 08:09.

    Comment


    • #3
      Hello,

      thanks a lot for the fast reply. It is working but then I loose data like the var "Company" and also other variables. How can I be sure that this data stays is not deleted?

      thanks

      Simon

      Comment


      • #4
        OK - you want the resulting difference to be a new variable. In that case:
        Code:
        sort date
        by date: egen mv1_1 = mean(var1) if x==1
        by date: egen mv1_2 = mean(var1) if x==2
        by date: egen meanvar1_1 = mean(mv1_1)
        by date: egen meanvar1_2 = mean(mv1_2)
        gen diffvar1 = meanvar1_1 - meanvar1_2

        Comment

        Working...
        X