Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.
From | "Alvarez,Sergio" <sergioal@ufl.edu> |
To | <statalist@hsphsun2.harvard.edu> |
Subject | Re: st: RE: Create variable with historic means |
Date | Tue, 16 Aug 2011 14:34:55 -0400 |
Thanks Nick! Works like a charm. Sergio On Tue, 16 Aug 2011 18:05:26 +0100, Nick Cox wrote:
Your bottom line implies surprise, but in general -if- automatically ignores excluded observations, both in using data and in assigning results. Also, at first sight -egen- is fairly hopeless for problems that somewhere involve leads or lags. But it may be that this will do what you want. by zone wave mode, sort: egen hckr = mean(catch/(year<2006)) There are two tricks here, one standard and one non-standard. 1. -egen, mean()- will work on expressions, which can be (much) more complicated than single variable names. 2. The expression here is catch / (year < 2006)The Boolean (year < 2006) evaluates as 1 or 0, so the whole evaluates ascatch if year < 2006 . if year == 2006 So the effect is to ignore 2006, but nevertheless to assign the result to all years including 2006. Another way to do it is by zone wave mode, sort: egen hckr = mean(cond(year<2006), catch, .) but I am fond of the first way. For a write-up, see SJ-11-2 dm0055 . . . . . . . . . . . . . . Speaking Stata: Compared with ... . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . N. J. Cox Q2/11 SJ 11(2):305--314 (no commands) reviews techniques for relating values to values in other observations Nick n.j.cox@durham.ac.uk SergioI'm trying to create a new variable that contains the historical means of CATCH, but only using the previous years. My dataset looks something likethis: YEAR CATCH ZONE WAVE MODE 2004 2 1 1 1 2005 3 2 3 2 ... ... ... ... ... 2006In this case I want to create a new variable that will take the value of the mean catch for the previous years (2004 and 2005) by ZONE, WAVE and MODE, but not use any of the 2006 observations to calculate the means. Its OK if the variable is created for the previous years as well, as I plan to dropthose observations once I can get the new variable.I have been able to create a table that gives me the mean of CATCH by ZONE,WAVE and MODE using: table zone wave mode if (year<2006), contents (m catch)And I can create a variable that has the means, using 2006 data, with:by zone wave mode, sort: egen hckr = mean(catch)When I try to create the variable using only the previous years, I haveused: by zone wave mode, sort: egen hckr = mean(catch) if(year<2006)But it just gives me missing values for all observations from year 2006.* * For searches and help try: * http://www.stata.com/help.cgi?search * http://www.stata.com/support/statalist/faq * http://www.ats.ucla.edu/stat/stata/
-- Sergio Alvarez Food and Resource Economics University of Florida * * For searches and help try: * http://www.stata.com/help.cgi?search * http://www.stata.com/support/statalist/faq * http://www.ats.ucla.edu/stat/stata/