Announcement

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

  • Calculating days between two dates

    I'm trying to calculate the duration between two dates?
    My variables are like this
    Code:
     
     DateBecame
    This is a long type, format is %10.0g, and looks like the following
    Code:
     
     19550101
    So YMD format

    My other variable
    Code:
     
     DateLeft
    Is also a long type, format %10.0g and looks like this
    Code:
     
     19961001
    Any help would be much appreciated!

    Thanks in advance

  • #2
    Try something like
    Code:
    generate int delta = date(string(DateLeft, "%10.0g"), "YMD") - date(string(DateBecame, "%10.0g"), "YMD")

    Comment


    • #3
      Cross-posted at http://stackoverflow.com/questions/4...wo-dates-stata

      Please note our policy on cross-posting, which is that you should tell us about it. http://www.statalist.org/forums/help#crossposting

      Comment


      • #4
        Joseph gives very good advice, which I would extend: all date manipulations with these variables will be awkward unless and until you create daily date variables to work with. Thus I'd revise the advice to


        Code:
        gen SDateLeft = daily(string(DateLeft, "%10.0g"), "YMD")
        gen SDateBecame = daily(string(DateBecame, "%10.0g"), "YMD")
        format SDate* %td  
        generate int delta = SDateLeft - SDateBecame
        Now you have two Stata daily date variables that could be useful for other purposes.

        Note that daily() is just the same function as date(), but I recommend using daily() as a more precise reminder of what it does.

        Comment


        • #5
          Nick and Joseph,

          That did the trick!!

          I apologize about the cross-posting. I'll definitely make a note of that.

          Thank you so much for helping out

          Comment

          Working...
          X