Bookmark and Share

Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down at the end of May, and its replacement, is already up and running.

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

Re: st: date calculation

From   Nick Cox <>
Subject   Re: st: date calculation
Date   Fri, 30 Sep 2011 13:59:10 +0100

I didn't understand the precise problem, but for this kind of data you have

1. a set of panels

2. a set of irregular dates

Under the assumption that no date is repeated in a given panel, it can
sometimes be useful to set up a counter

bysort id (day) : gen counter = _n

and then to

tsset id counter

This makes some calculations easier, e.g. and and
are then defined and will automatically be calculated _within_ panels.


On Thu, Sep 29, 2011 at 11:05 PM, Daniel Marcelino <> wrote:

> I got the solution, despite in a non beautiful way:
> bysort id: gen days = day[_n+1]
> format days %td
> gen days2 = days-day
> bysort iddeputado: egen total = total(days2)
> collapse total, by(id)

On Thu, Sep 29, 2011 at 5:21 PM, Daniel Marcelino <> wrote:

>> I'm fuzzing my mind to get into calculation of date vars. I've a data
>> set with historic of events that I want to calculate number of days.
>> However, I need to account for those occurrences in par of them, the
>> first date is the enter and the subsequent date is the out. If the
>> same id enter again it will receive new enter e new out date. My point
>> is, how can I calculate the overall period regarding this intermediary
>> calculation? Any thoughts is welcome.
>>  day              flag_n       flag_N  id
>> 07may2003       1       4          1
>> 23feb2005               2       4          1
>> 31mar2005       3       4          1
>> 31jan2007       4       4          1
>> 01feb2003               1       2          5
>> 21dec2005       2       2         5

*   For searches and help try:

© Copyright 1996–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index