STATA- explicit subscripting

Shay

New Member
I would like to get totals for subgroups (or sections) in a column (a). My database is in stata 8.
I want these group totals to be displayed in an adjacent column (b), and the same total to be repeated in column b for every cell in the group in column a.
At the moment I'm using a really convoluted method to do this:

Column a- totzero
Column b- totnite

I've generated my _N and _n

I want the totals from every verical group of 5 values in 'totzero' to be added together and displayed in column 'totnite' 5 times, i.e. next to each value in column totzero in the group, like this:-

by treat hno night: gen totnite = totzero[_n] + totzero[_n-1]+ totzero[_n-2] + totzero[_n-3] + totzero[_n-4]
by treat hno night: gen totnite2 = totzero[_n] + totzero[_n+1] + totzero[_n-1] + totzero[_n-2] + totzero[_n-3]
by treat hno night: gen totnite3 = totzero[_n] + totzero[_n+1] + totzero[_n+2] + totzero[_n-1] + totzero[_n-2]
by treat hno night: gen totnite4 = totzero[_n] + totzero[_n+1] + totzero[_n+2] + totzero[_n+3] + totzero[_n-1]
by treat hno night: gen totnite5 = totzero[_n] + totzero[_n+1] + totzero[_n+2] + totzero[_n+3] + totzero[_n+4]

by treat hno night: replace totnite = totnite2 if small==4
by treat hno night: replace totnite = totnite3 if small==3
by treat hno night: replace totnite = totnite4 if small==2
by treat hno night: replace totnite = totnite5 if small==1

drop totnite2
drop totnite3
drop totnite4
drop totnite5

It is the only way I could think of getting all the totals in one column!
Anyone has any ideas? there may be some simple command that I've overlooked.
cheers
Shay