Statalist


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

st: Trouble creating a--by group--cumulative variable


From   [email protected]
To   [email protected]
Subject   st: Trouble creating a--by group--cumulative variable
Date   Sat, 17 Jan 2009 01:32:25 -0500

I'm trying to create a variable that is the stepwise, by group, cummulative tally of another variables values. For example, say I have three groups of different size, where all members of the group have the same score. My goal is to add the score from the current group to the preceeding group, on down, so the final group's calculated value is the sum of all groups scores. I want to accomplish this without destroying the data (collapsing).

groupid  score  newvar <--what I'm trying to code
1        5      5
1        5      5
2        11     16
2        11     16   2        11     16
3        4      20
3        4      20
3        4      20
3        4      20

In excel, I would in effect carry out this by looking for breaks in the group id and having conditional replacement in the newvar, like:

if current id != prior id replace newvar with (current score + prior newvar value) ;
else if current id == prior id replace newvar with (prior newvar value) ;


For example, when I get to the first observation in groupid 3, my code should add 4 to 16 in my newvar. While the code would simply replace a missing value in my newvar with the previous newvar score for the second observation in group 3.

Seems simple enough, but I'm relatively new to stata programming and haven't figured out how to do this, efficiently. I could see using the group() command to facilitate this, but I don't know how to call the max value of the prior group. In fact, I can't seem to figure out how to sequentially run the program observation to observation or group to group. Any help would be appreciated.

Dave Kershaw






*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   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