Announcement

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

  • Generate variable based on previous episode date

    Hi. My data has 3 varibales: ID, date of visit, visit number. I want to create a new variable that has the previous eval date so I can calculate the number of days since the previous date. Eg for the first ID, obs 2, I want to add a fourth variable that is the date of obs1. This is where I need help.

    My next step is do run datediff(evaldate, prevdate, "day") but I welcome input if there's a better way to calculate the difference. Thanks!

    * Example generated by -dataex-. For more info, type help dataex
    clear
    input long id str10 evaldate float epnumber
    5 "2020-06-07" 1
    5 "2020-09-07" 2
    9 "2020-03-21" 1
    9 "2020-05-23" 2
    9 "2020-05-24" 3
    9 "2020-06-21" 4
    9 "2020-06-28" 5
    9 "2020-07-05" 6
    9 "2020-09-16" 7
    9 "2021-02-28" 8
    9 "2021-03-04" 9
    Last edited by Paige Cohen; 05 Apr 2022, 14:03.

  • #2
    Update: I managed to do this with

    by id (evaldate), sort: gen prevdate = evaldate[_n-1]

    Hope this can help someone else!

    Comment


    • #3
      Yes, except that code won't run at all with your data because evaldate is, according to what you showed, a string variable. You have to convert that to a true Stata internal format numerical date variable in order to use that.
      Code:
      * Example generated by -dataex-. For more info, type help dataex
      clear
      input long id str10 evaldate float epnumber
      5 "2020-06-07" 1
      5 "2020-09-07" 2
      9 "2020-03-21" 1
      9 "2020-05-23" 2
      9 "2020-05-24" 3
      9 "2020-06-21" 4
      9 "2020-06-28" 5
      9 "2020-07-05" 6
      9 "2020-09-16" 7
      9 "2021-02-28" 8
      9 "2021-03-04" 9
      end
      
      //  CONVERT evaldate TO A STATA DATE VARIABLE
      gen _evaldate = daily(evaldate, "YMD")
      assert missing(_evaldate) == missing(evaldate)
      format _evaldate %td
      drop evaldate
      rename _evaldate evaldate
      
      by id (evaldate), sort: gen prevdate = evaldate[_n-1]
      But let me point out that if you have no other need for the prevdate variable, then you don't have to bother calculating it just to get the number of days. You can do that in one line with:
      Code:
      by id (evaldate), sort: gen days_since_previous = datediff(evaldate[_n-1], evaldate, "d")

      Comment

      Working...
      X