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: Selecting within a subset of observations
From
Maarten Buis <[email protected]>
To
[email protected]
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:
<snip>
> 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
clear
input ID str10 EventDate
1 "01/03/2011"
2 "31/01/2011"
2 "05/09/2011"
3 "12/03/2011"
end
// 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
list
*------------------- end example -------------------
* (For more on examples I sent to the Statalist see:
* http://www.maartenbuis.nl/example_faq )
Hope this helps,
Maarten
---------------------------------
Maarten L. Buis
WZB
Reichpietschufer 50
10785 Berlin
Germany
http://www.maartenbuis.nl
---------------------------------
*
* 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/