Announcement

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

  • Difference between two dates

    I have dates in format
    Code:
    /*YYYYMMDD
    How can I get the difference between the wto dates without turning them into numbers we cannot direct interpret?

  • #2
    As pointed out in another thread, "format" in Stata means display format. Despite that I guess that you mean that you have daily dates which are integers like 20150612. I don't know what you mean by /* here.

    Sooner or later you will need to stop inventing or discovering ad hoc techniques for such dates, and use Stata's date functions. Despite the innuendo, Stata dates are easy to interpret once you have specified appropriate date formats.

    Here is an example:

    Code:
    . di daily(string(20150612, "%8.0f"), "YMD") - daily(string(20140612, "%8.0f"), "YMD")
    365
    The conversion steps are thus.

    1. If your dates are integers, convert them to strings.

    2. Convert such strings to numeric dates that Stata can understand using daily() (its longer-lasting synonym is date())

    3. The difference between any two dates is just a number in whatever time units are being used.

    Your data are presumably variables, so you would need something like

    Code:
    gen sdate = daily(string(mydate, "%8.0f"), "YMD"))
    
    format sdate %td
    for each such date variable.

    Comment


    • #3
      To Nick's answer, let me add that I cannot too strongly recommend becoming very familiar with the material in Chapter 24 of the Stata User's Guide, "Working with dates and times." As Nick suggests, any attempt to deal with anything more complicated than whole years by means other than what Stata expects will simply make more work for you. That chapter, and the output of help datetime when I need a quick reminder, are my constant companions. I suspect you have not become familiar with that information, or you would not be trying to avoid SIF dates.

      More generally, when I began using Stata in a serious way last fall, I started by reading my way through the Getting Started manual relevant to my setup. Chapter 18 of the Getting Started manual gives suggested further reading, much of which is in the Stata User's Guide, and I worked my way through much of that reading as well. The "Programming Stata" chapter of the User's Guide was especially helpful as my do-files become more complex. All of these manuals are included as PDFs in the Stata installation and are accessible from within Stata - for example, through Stata's Help menu on the Mac.

      Comment

      Working...
      X