Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Second minimum date

    I have different date variables and want to find the second minimum date. That is when you order the dates from lowest to highest, what is the second lowest date after the first? Can someone help me with this, please? Below is an example dataset. Thank you very much.
    ID age date1 date2 date3 date4
    1. 30. 16/12/2012 10/06/2000 01/01/1999 06/05/2001
    ​​​​​​2. 19. 16/07/2014 09/04/2002 02/10/1999 07/04/1998

  • #2
    Check out the user written command -rowranks- and this article here:
    https://journals.sagepub.com/doi/pdf...867X0900900107

    and if you do not figure it out, provide a better data sample using -dataex-.

    Comment


    • #3
      Another strategy would be to reshape the data to long, sort by date, and then the second lowest would be in date[2].

      Comment


      • #4
        Naa Naadu The first and second threads you started saw repeated requests to give us proper data examples:

        https://www.statalist.org/forums/for...onths-for-date

        https://www.statalist.org/forums/for...on-for-stsplit

        The request stands and is spelled out in the FAQ Advice, which all members are prompted to read before posting: Here it is again, with added emphasis:

        We can understand your dataset only to the extent that you explain it clearly.

        The best way to explain it is to show an example. The community-contributed command dataex makes it easy to give simple example datasets in postings. It was written to support Statalist and its use is strongly recommended. Usually a copy of 20 or so observations from your dataset is enough to show your problem. See help dataex for details.

        As from Stata 15.1 (and 14.2 from 19 December 2017), dataex is included with the official Stata distribution. Users of Stata 15 (or 14) must update to benefit from this. Users of Stata 16 or 17 need not do anything: dataex is already part of your installation.

        Users of earlier versions of Stata must install dataex from SSC before they can use it. Type ssc install dataex in your Stata.

        The merits of dataex are that we see your data as you do in your Stata. We see whether variables are numeric or string, whether you have value labels defined and what is a consequence of a particular display format. This is especially important if you have date variables. We can copy and paste easily into our own Stata to work with your data.

        If your dataset is confidential, then provide a fake example instead.




        Another strategy -- really the same strategy as implemented in rowsort -- is just to push the dates into Mata, sort and pull out the second ciolumn after sorting.

        Code:
        * Example generated by -dataex-. For more info, type help dataex
        clear
        input float id byte age float(date1 date2 date3 date4)
        1 30 19343 14771 14245 15101
        2 19 19920 15439 14519 13976
        end
        format %td date1
        format %td date2
        format %td date3
        format %td date4
        
        gen second = .
        
        mata: dates = st_data(., "date1 date2 date3 date4")
        mata: _sort(dates, 2)
        mata: st_store(., "second", dates[,2])
        
        format second %td
        
        list
        
        +----------------------------------------------------------------------+
        | id age date1 date2 date3 date4 second |
        |----------------------------------------------------------------------|
        1. | 1 30 16dec2012 10jun2000 01jan1999 06may2001 10jun2000 |
        2. | 2 19 16jul2014 09apr2002 02oct1999 07apr1998 09apr2002 |
        +----------------------------------------------------------------------+
        Last edited by Nick Cox; 22 Dec 2022, 03:58.

        Comment


        • #5
          Note that #3 answers a different question from #2 or #4.

          Comment

          Working...
          X