Hi,
I have data on variable X for only single days of a month. The other days of a month have a zero for the observation. I want to succesively add up the observations for one month, i.e. Xcum_t = X_t + X_t-1. At the first of each month I want to start a new sum. I tried the following:
format %td date
tsset date
tsfill
replace X = 0 if X == .
generate month = month(date)
generate year = year(date)
generate yearmonth = ym(year,month)
sort date
bysort yearmonth: gen cumX = X + L.X
But this always gives me "not sorted".
What am I doing wrong?
Best, Johanna
I have data on variable X for only single days of a month. The other days of a month have a zero for the observation. I want to succesively add up the observations for one month, i.e. Xcum_t = X_t + X_t-1. At the first of each month I want to start a new sum. I tried the following:
format %td date
tsset date
tsfill
replace X = 0 if X == .
generate month = month(date)
generate year = year(date)
generate yearmonth = ym(year,month)
sort date
bysort yearmonth: gen cumX = X + L.X
But this always gives me "not sorted".
What am I doing wrong?
Best, Johanna
Comment