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 on April 23, and its replacement, is already up and running.

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

Re: st: Selecting within a subset of observations

From   Maarten Buis <>
Subject   Re: st: Selecting within a subset of observations
Date   Thu, 13 Jun 2013 11:16:59 +0200

On Thu, Jun 13, 2013 at 10:52 AM, Ada Keding wrote:
> I am trying to use min/max or similar within a subset of observations.
> For example, I have one or more event dates for different identifiers:
> Now I would like to create a variable that displays the latest event
> date within each identifier.

Here is how I would do this:

*------------------ begin example ------------------
// copy your example dataset
input ID    str10 EventDate
1      "01/03/2011"
2      "31/01/2011"
2      "05/09/2011"
3      "12/03/2011"

// transform the datestring to Stata dates
gen date = date(EventDate,"DMY")
format date %td

// find the most recent event per ID
bys ID : egen last = max(date)

// format it, so it also looks like a date
format last %td

// admire the results
*------------------- end example -------------------
* (For more on examples I sent to the Statalist see:
* )

Hope this helps,

Maarten L. Buis
Reichpietschufer 50
10785 Berlin
*   For searches and help try:

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