Announcement

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

  • Create a new variable out of the mean values of existing variables

    Hello,

    I want to create a new variable which is the mean value of a long list of existing variables, i.e.,
    observation 1 = mean of var 1
    observation 2 = mean of var 2
    etc.

    In total I have 150 variables with 5000 observations, thus my new variable should contain 150 observations.

    Is this possible with the egen or generate command since my new variable would contain substantially less observations compared to the other variables?

    Thanks a lot in advance!

  • #2
    your question is very unclear (please read the FAQ and follow its advice), but a guess -collapse- will help; see
    Code:
    h collapse

    Comment


    • #3
      Ok sorry - Ill try again because collapse is not really doing the job.

      The data looks like this:
      var1 var2 var 3 var 4 indicator_variable
      0 2 2 1 0
      5 3 3 2 1
      6 5 4 3 2
      7 6 6 4 1
      3 2 5 3 1
      5 9 7 5 0
      6 1 8 6 1
      7 7 9 5 2
      2 7 9 5 0
      I want the means (conditional on the indicator variable) to be transposed into a new variable which then has 4 observations because I have 4 variables. Below I presented an example of how the new variables are supposed to look like.
      mean0 is the mean of variable 1-4 for all observations which have 0 as the indicator variable.

      Thus the first observation is 3,5, as the mean of var1 if indicator=0 -> (0+5+2)/3=3,5

      The second observation is 9 because the mean of var 2 if indicator=0 -> (2+9+7)/3=9

      mean0 mean1 mean2
      3,5 5,25 6,5
      9 6 6
      9 11 6,5
      5,5 7,5 3,5
      Can I create these new variables in the same dataset or do I need to create a new dataset and what would the command be?

      Thanks a lot !!
      Last edited by Philip HU; 25 Oct 2023, 07:33.

      Comment

      Working...
      X