Announcement

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

  • sort dataset on date variable

    Hi, I want to sort a dataset according to the date for the observations. I have a variable date in this format: "DMYhms". One examle of the variable: "05-01-2017 08:54:45". The command sort uses only the first part of the variable (Day) to sort the dataset. How can I sort the dataset from the newest to the oldest obserervation?

  • #2
    I think what you say is wrong as sort will not ignore any part of the string it is sorting on.

    But converting this to a Stata date-time variable is long overdue.


    Code:
    gen double numdate = clock(strdate, "DMY hms")
    gsort -numdate 

    Comment


    • #3
      Thanks!

      Comment


      • #4
        Hi Nick Cox
        Assuming one has converted the stata date into a Stata form using the code:

        gen admissiondate = date(admidate, "YMDhms")

        will the sort only work if the date it is in STATA form?

        I find that once I have converted the dates into a STATA date and then into a readable form for the human eyes, stata will sort the dates but it's not the ascending order...
        format admissiondate %td

        Comment


        • #5
          #4 Please give a data-based example, FAQ Advice #12 applies (see also #18 while visiting).

          Comment


          • #6
            the code in #4 appears wrong in two senses, each covered in @Nick Cox's post in #2: (1) your generate statement should be followed by the word "double" as highlighted in the help file; (2) if you want reversed order you should, as Nick did in #2, use gsort with a minus sign before the name of the variable you wanted reverse sorted; again, see the help file for -gsort-

            Comment


            • #7
              You can just do
              Code:
              sort -date
              If I understand you well

              Comment


              • #8
                Too much small confusion here. Working backwards

                #7 Jared For that you need gsort not sort.

                #6 Rich Indeed. Also there may be confusion in #4 between clock() and date() -- noting that date() yields daily dates only, and not date-times.

                Comment


                • #9
                  Too quick !

                  Aim: To sort generate a _n observation for each episode the patient was discharged in hospital and sort the episodes according to discharge date in ascending order

                  Here's a copy of my data:
                  ----------------------- copy starting from the next line -----------------------
                  Code:
                  * Example generated by -dataex-. For more info, type help dataex
                  clear
                  input float(Procedureno Indexno HospitalD discharge episodeno)
                  20 1 17 14762 1
                   . 0 17 14396 2
                  21 1 20 14335 1
                  21 1 20 22743 2
                  end
                  format %td discharge
                  ------------------ copy up to and including the previous line ------------------

                  Listed 4 out of 4 observations

                  Code used:
                  bys HospitalD (Procedureno Indexno discharge): gen episodeno = _n

                  The final result of the code is above.
                  I thought this was secondary to the fact that perhaps I translated into into readable human form and therefore stata couldnt work it out
                  I tried the above again just with stata dates (not translating to human form) and it still didn't sort it out...

                  Comment


                  • #10
                    Perhaps I should post this on a separate thread ?
                    but As we’re talking about sorting dates I thought this would be relevant here

                    Comment

                    Working...
                    X