Dear Stata experts

I am a bloody beginner when it comes to stata. I am working with unbalanced panel data. I have data over the period from 2004-2016 (year) for a number of firms (CIK identifier). I have already created a variable for the annual effective tax rates (gaapetr). However, I would also like to include a variable for the long-term cash rates over a 5-year period (lt-gaapetr), defined as follows: Sum of income tax (txt) over 5 years divided by sum of pre-tax income (= pi; minus special items = spi) over these same 5 years.

I have been searching the internet and several forums for getting an idea of how to approach this, but unfortunately, I have not gotten any closer to finding a solution on how to solve this.
My first approach/idea would have looked somewhat like this, but it tells me the syntax is invalid and I am pretty sure there is a more straightforward way to do this.

. bysort cik (year): gen lt-gaapetr = (txt+ txt[_n-1] + txt[_n-2] + txt[_n-3] + txt[_n-4]) / ((pi - spi) + (pi[_n-1] - spi[_n-1]) + (pi[_n-2] - spi[_n-2]) + (pi[_n-3] - spi[_n-3]) + (pi[_n-4] - spi[_n-4]))

I hope someone can help me shed a light on my personal black box here!

Thank you very much!

Ellen