Announcement

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

  • Creating a New Variable for Time Length

    Dear all,

    I have a dataset, and I want to create a new variable, by calculating the days between two events (time length between two rows between _n and _n-1). I want to put these days in the row where the second event occurred. I provide an example by dataex below. May you help me to find a way to achieve this?

    I also provide a code for this that I got an error.
    William Lisowski might be interested in this.


    Best,

    Code:

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input str57 country int ccode float(startday startmonth startyear endday endmonth endyear between)
    "Canada" 20  1  7 1990  1  7 1990   .
    "Canada" 20 19  9 1990 19  9 1990   .
    "Canada" 20 14  8 1990 15  8 1990   .
    "Canada" 20 12  7 1990  6  9 1990  22
    "Canada" 20 25  6 1990 25  6 1990   .
    "Canada" 20 15  1 1990 15  1 1990   .
    "Canada" 20 10  9 1991 17  9 1991   .
    "Canada" 20 28  9 1991  2 10 1991   .
    "Canada" 20  4  5 1992  5  5 1992   .
    "Canada" 20 16  5 1993 16  5 1993   .
    "Canada" 20  1  7 1993 31  8 1993   .
    "Canada" 20 18 11 1994 18 11 1994   .
    "Canada" 20  1  9 1994  1  9 1994   .
    "Canada" 20 20  2 1995 20  2 1995   .
    "Canada" 20  8  9 1995  8  9 1995   .
    "Canada" 20 26 10 1996 26 10 1996   .
    "Canada" 20 28 10 1997  9 11 1997   .
    "Canada" 20 19 11 1997  4 12 1997   .
    "Canada" 20  .  .    .  .  .    .   .
    "Canada" 20  .  .    .  .  .    .   .
    "Canada" 20  9  5 2000  9  5 2000   .
    "Canada" 20 16  6 2000 16  6 2000   .
    "Canada" 20 26  2 2000 26  2 2000   .
    "Canada" 20 22  2 2000 23  2 2000   .
    "Canada" 20  .  .    .  .  .    .   .
    "Canada" 20  .  .    .  .  .    .   .
    "Canada" 20  3  5 2003  3  5 2003   .
    "Canada" 20  .  .    .  .  .    .   .
    "Canada" 20 10  9 2005 10  9 2005   .
    "Canada" 20  3  3 2005  3  4 2005   .
    "Canada" 20 21  2 2006 17  8 2006 501
    "Canada" 20 29  6 2007 29  6 2007   .
    "Canada" 20 10  8 2008 10  8 2008   .
    "Canada" 20 13  5 2009 13  5 2009   .
    "Canada" 20 10 11 2009 10 11 2009   .
    "Canada" 20 12  2 2010 13  2 2010  95
    "Canada" 20  3  7 2011  3  7 2011   .
    "Canada" 20 10  2 2012  6  6 2012   .
    "Canada" 20  .  .    .  .  .    .   .
    "Canada" 20  .  .    .  .  .    .   .
    "Canada" 20  6 10 2015  6 10 2015   .
    "Canada" 20  2  2 2016  2  2 2016   .
    "Canada" 20 25  2 2016 25  2 2016   .
    "Canada" 20 12  4 2016 12  4 2016   .
    "Canada" 20 13 10 2016 11 11 2016   .
    "Canada" 20 10  2 2016 10  2 2016   .
    "Canada" 20 20  3 2016  4  4 2016   .
    "Canada" 20 23  4 2016 23  4 2016   .
    "Canada" 20 24  3 2016 24  3 2016   .
    "Canada" 20 14  4 2016 14  4 2016   .
    "Canada" 20 30  7 2016 30  7 2016   .
    "Canada" 20  1 10 2017 26 10 2017   .
    "Canada" 20 17  3 2018 25  3 2018   .
    "Canada" 20  9  2 2018  9  2 2018   .
    "Canada" 20  7  1 2019  8  1 2019   .
    "Canada" 20  8  1 2019  8  1 2019   .
    "Canada" 20 18  6 2019 18  6 2019   .
    "Canada" 20 26  1 2019 26  1 2019   .
    "Canada" 20  6  7 2019  6  7 2019   .
    "Canada" 20 10  2 2020 24  2 2020   .
    
    end

    Code:
    gen newnewbetween = [endyear*365+ endmonth*30+ endday][_n] - [startyear*365+ startmonth*30+ startday][_n-1]
    Here, I want to calculate the day-length between two events and assign it two the last second row.
    Last edited by Nihat Mugurtay; 07 Jul 2022, 03:59.

  • #2
    Perhaps this will start you in a useful direction, and one more theoretically correct than assuming every month has 30 days and every year has 365 days.
    Code:
    generate start = mdy(startmonth, startday, startyear)
    generate end = mdy(endmonth, endday, endyear)
    format %td start end
    generate newbetween = end - start[_n-1]
    list start end between newbetween, clean noobs
    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


    • #3
      Dear William Lisowski ,

      Thank you so much, I will definitely check the Stata User manual again. Meanwhile, your code worked for me.

      Best,

      Nihat.

      Comment

      Working...
      X