Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: st: data management question


From   Daniel Feenberg <[email protected]>
To   "[email protected]" <[email protected]>
Subject   Re: st: data management question
Date   Wed, 18 Sep 2013 18:33:19 -0400 (EDT)



On Wed, 18 Sep 2013, Caroline Wilson wrote:

Hello,

 

I’m wondering if someone can help with a data management
question.

 

I’m trying to create a variable called “mean”, which, for a given pat_ID, would be calculated by taking the mean of every other value of “Md_T” in the same phy_ID EXCEPT for the current row. 


I am a little unclear on what you are asking - Md_T isn't in the sample data you show, but you want the mean of it? So I won't use your variable names. Nevertheless, I think that the -egen- -total- function and a generate statement will get you what you want:

  by ID: egen sum=total(var)
  generate sumex= sum-var
  by ID: generate meanex = sumex/(_N-1)

The total by ID gives the sum of var for each level of ID. Then we subtract the current level of var and divide by the number of observations in the ID group. (_N is the number of observations in the by group).

Daniel Feenberg


© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index