Announcement

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

  • compare two dates, contradict results

    Dear STATA users,

    I have a dataset including starting date, stopping date, and treatment date. I need to compare those dates. But STATA did not show correct results when I compared starting date and treatment date.

    The first Fig. is code, and the second Fig. is the output.

    I could not figure out why this happened.

    Many thanks.

    Hui


    Click image for larger version

Name:	Screen Shot 2017-06-12 at 9.48.02 AM.png
Views:	1
Size:	287.4 KB
ID:	1397376


    Click image for larger version

Name:	Screen Shot 2017-06-12 at 9.46.40 AM.png
Views:	1
Size:	56.4 KB
ID:	1397377

















  • #2
    Welcome to the Stata Forum / Statalist.

    Instead of snapshots, I kindly suggest to present data as well as output as recommended in the FAQ.
    Best regards,

    Marcos

    Comment


    • #3
      Welcome to Statalist.

      The problem is that treatmentdate is a Stata Internal Format daily date, while startym and endym are Stata Internal Format monthly dates.

      To compare dates, they must be in the same Stata Internal Format.

      Before working with dates and times, any Stata user should thoroughly review the very detailed Chapter 24 (Working with dates and times) of the Stata User's Guide PDF. After that, the help datetime documentation will usually be enough to point the way. All Stata manuals are included as PDFs in the Stata installation (since version 11) and are accessible from within Stata - for example, through the PDF Documentation section of Stata's Help menu.

      With that said, something like
      Code:
      list main treatmentdate startym if mofd(treatmentdate) > startym
      might do what you want.

      A little further advice to make your future posts more effective. Please review the Statalist FAQ linked to from the top of the page, as well as from the Advice on Posting link on the page you used to create your post, looking especially at sections 9-12 on how to best pose your question. In particular, please read FAQ #12 and use dataex and CODE delimiters when posting to Statalist. Pictures are particularly unhelpful, they force the reader to retype text from the picture rather than enabling the text to be copied and pasted.

      Comment


      • #4
        Please see Stata FAQ Advice #12 on how to show data and CODE. No screenshots please!

        The problem here is that daily dates and monthly dates are in different units. What you have is like comparing mm and m or g or kg, only more complicated than that.

        For example, today's daily date is #days since 1 January 1960 and today's monthly date is #months since January 1960.

        Code:
        . di mdy(6,12,2017)
        20982
        
        . di ym(2017, 6)
        689
        What you want is something more like

        Code:
        ... if treatmentdate > mdy(month(mofd(treatmentdate)), 1, year(mofd(treatmentdate)))
        where 1 indicates the start of the month.


        Comment


        • #5
          Thank you for all the answers. Sorry for my late response. I will do a correct post next time.

          Comment

          Working...
          X