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]

st: RE: RE: Calculating moving windows over time with conditions


From   Nick Cox <n.j.cox@durham.ac.uk>
To   "'statalist@hsphsun2.harvard.edu'" <statalist@hsphsun2.harvard.edu>
Subject   st: RE: RE: Calculating moving windows over time with conditions
Date   Fri, 4 Feb 2011 13:28:27 +0000

Commenting now on the code, 

0. Your basic structure is 

by id year:

There is only one observation in each of those combinations. You need 

by id (year):

1. A key thing is that -egen-'s "functions" do not behave at all like Stata's functions. Thus you must refer to just _one_ function on the right-hand side of an = sign. 

The syntax of -egen- is given in the help. 

egen [type] newvar = fcn(arguments) [if] [in] [, options]

So the minimal call is 

egen newvar = fcn(arguments) 

There is no scope for more than one -fcn()- call. 

2. -if- is allowed just once in any Stata command. -if- never appears _inside_ anything else. 

3. You could use -cond(,)- as part of an expression to express branching. In this case, it would get messy almost beyond belief. 

I'd back off from this approach and use L. directly as Johannes suggested or -rolling- or -mvsumm- (SSC) as I suggested earlier. 

Nick 
n.j.cox@durham.ac.uk 

Nick Cox

Consider also using -rolling- or -mvsumm- (SSC). Writing your own code for problems like this is instructive, but not necessary. 

erik.aadland@bi.no

I have an unbalanced panel dataset in which I need to calculate a 3 year moving window for a variable for each actor in the dataset.

I have already calculated the annual total sum for the variable for each year (var_x). I have tagged individuals by year and removed all observations but one per year.

Now I need to sum the annual totals up for each actor by year in 3 year moving windows. As the dataset is unbalanced, I need to make sure that observation _n-1 is indeed the year before _n, and not several years prior to _n. I don't get it quite right. I use stata 10.

Here is the code so far:
sort id year ;
egen tag_id_year = tag(id year) ;
keep if tag_id_year == 1;

sort id year ;
bysort id year: egen var_3yrs = total(var_x) & total(var_x[_n-1]if year==year[_n-1]+1) & total(var_x[_n-2]if year==year[_n-2]+2) ;

I have also tried:
bysort id year: egen var_3yrs = total(var_x) + total(var_x[_n-1]if year==year[_n-1]+1) + total(var_x[_n-2]if year==year[_n-2]+2) ;

And:
bysort id year: egen var_3yrs = total(var_x + var_x[_n-1]if year==year[_n-1]+1 + var_x[_n-2]if year==year[_n-2]+2) ;


*
*   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/


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