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, statalist.org 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   Nick Cox <njcoxstata@gmail.com>
To   "statalist@hsphsun2.harvard.edu" <statalist@hsphsun2.harvard.edu>
Subject   Re: st: Selecting within a subset of observations
Date   Thu, 13 Jun 2013 11:25:29 +0100

Maarten is quite correct here. Dates are in many ways special in Stata
but the last date is just the maximum date and identifiable by
whatever identifies the maximum in your situation.

In addition, a

. search date, faq

would have pulled up an FAQ which gives advice on other versions of
the problem too.

FAQ     . . . . . . . . . . . . . . . . . . . . . . . Generating the last date
        . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  N. J. Cox
        7/11    How can I generate a variable containing the last of
                several dates?
                http://www.stata.com/support/faqs/data-management/
                generating-last-date/

There is even advice on first dates, which might prove to be what you
need sometimes.

Nick
njcoxstata@gmail.com


On 13 June 2013 10:16, Maarten Buis <maartenlbuis@gmail.com> wrote:
> 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/
*
*   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–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index