Statalist


[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

Re: st: calculating cumulative exposure


From   "Sebastian F. B�chte" <[email protected]>
To   [email protected]
Subject   Re: st: calculating cumulative exposure
Date   Fri, 3 Aug 2007 17:15:23 +0200

On 8/3/07, Raoul C Reulen <[email protected]> wrote:
> Dear statalisters,
>
> I've got multiple records per person and want to calculate some kind of cumulative exposure index per person. I've got a variable called "exposure" and want to create a new variable called "cum_exposure". Each observation in the cum_exposure variable should give the sum of all previous cells in the exposure column (but per person). So something like this:
>
> Id      Exposure        list            Cum.Exposure
> 1       10              1                       10
> 1       14              2                       24
> 1       15              3                       39
> 2       8               1                       8
> 2       10              2                       18
> 2       15              3                       32
>

First, what I am really missing in your data example is a time
variable so that a defined sort order is possible. I work in the field
of epidemiology, where exposure typically happens to subject at a
certain point in time and cumulative exposure reflects the exposure
experienced by the subject in the past up to a specified point in
time.

Answering you question, I think one of the most efficient ways is to
use -bysort- (or just -by- if your data is already sorted)

bysort id (timevariable): gen cum_exposure = sum(exposure)

Regards
Sebastian
*
*   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