Announcement

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

  • calculate the difference between a date n and the first date of a long set

    Please I need to calculate the difference between a date n and the first date of a long set.
    I do not know how to specify the date 1

    example:
    id . date_edss edss
    Cemcat-10 04/01/2000 1.5
    Cemcat-10 27/12/2000 1
    Cemcat-10 22/12/2001 0
    Cemcat-10 17/12/2002 1
    Cemcat-10 12/12/2003 1
    Cemcat-10 06/12/2004 0
    Cemcat-10 01/12/2005 1
    Cemcat-10 26/11/2006 1
    Cemcat-10 21/11/2007 1
    Cemcat-10 15/11/2008 1
    Cemcat-10 05/11/2010 0
    Cemcat-10 20/10/2013 0

    excuse me, I'm new to the list.
    thansk

  • #2
    Check out -dataex- and provide an example of your data we can work with.

    Then how do you want the difference? In days?

    Comment


    • #3
      Augusto, welcome to Statalist!

      Here is sample data from a different Stata post that might illustrate how to do it. If this doesn't do what you want, feel free to come back and ask additional questions.

      As Joro mentions, it is *far* easier for us to help you if you post a sample of your data using Stata's dataex command. If you need help using dataex, I created a Youtube tutorial here. (I made it too long--feel free to watch at 2x speed)

      Code:
      dataex id date_admitted  // To install: ssc install dataex
      clear
      input long id float date_admitted
      12354 19421
      12354 19881
      12354 19943
      12354 20078
      43567 19361
      43567 20073
      43567 20078
      74333 20575
      74333 20940
      74333 21305
      74333 21311
      93231 20575
      end
      format %td date_admitted
      
      * NOTE: Dates have to be in Stata format for this to work (help datetime translation)
      sort id date_admit
      egen first_date = min(date_admitted), by(id)
      egen last_date  = max(date_admitted), by(id)
      gen days_elapsed = date_admitted - first_date
      format first_date last_date %td
      
      . list, sepby(id) abbrev(14) noobs
      
        +---------------------------------------------------------------+
        |    id   date_admitted   first_date   last_date   days_elapsed |
        |---------------------------------------------------------------|
        | 12354       04mar2013    04mar2013   21dec2014              0 |
        | 12354       07jun2014    04mar2013   21dec2014            460 |
        | 12354       08aug2014    04mar2013   21dec2014            522 |
        | 12354       21dec2014    04mar2013   21dec2014            657 |
        |---------------------------------------------------------------|
        | 43567       03jan2013    03jan2013   21dec2014              0 |
        | 43567       16dec2014    03jan2013   21dec2014            712 |
        | 43567       21dec2014    03jan2013   21dec2014            717 |
        |---------------------------------------------------------------|
        | 74333       01may2016    01may2016   07may2018              0 |
        | 74333       01may2017    01may2016   07may2018            365 |
        | 74333       01may2018    01may2016   07may2018            730 |
        | 74333       07may2018    01may2016   07may2018            736 |
        |---------------------------------------------------------------|
        | 93231       01may2016    01may2016   01may2016              0 |
        +---------------------------------------------------------------+
      Last edited by David Benson; 01 Feb 2019, 15:36.

      Comment


      • #4
        Dear David and Joro.
        The syntax has worked perfectly for me.
        I'm very grateful.

        Comment

        Working...
        X