Announcement

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

  • How to calculate time intervals between two consecutive variables

    I have about 120 subject ids, each has 7 different lab tests, some of the tests are done at a different dates, l have a column with the dates of each test. I need to calculate the interval between each consecutive test in each column.
    for example alt i need the duration between each alt test for the specific subject
    Is there a way to calculate this with all variables or it should be done separately one variable at a time?
    Thanks
    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input int id byte ast int(plt alk) double direct_bili byte(alt bilirubinindirectexternallab ggt) int results_date
      352 42 214  99  . 53 .  . 20825
      352 40   .   .  . 65 .  . 21070
      352 36 190  90 .2 58 . 40 21385
      352 47 286 138 .1 38 .  . 21623
     5759 40   . 128  . 35 .  . 21728
     5759 76   . 102  . 49 .  . 21740
     5759  . 200   .  .  . .  . 21747
    26570  . 295   .  .  . .  . 21756
    26570 38   . 106  . 40 .  . 21903
    26570 19 192  75 .3 21 . 53 22134
    26570 29 241  84  . 34 . 34 22735
    32659 21   .  88  . 25 .  . 22946
    32659 24 282  75  . 25 .  . 20825
    32659  . 286   .  .  . .  . 21108
    32659 22   .   .  . 22 .  9 21126
    end
    format %tddd-Mon-YY results_date

  • #2
    Actually calculating the intervals would be very easy if the data were in long layout instead of wide. (This, of course, is true of most things in Stata data management and analysis.) The barrier to getting there is you have some variable names that are difficult to work with. So a few -rename-s have to come first.

    Code:
    rename bilirubinindirectexternallab biliindextlab // TOO LONG
    rename results_date date // WILL CLASH WITH DESIRED NAME OF VARIABLE CONTAINING RESULT
    rename (ast-ggt) (result=)
    reshape long result, i(id date) j(test) string
    drop if missing(result)
    
    isid id test date, sort
    by id test (date): gen interval = date-date[_n-1]
    Now, I don't know how you plan to use this data, so, while I would wager a small amount that you are better off keeping it in the long layout going forward, I can foresee some use cases where you would be better off going back to wide. If you do find that to be the case, you can do that by following this code with:
    Code:
    rename (result interval) =_
    reshape wide result_ interval_, i(id date) j(test) string

    Comment

    Working...
    X