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

st: RE: creating a cumulative variable


From   "Nick Winter" <nwinter@policystudies.com>
To   <statalist@hsphsun2.harvard.edu>
Subject   st: RE: creating a cumulative variable
Date   Tue, 9 Jul 2002 14:37:43 -0400

> -----Original Message-----
> From: Sarah Mustillo [mailto:smustillo@psych.mc.duke.edu] 
>   I have a longitudinal data set in long form with annual obs
> > for 6 years on 1450 kids.  I am trying to create a 
> cumulative variable 
> such that the
> > value for age 10 is the sum of the values at age 9 and age 
> 10, the value 
> for age 11 is
> > the sum of the value for 9, 10, & 11 and so on.  I can figure out
> > how to do this in wide form, but I am going to have to 
> repeat it for 
> about 50
> > different variables. It would be much easier if I could 
> just keep the
> > data in long form.  I experimented with _n, and by id age, but
> > couldn't get what I wanted (e.g., for age 11 I could get 
> the sum of age 
> 10 (_n-1) and
> > age 11, but not age 9, 10, &11).

I think that you want:

	. bysort id (age): generate cum = sum(<varname>)

Then to do this for lots of variables:

	. foreach var of varlist a b c {
	. bysort id (age): generate cum_`var' = sum(`var')
	. }

where you replace "a b c" with your variable list...

Nick Winter

*
*   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–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index