Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.

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

From |
Nick Cox <njcoxstata@gmail.com> |

To |
"statalist@hsphsun2.harvard.edu" <statalist@hsphsun2.harvard.edu> |

Subject |
Re: st: calculating cumulative values of other observations |

Date |
Sat, 7 Apr 2012 09:08:49 +0100 |

Why not bysort id activity (time): gen priorhrs = sum(time) - time Nick On 7 Apr 2012, at 02:29, Eric Booth <eric.a.booth@gmail.com> wrote:

<> ******************! //input example data// clear inp ID str18 date str4 activity hours 1 20071230 A 10 1 20071122 A 11 2 20071120 A 5 3 20071210 B 6 3 20071111 B 24 3 20071130 F 20 3 20070511 B 49 4 20070611 A 50 end //cleanup// g date2 = date(date, "YMD") format date2 %td sort ID activity date2 //gen prior hours var// g priorhrs = 0 forval n = 1/`=_N' { by ID activity: replace priorhrs = /// priorhrs + hours[_n-`n'] /// if date2[_n]>=date2[_n-`n'] } l ********************! - Eric __ Eric A. Booth Public Policy Research Institute Texas A&M University ebooth@ppri.tamu.edu +979.845.6754 On Apr 6, 2012, at 6:09 PM, KOTa wrote:Hi all, i have a data looking like this:ID date activity hours prior resultshould be:1 20071230 A 10 ? <-- 11 1 20071122 A 11 ? 0 2 20071120 A 5 ? 0 3 20071210 B 6 ? 73 3 20071111 B 24 ? 49 3 20071130 F 20 ? 0 3 20070511 B 49 ? 0 4 20070611 A 50 ? 0(better picture here http://gyazo.com/5dc1218534d8b185ab22989069034b8a.png)i need to fill "prior" column with following: - amount of hours spent on same activity by same user(ID) prior to date of current activity. example: in case of observation 4(line 4) activity is B, so prior = sum of hours spent on activity B before it took place (i.e. before 20071210) there are 2 such observations, one with 49 hour, another with 24. Therefore, prior = 24+49=73. any suggestions how to make this simple? i was thinking to use "sort" and "by:", but i can't figure out how to conditionally("if") sum up values from different observations into "prior" var its not a first time i encounter similar problem and always took some way around, but this time i decided to ask, maybe there is proper, simple way to do this thank you in advance K. * * 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/* * 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/

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

**Follow-Ups**:**Re: st: calculating cumulative values of other observations***From:*KOTa <kota.alba@gmail.com>

**References**:**st: calculating cumulative values of other observations***From:*KOTa <kota.alba@gmail.com>

**Re: st: calculating cumulative values of other observations***From:*Eric Booth <eric.a.booth@gmail.com>

- Prev by Date:
**Re: st: Capturing the date and which something first occurs** - Next by Date:
**st: spells of missing values completely in between** - Previous by thread:
**Re: st: calculating cumulative values of other observations** - Next by thread:
**Re: st: calculating cumulative values of other observations** - Index(es):