Announcement

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

  • Working with dates

    Hello,
    I am attempting to turn a string value "2020-2021" into a date value. Nothing I am doing is actually working, its only returning 4.490e+21 as the output. I have attached the code below:

    gen date2=date("2020-2021", "YY")

    format date2 %ty

    display date2


    I understand that the issue may lie in STATA using a reference date in 1960 for the date() function. I have no idea how to mediate this.

  • #2
    Since "2020-2021" is, itself, not a date, it is not possible to directly convert it to a Stata date. It is a range of years, not a date.

    I don't know how you plan to use this data, but it seems to me you have two ways of handling this. You can create two separate year variables demarcating the years encompassed in the range, or you could pick a date within that range, perhaps the midpoint:

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input str9 year_range
    "2020-2021"
    end
    
    split year_range, gen(yr) parse("-") destring
    rename yr1 start_year
    rename yr2 end_year
    
    gen mid_point = ceil(0.5*(mdy(1, 1, start_year) + mdy(12, 31, end_year)))
    format mid_point %td


    Comment


    • #3
      Clyde Schechter is naturally correct but leaves room for me to jump on a personal hobby-horse.

      The function date() does back to a time when it was introduced to support daily dates, yearly dates being usually easy enough: if you want years like 1984 or 2022 they are just integers and need no special support.

      Other kinds of dates (e.g. quarterly or monthly) and date-times came later. With those extra dates came daily() which is just the same code as date() underneath the name. But, but, but: it's evidently all too easy to imagine that date() is an all-purpose generic date function, even though the documentation nowhere says that and it's not true.

      I imagine StataCorp are in a bind here. Mentions of date() that I see vastly outnumber mentions of daily(), so StataCorp would just provoke puzzlement at best if they tried to push people, even gently, over to using daily(). I am not StataCorp, but I recommend that you and your friends do your bit for clarity and precision by using the daily() function for daily dates.

      Not, as explained by Clyde, that has anything do with the problem in #1 apart a mistake of using date().

      Comment


      • #4
        Stata's "date and time" variables are complicated and there is a lot to learn. If you have not already read the very detailed Chapter 24 (Working with dates and times) of the Stata User's Guide PDF, do so now. If you have, it's time for a refresher. After that, the help datetime documentation will usually be enough to point the way. You can't remember everything; even the most experienced users end up referring to the help datetime documentation or back to the manual for details. But at least you will get a good understanding of the basics and the underlying principles. An investment of time that will be amply repaid.

        All Stata manuals are included as PDFs in the Stata installation and are accessible from within Stata - for example, through the PDF Documentation section of Stata's Help menu.

        Comment

        Working...
        X