I would like to generate a new variable which computes a rolling cumulative sum of observations of x from 3 days before up until 1 days before the observation in consideration within panels.
For example if I have:
ID Date x to_create
1 1 1 .
1 2 2 .
1 3 3 .
1 4 2 6
1 5 1 7
1 6 2 6
2 1 2 .
2 2 3 .
2 3 4 .
2 4 3 9
2 5 2 10
2 6 3 9
to_create is the variable I would like to obtain.
Going through statalist I found the following commands:
bysort ID (Date): gen to_create = sum(x[_n-3] - x[_n-1])
However using this command, stata simply subtracts x[_n-1] from x[_n-3]
I have tried numerous other commands but keep on getting only missing values or all values 0. Is there any other way to compute to_create?
As I am new to this forum I hope to have expressed my problem clearly. T
Thanks in advance!
For example if I have:
ID Date x to_create
1 1 1 .
1 2 2 .
1 3 3 .
1 4 2 6
1 5 1 7
1 6 2 6
2 1 2 .
2 2 3 .
2 3 4 .
2 4 3 9
2 5 2 10
2 6 3 9
to_create is the variable I would like to obtain.
Going through statalist I found the following commands:
bysort ID (Date): gen to_create = sum(x[_n-3] - x[_n-1])
However using this command, stata simply subtracts x[_n-1] from x[_n-3]
I have tried numerous other commands but keep on getting only missing values or all values 0. Is there any other way to compute to_create?
As I am new to this forum I hope to have expressed my problem clearly. T
Thanks in advance!
Comment