Stata The Stata listserver
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

Re: st: cumulative average moving through time


From   Daniel Lawson <[email protected]>
To   [email protected]
Subject   Re: st: cumulative average moving through time
Date   Wed, 6 Oct 2004 14:21:28 -0500

Dear Dan,

I'm sure there is a more elegant way to do this, but how about

sort PID OB
gen calc_sum = calc
replace calc_sum = calc_sum[_n-1] + calc if (_n>1) & (PID[_n] == PID[_n-1])
gen cave = calc_sum / OB

Peace,
Daniel Lawson

On Oct 6, 2004, at 1:38 PM, Daniel Egan wrote:


Hello all,

I have iregularly spaced time/measurement data. I want to calculate an
average using all prior measurements. FYI, the variable to be
"averaged" is a dummy, so I would be looking at a percentage.

a hail mary was:

sort PID OB
egen calc_sum=sum(calc[_n-_n]), by(PID)
gen cave=calc_sum/OB

which obviously does not work.

My data looks like this. cave is the variable i wish I knew how to create.

PID OB dm dm_m calc cave
1 1 1-4-2000 0 1 1
1 2 1-10-2000 6 0 .5
1 3 1-28-2000 24 0 .333
1 4 2-1-2000 28 1 .5
2 1 10-5-1998 0 1 1
2 2 10-8-1998 3 0 .5
2 3 1-22-1998 17 1 .666
2 4 11-5-1998 31 1 .75



Thanks,

Dan
*
* For searches and help try:
* http://www.stata.com/support/faqs/res/findit.html
* http://www.stata.com/support/statalist/faq
* http://www.ats.ucla.edu/stat/stata/

*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/



© Copyright 1996–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index