Bookmark and Share

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]

Re: st: Subtracting in Variable by a Group


From   Nick Cox <[email protected]>
To   "[email protected]" <[email protected]>
Subject   Re: st: Subtracting in Variable by a Group
Date   Fri, 29 Mar 2013 16:16:16 +0000

Your do-file could consist of

bysort group (date): gen time = date - date[_n-1]

but many Stata users would just type that one command.


Nick

On 29 Mar 2013, at 16:10, Arya Eskamani <[email protected]> wrote:

Hello and thank you in advance

I'm attempting to find the difference in days between events within different groups. The date is formatted %td (daily). Here's an example of my dataset:

group    event    date
A    1    08mar2013
A    2    10mar2013
A    3    15mar2013
B    1    10sep2012
B    2    15sep2012
C    1    07aug2011
C    2    23aug2011
C    3    23aug2011
C    4    24aug2011
D    1    25dec2012
E    1    01jan2013

I want to generate a new variable, say "time" , that calculates the difference in days from one event occurring to the next event in between the groups. For example, in group A there are 2 days between even 1 and event 2 and then 5 days from event 2 to event 3, and similarly for others. The example below shows what I'm trying to achieve:

group    event    date    time
A    1    08mar2013    .
A    2    10mar2013    2
A    3    15mar2013    5
B    1    10sep2012    .
B    2    15sep2012    5
C    1    07aug2011    .
C    2    23aug2011    16
C    3    23aug2011    0
C    4    24aug2011    1
D    1    25dec2012    .
E    1    01jan2013    .

Notice that the time variable is blank for 1st events because there is no previous event to subtract from. I've never ran an operation within a column before and have been doing this in excel but would like to just write a do file on this. Any help is much appreciated. Thank you again.


Arya Eskamani


*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/faqs/resources/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/faqs/resources/statalist-faq/
*   http://www.ats.ucla.edu/stat/stata/


© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index