Announcement

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

  • How can I get difference between two dates in days

    Hello,
    I want to get the difference between these two dates (Fenddt - Rptdt) in days at a new variable called days



    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str10 Stkcd str24 Rptdt str20 Fenddt
    "000001" "2002-06-28" "2002-12-31"
    "000001" "2002-07-18" "2002-12-31"
    "000001" "2003-04-04" "2003-12-31"
    "000001" "2003-06-04" "2003-12-31"
    "000001" "2004-03-29" "2004-12-31"
    "000001" "2004-04-15" "2004-12-31"
    "000001" "2004-04-15" "2004-12-31"
    "000001" "2004-04-15" "2005-12-31"
    "000001" "2004-04-15" "2005-12-31"
    "000001" "2004-06-29" "2004-12-31"
    "000001" "2004-06-29" "2005-12-31"
    "000001" "2004-08-18" "2004-12-31"
    "000001" "2004-08-18" "2005-12-31"
    "000001" "2004-08-18" "2006-12-31"
    "000001" "2005-05-09" "2005-12-31"
    "000001" "2005-05-09" "2006-12-31"
    "000001" "2005-05-09" "2007-12-31"
    "000001" "2005-07-31" "2005-12-31"
    "000001" "2005-07-31" "2006-12-31"
    "000001" "2005-07-31" "2007-12-31"
    "000001" "2005-08-19" "2005-12-31"
    "000001" "2005-08-19" "2006-12-31"
    "000001" "2005-08-19" "2007-12-31"
    "000001" "2005-09-29" "2005-12-31"
    "000001" "2005-09-29" "2006-12-31"
    "000001" "2005-11-01" "2005-12-31"
    "000001" "2005-11-01" "2006-12-31"
    "000001" "2005-11-01" "2007-12-31"
    "000001" "2005-11-06" "2006-12-31"
    "000001" "2005-11-06" "2007-12-31"
    "000001" "2005-11-06" "2008-12-31"
    "000001" "2006-01-11" "2005-12-31"
    "000001" "2006-01-11" "2006-12-31"
    "000001" "2006-02-14" "2005-12-31"
    "000001" "2006-02-22" "2005-12-31"
    "000001" "2006-02-22" "2006-12-31"
    "000001" "2006-03-08" "2005-12-31"
    "000001" "2006-03-08" "2006-12-31"
    "000001" "2006-04-04" "2006-12-31"
    "000001" "2006-04-04" "2007-12-31"
    "000001" "2006-04-04" "2008-12-31"
    "000001" "2006-04-10" "2006-12-31"
    "000001" "2006-04-10" "2007-12-31"
    "000001" "2006-04-19" "2006-12-31"
    "000001" "2006-04-19" "2007-12-31"
    "000001" "2006-04-27" "2006-12-31"
    "000001" "2006-04-27" "2007-12-31"
    "000001" "2006-04-27" "2008-12-31"
    "000001" "2006-05-28" "2006-12-31"
    "000001" "2006-05-28" "2007-12-31"
    "000001" "2006-05-30" "2006-12-31"
    "000001" "2006-05-30" "2007-12-31"
    "000001" "2006-06-12" "2006-12-31"
    "000001" "2006-06-12" "2007-12-31"
    "000001" "2006-06-12" "2008-12-31"
    "000001" "2006-06-15" "2006-12-31"
    "000001" "2006-06-15" "2007-12-31"
    "000001" "2006-07-04" "2006-12-31"
    "000001" "2006-07-04" "2007-12-31"
    "000001" "2006-08-17" "2006-12-31"
    "000001" "2006-08-17" "2007-12-31"
    "000001" "2006-08-23" "2006-12-31"
    "000001" "2006-08-23" "2007-12-31"
    "000001" "2006-08-23" "2008-12-31"
    "000001" "2006-10-13" "2006-12-31"
    "000001" "2006-10-13" "2007-12-31"
    "000001" "2006-10-26" "2006-12-31"
    "000001" "2006-10-26" "2007-12-31"
    "000001" "2006-10-27" "2006-12-31"
    "000001" "2006-10-27" "2007-12-31"
    "000001" "2006-10-27" "2008-12-31"
    "000001" "2006-11-06" "2006-12-31"
    "000001" "2006-11-06" "2007-12-31"
    "000001" "2006-11-06" "2008-12-31"
    "000001" "2007-02-05" "2006-12-31"
    "000001" "2007-02-05" "2007-12-31"
    "000001" "2007-02-05" "2008-12-31"
    "000001" "2007-03-22" "2007-12-31"
    "000001" "2007-03-22" "2007-12-31"
    "000001" "2007-03-22" "2007-12-31"
    "000001" "2007-03-22" "2008-12-31"
    "000001" "2007-03-22" "2008-12-31"
    "000001" "2007-03-22" "2009-12-31"
    "000001" "2007-03-23" "2007-12-31"
    "000001" "2007-03-23" "2008-12-31"
    "000001" "2007-03-23" "2009-12-31"
    "000001" "2007-05-16" "2007-12-31"
    "000001" "2007-05-17" "2007-12-31"
    "000001" "2007-06-19" "2007-12-31"
    "000001" "2007-06-19" "2008-12-31"
    "000001" "2007-06-19" "2009-12-31"
    "000001" "2007-06-20" "2007-12-31"
    "000001" "2007-06-20" "2008-12-31"
    "000001" "2007-06-20" "2009-12-31"
    "000001" "2007-08-15" "2007-12-31"
    "000001" "2007-08-15" "2008-12-31"
    "000001" "2007-08-16" "2007-12-31"
    "000001" "2007-08-16" "2008-12-31"
    "000001" "2007-08-17" "2007-12-31"
    "000001" "2007-08-17" "2008-12-31"
    end

  • #2
    Code:
    gen difference = daily(Fenddt, "YMD") - daily(Rptdt, "YMD")
    Note as a more general point that daily dates held as strings are informative but essentially useless for calculations. You need to push them through daily() (or date() -- which is the same functionality under another name).

    Comment


    • #3
      Originally posted by Nick Cox View Post
      Code:
      gen difference = daily(Fenddt, "YMD") - daily(Rptdt, "YMD")
      Note as a more general point that daily dates held as strings are informative but essentially useless for calculations. You need to push them through daily() (or date() -- which is the same functionality under another name).
      Thanks very much, for the below dataset, if I want to calculate the difference (ListedDate-EstablishDate) in ages. I try to use
      Code:
      personage EstablishDate ListedDate , gen(age1)
      but it gives me string variables not allowed in varlist; EstablishDate is a string variable. Could you help me in to get it



      Code:
      * Example generated by -dataex-. To install: ssc install dataex
      clear
      input str10 Stkcd str24 EstablishDate str20 ListedDate
      "000001" "1987-12-22" "1991-04-03"
      "000002" "1988-11-01" "1991-01-29"
      "000003" "1988-02-08" "1991-07-03"
      "000004" "1986-05-05" "1991-01-14"
      "000005" "1990-02-01" "1990-12-10"
      "000006" "1989-04-01" "1992-04-27"
      "000007" "1988-11-21" "1992-04-13"
      "000008" "1989-10-11" "1992-05-07"
      "000009" "1990-09-01" "1991-06-25"
      "000010" "1988-12-13" "1995-10-27"
      "000011" "1983-01-17" "1992-03-30"
      "000012" "1984-09-10" "1992-02-28"
      "000013" "1992-03-18" "1992-05-06"
      "000014" "1992-04-21" "1992-06-02"
      "000015" "1992-05-08" "1992-06-25"
      "000016" "1980-10-01" "1992-03-27"
      "000017" "1992-03-12" "1992-03-31"
      "000018" "1992-05-09" "1992-06-16"
      "000019" "1992-08-06" "1992-10-12"
      "000020" "1992-03-20" "1992-04-28"
      "000021" "1993-12-29" "1994-02-02"
      "000022" "1993-03-21" "1993-05-05"
      "000023" "1984-09-18" "1993-04-29"
      "000024" "1993-04-10" "1993-06-07"
      "000025" "1993-04-18" "1993-06-21"
      "000026" "1993-04-18" "1993-06-03"
      "000027" "1993-06-02" "1993-09-03"
      "000028" "1993-07-03" "1993-08-09"
      "000029" "1993-08-15" "1993-09-15"
      "000030" "1993-08-28" "1993-09-29"
      "000031" "1993-09-26" "1993-10-08"
      "000032" "1993-09-26" "1993-10-28"
      "000033" "1993-11-15" "1994-01-03"
      "000034" "1993-12-18" "1994-05-09"
      "000035" "1994-01-08" "1994-04-08"
      "000036" "1994-01-29" "1994-06-17"
      "000037" "1994-04-19" "1994-07-01"
      "000038" "1994-02-02" "1994-08-08"
      "000039" "1992-09-30" "1994-04-08"
      "000040" "1994-06-15" "1994-08-08"
      "000042" "1994-09-13" "1994-09-21"
      "000043" "1994-09-08" "1994-09-28"
      "000045" "1994-08-06" "1994-08-15"
      "000046" "1989-01-04" "1994-09-12"
      "000047" "1994-09-28" "1994-10-21"
      "000048" "1994-09-17" "1994-11-01"
      "000049" "1995-02-18" "1995-03-20"
      "000050" "1995-02-26" "1995-03-15"
      "000055" "1994-03-31" "1996-04-15"
      "000056" "1993-03-18" "1996-07-08"
      "000058" "1996-07-16" "1996-12-26"
      "000059" "1997-01-18" "1997-01-30"
      "000060" "1994-06-06" "1997-01-23"
      "000061" "1994-01-19" "1997-01-10"
      "000062" "1994-01-19" "1997-01-30"
      "000063" "1997-11-11" "1997-11-18"
      "000065" "1984-12-01" "1998-06-05"
      "000066" "1997-06-21" "1997-06-26"
      "000068" "1997-06-06" "1997-06-11"
      "000069" "1997-09-02" "1997-09-10"
      "000070" "1997-07-29" "2000-05-11"
      "000078" "1992-12-13" "1998-12-18"
      "000088" "1997-07-21" "1997-07-28"
      "000089" "1998-04-10" "1998-04-20"
      "000090" "1993-12-06" "1999-07-21"
      "000096" "1999-02-18" "2000-07-24"
      "000099" "1999-02-11" "2000-07-31"
      "000100" "2002-04-19" "2004-01-30"
      "000150" "1993-02-19" "2000-08-07"
      "000151" "1999-03-01" "2000-09-06"
      "000153" "1997-08-30" "2000-09-20"
      "000155" "1997-10-20" "2000-09-26"
      "000156" "1994-06-30" "2000-09-06"
      "000157" "1999-08-01" "2000-10-12"
      "000158" "1998-12-29" "2000-07-24"
      "000159" "1999-03-28" "2000-09-26"
      "000166" "1996-09-16" "2015-01-26"
      "000301" "1998-07-16" "2000-05-29"
      "000333" "2000-04-07" "2013-09-18"
      "000338" "2002-12-23" "2007-04-30"
      "000400" "1993-03-15" "1997-04-18"
      "000401" "1994-05-08" "1996-06-14"
      "000402" "1995-11-08" "1996-06-26"
      "000403" "1993-12-28" "1996-06-28"
      "000404" "1994-06-16" "1996-06-19"
      "000405" "1992-08-10" "1996-06-20"
      "000406" "1993-05-08" "1996-06-28"
      "000407" "1994-05-11" "1996-07-03"
      "000408" "1996-06-25" "1996-06-28"
      "000409" "1993-04-28" "1996-06-27"
      "000410" "1993-05-20" "1996-07-18"
      "000411" "1993-04-01" "1996-07-16"
      "000412" "1993-06-28" "1996-07-15"
      "000413" "1992-12-26" "1996-09-25"
      "000415" "1993-06-30" "1996-07-16"
      "000416" "1993-06-12" "1996-07-19"
      "000417" "1993-10-19" "1996-08-12"
      "000418" "1993-11-29" "1997-03-28"
      "000419" "1996-08-10" "1996-08-16"
      "000420" "1993-05-08" "1996-08-02"
      end

      Comment


      • #4
        You already have an answer -- which you quote back at us.. Use daily() to do that.


        daily dates held as strings are informative but essentially useless for calculations. You need to push them through daily() (or date() -- which is the same functionality under another name).
        That is, the difference in days you ask for is only easy if you get it by subtracting one Stata daily date from another.

        For other purposes you just need to put them in new variables too.

        personage is from SSC and superseded by age functions added in Stata 16 on 5 November 2020. As explained in the FAQ Advice, you're assumed to be using Stata 17 unless you tell us otherwisel

        Comment


        • #5
          Originally posted by Nick Cox View Post
          You already have an answer -- which you quote back at us.. Use daily() to do that.




          That is, the difference in days you ask for is only easy if you get it by subtracting one Stata daily date from another.

          For other purposes you just need to put them in new variables too.

          personage is from SSC and superseded by age functions added in Stata 16 on 5 November 2020. As explained in the FAQ Advice, you're assumed to be using Stata 17 unless you tell us otherwisel
          I use Stata 16. Actually, I used the daily but what i wanted in this example in years, I try to divided days by 365 but sure not completely accurate

          Comment


          • #6
            I used the daily but what i wanted in this example in years, I try to divided days by 365 but sure not completely accurate
            Sorry for crashing the party, but I can't resist commenting on this. Taking the difference in years and dividing days by 365 to get years is actually much more accurate for most purposes than using the age() function. Consider. Where I am located, today is 6 October 2021. A person born on 6 October 2011 is considered ten years old using the age function. But so is a person born 7 October 2010--even though the latter has lived 364 days longer than the former. That difference would be picked up by calculating the difference in days and dividing by 365. If you want to make it a bit more accurate, allowing for leap years, you can divide instead by 365.25 (which is the usual practice in my field, epidemiology).

            In fact, age truncated the the integer part, which is the colloquial use of the word age, and what the age() function calculates, is, as seen by this example, quite coarse and inaccurate. Its main use in data analysis would be identifying which side of some specified, usually somewhat arbitrary, age limit (21 to drink alcohol in the US, 65 to get Medicare in the US, 12 to get a Covid vaccine in the US) people lie on.

            Comment


            • #7
              I am sure that Clyde Schechter is aware of this, but I will point out that in addition to the -age()- function, there is an -age_frac()- function that will provide age in years, including the fractional part. This should be even more accurate than dividing by 365 or 365.25.

              Code:
              di age(`=td(8oct2010)', `=td(7oct2021)')
              di age(`=td(7oct2011)', `=td(7oct2021)')
              di age_frac(`=td(8oct2010)', `=td(7oct2021)')
              di age_frac(`=td(7oct2011)', `=td(7oct2021)')
              di (-`=td(8oct2010)'+ `=td(7oct2021)')/365
              di (-`=td(8oct2010)'+ `=td(7oct2021)')/365.25
              Res.:

              Code:
              . di age(`=td(8oct2010)', `=td(7oct2021)')
              10
              
              . 
              . di age(`=td(7oct2011)', `=td(7oct2021)')
              10
              
              . 
              . di age_frac(`=td(8oct2010)', `=td(7oct2021)')
              10.99726
              
              . 
              . di age_frac(`=td(7oct2011)', `=td(7oct2021)')
              10
              
              . 
              . di (-`=td(8oct2010)'+ `=td(7oct2021)')/365
              11.005479
              
              . 
              . di (-`=td(8oct2010)'+ `=td(7oct2021)')/365.25
              10.997947

              Comment


              • #8
                Andrew Musau Yes, I am aware of the age_frac() function. But it slipped my mind when I wrote that post. Thanks for reminding me of it.

                Comment

                Working...
                X