Announcement

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

  • Dealing with dates in double and float format

    Dear Community,

    I encounter a problem when merging two files. In the first file date is displayed as double e.g. 30sep2021. In the second file I have only years and quarters in float in format %tq, e.g. 2021q3. I would like to convert the date in the first file to quarter year format. I scanned the article https://www.stata-journal.com/articl...article=dm0098 in search for the answer. However, I'm not successful. I type for the first file:
    gen t=dofc(date)
    Then I wanted to type:
    format t %tq

    But I realised that my t is 0. So stata treats e.g 31dec2017 as 1960:/
    Last edited by sladmin; 28 Aug 2023, 09:38. Reason: anonymize original poster

  • #2
    dofc() converts from datetime to date, qofd() converts from date to quarter,
    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input double date
    22553
    end
    format %td date
    
    gen double qt = qofd(date)
    gen double qt2 = qofd(dofc(cofd(date)))
    format %tq qt qt2
    
    list, clean
    
                date       qt      qt2  
      1.   30sep2021   2021q3   2021q3
    Last edited by Øyvind Snilsberg; 22 Apr 2022, 04:34.

    Comment


    • #3
      Some general remarks about the data storage types for dates and times. Double should be the storage type for datetimes, whereas integer (int) is sufficient for all dates (daily dates, quarters, months, etc). Using more accommodating types for dates (e.g., long, float, double) won't cause any harm because there is no risk of losing data by using these types, nor is the extra precision useful. However, you will lose information if you try to store datetimes is anything other than double, and that can lead to subtle errors that are hard to diagnose.

      Comment


      • #4
        The problem with #1 is that changing the display format doesn’t change the stored value; hence it is not a way to effect a change of date type. There was a Tip in the Stata Journal
        on this. I will add a precise reference when next able unless someone else gets there first.

        Comment


        • #5
          https://www.stata-journal.com/articl...article=dm0067

          Comment

          Working...
          X