I am trying to create running Year-To-Date sums of expenses for multiple accounts in multiple categories.
A simplified example of the data that I have:
I would like to calculate the YTD expenditure for each unique set, something like as follows, corresponding to the data above.
The issue that I am running into is that I can't get things to sort properly to allow me to do so.
Essentially just recreates the expense variable, as it is breaking things down by date, not creating a cumulative sum.
is closer, but the months are out of order, resulting in the ytd being randomly ordered and not useful.
I am coming very recently from Python, so my instinct is to just use a "where" function, but can't find a suitable equivalent in Stata.
A simplified example of the data that I have:
Code:
input long account str6(category month) float year str9 date float expense 001 "A" "Jan" 2016 "1/1/2016" 100 001 "A" "Jan" 2016 "1/1/2016" 20 002 "B" "Jan" 2016 "1/1/2016" 50 002 "B" "Jan" 2016 "1/1/2016" 80 001 "A" "Feb" 2016 "2/1/2016" 17 end
Code:
ytd 100 20 50 80 117
Code:
bysort account category date : gen ytd = sum(expense)
Code:
bysort account category year : gen ytd = sum(expense)
I am coming very recently from Python, so my instinct is to just use a "where" function, but can't find a suitable equivalent in Stata.
Comment