I'm pretty new to Stata, and am having difficulty in creating for loops for the following dataset.
I have several types of variables(Y,X...) which are in fact observations taken from several years(2-12). e.g. aY2-aY13, aX2-aX12.... and so on and so forth.
I want to generate a new set of variables, which are the cumulative sums of each of the prior observations. e.g. Cumul aY5 =
aY2 + aY3 + aY4 + aY5.
I am guessing that the solution is probably nesting a for loop inside another ( one for the number at the end, and the other for the variable types), but I'm not sure how to go about it.
Hypothetical observation and output below(with 2 observations, 2 variable types, 3 years):
I am guessing that the solution is probably nesting a for loop inside another ( one for the number at the end, and the other for the variable types), but I'm not sure how to go about it.
Hypothetical observation and output below(with 2 observations, 2 variable types, 3 years):
aY1 | aY2 | aY3 | aX1 | aX2 | aX3 | Cumul aY2 | Cumul aY3 | Cumul aX2 | Cumul aX3 | |
1 | 2 | 3 | 4 | 5 | 6 | 3 | 6 | 9 | 15 | |
3 | 4 | 2 | 7 | 1 | 3 | 7 | 9 | 8 | 11 |
Thanks so much!
0 Response to For loops for generating cumulative sums of variables?
Post a Comment