Statalist The Stata Listserver


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

Re: st: cumulate values of X whithin group


From   "Svend Juul" <[email protected]>
To   <[email protected]>
Subject   Re: st: cumulate values of X whithin group
Date   Sat, 5 May 2007 21:41:38 +0200

Sara Borelli wrote:
 
I have the following question:

Member       Date              X
133          04-05-2000        1
133          04-07-2000        4
133          05-06-2000        2
133          07-10-2000        3
145          06-05-2000        5
145          06-15-2000       5
145          08-05-2000       0
145          10-20-2000       2
145         11-05-2000       4
145          12-03-2000      6
 
Within each member, I need to create a variable that
cumulates the values of X up to the date of the
current observation excluding the value of X of the
current observation, that is I need to create Xnew
such that:
 
Member   Date                   X    Xnew
133          04-05-2000        1     0
133          04-07-2000        4     1
133          05-06-2000        2      5
133          07-10-2000        3      7
145          06-05-2000        5      0
145          06-15-2000       5       5  
145          08-05-2000       0       10
145          10-20-2000       2       10
145         11-05-2000       4        12
145          12-03-2000      6        16
 
--------------------------------------------------------------------
 
This does it:

   sort Member Date
   by member: generate Xnew=sum(X)-X
 
-sum()- calculates the cumulative sum, here within a by-group; see:
   
   help sum()

Hope this helps
 
Svend
 
________________________________________________________ 
 
Svend Juul
Institut for Folkesundhed, Afdeling for Epidemiologi
(Institute of Public Health, Department of Epidemiology)
Vennelyst Boulevard 6 
DK-8000 Aarhus C,  Denmark 
Phone, work:  +45 8942 6090 
Phone, home:  +45 8693 7796 
Fax:          +45 8613 1580 
E-mail:       [email protected] 
_________________________________________________________ 

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