I recently read Nick Cox's article on Identifying Spells (https://www.stata-journal.com/articl...article=dm0029). It's really interesting. I figured that you could alter it pretty easily to do cumulative variables.
I think you can capture total the amount of times something happened over the entire study using by modifying this code in section 7.2:
To the following:
This would also, I think, be equivalent to using -egen, rowtotal in wide format.
Let's say that we wanted to sum the values of X up until the present time. In other words, to create a cumulative variable, summing the amount of X for each person up to the current time. So for example, imagine if a study started in 1990 and ended in 2010. At year=1999, we would sum the values from 1990 to 1998. At 2001, you would sum from 1990 to 2000. At 2005, you would sum from 1990 to 2004.
To put it a little more formally, I'd want to sum from year=1990 to year=_n-1, by id. I think this is what the sum() function was designed for, but maybe there's a better way to do this.
Thanks for all of your help.
EDIT: I think it's a rule that I find a relevant thread the moment I post something to Statalist. Here it is https://www.statalist.org/forums/for...ative-variable.
This suggests that sum(X) is the right way to generate the variable from the beginning to _n, but not creating a constant for each ID? Is that correct?
I think you can capture total the amount of times something happened over the entire study using by modifying this code in section 7.2:
Code:
by id spell (year), sort: gen length = year[_N] - year[1] + 1
Code:
by id (year), sort: gen cumulative_x = sum(X)
Let's say that we wanted to sum the values of X up until the present time. In other words, to create a cumulative variable, summing the amount of X for each person up to the current time. So for example, imagine if a study started in 1990 and ended in 2010. At year=1999, we would sum the values from 1990 to 1998. At 2001, you would sum from 1990 to 2000. At 2005, you would sum from 1990 to 2004.
To put it a little more formally, I'd want to sum from year=1990 to year=_n-1, by id. I think this is what the sum() function was designed for, but maybe there's a better way to do this.
Thanks for all of your help.
EDIT: I think it's a rule that I find a relevant thread the moment I post something to Statalist. Here it is https://www.statalist.org/forums/for...ative-variable.
This suggests that sum(X) is the right way to generate the variable from the beginning to _n, but not creating a constant for each ID? Is that correct?
Comment