Announcement

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

  • calculus long (patients treated in a time window)

    Hello everyone.
    We are working on a long format database with patient treatments. We need to identify:
    1.-The treatment that the patient had at a moment in time marked by the REFERENCE_DATE variable. with the date_start and date_stop corresponding to said treatment
    2.- The treatment 1 year before the REFERENCE_DATE with the date_start and date_stop corresponding to said treatment.

    Thank you very much for all the help.

    clear
    input double REFERENCE_DATE float(date_start date_stop) str21 alias str8 no_treat float id
    22166.79127314815 19768 20342 "Interferon Beta-1a" "" 1
    22166.79127314815 20349 21663 "Dimethyl fumarate" "" 1
    22166.79127314815 21671 . "Rituximab" "" 1
    22047.680081018516 16997 . "Interferon Beta-1a" "" 2
    22047.702465277776 . . "" "No-treat" 3
    22049.722025462965 . . "" "No-treat" 4
    22060.65284722222 . . "" "No-treat" 5
    22046.94578703704 20439 20653 "Peginterferon Beta-1a" "" 6
    22046.94578703704 20667 . "Alemtuzumab" "" 6
    22046.84505787037 16847 20096 "Interferon Beta-1a" "" 7
    22046.84505787037 20103 21080 "Dimethyl fumarate" "" 7
    22046.84505787037 21503 22084 "Dimethyl fumarate" "" 7
    22046.84505787037 22144 . "Ocrelizumab" "" 7
    22048.214120370372 . . "" "No-treat" 8
    22047.348738425928 22039 . "Dimethyl fumarate" "" 9
    22162.813275462962 . . "" "No-treat" 10
    22046.838946759257 . . "" "No-treat" 11
    22060.685555555552 . . "" "No-treat" 12
    22053.709814814814 19769 . "Interferon Beta-1a" "" 13
    22060.825636574074 . . "" "No-treat" 14
    22046.946851851855 21927 . "Teriflunomide" "" 15
    22046.885474537034 15751 17275 "Interferon Beta-1a" "" 16
    22046.885474537034 17275 17660 "Mitoxantrone" "" 16
    22046.885474537034 17846 20436 "Glatiramer Acetate" "" 16
    22046.885474537034 20491 20496 "Alemtuzumab" "" 16
    22046.885474537034 20856 20858 "Alemtuzumab" "" 16
    22046.885474537034 22657 . "Rituximab" "" 16
    22060.678912037038 . . "" "No-treat" 17
    22165.468090277776 22043 22756 "Natalizumab" "" 18
    22165.468090277776 22789 . "Ocrelizumab" "" 18
    22046.939884259256 17920 21145 "Interferon Beta-1b" "" 19
    22046.939884259256 21151 23153 "Natalizumab" "" 19
    22046.939884259256 23181 . "Ocrelizumab" "" 19
    22046.844710648147 18642 20917 "Interferon Beta-1a" "" 20
    22046.844710648147 20924 22616 "Teriflunomide" "" 20
    22046.844710648147 22670 . "Ocrelizumab" "" 20
    22046.853530092594 . . "" "No-treat" 21
    22047.606782407405 . . "" "No-treat" 22
    22166.51505787037 . . "" "No-treat" 23
    22047.352013888885 17119 20142 "Interferon Beta-1b" "" 24
    22047.352013888885 . . "NA" "No-treat" 24
    22046.904953703706 21734 21772 "Cladribine" "" 25
    22046.904953703706 22119 22153 "Cladribine" "" 25
    22063.81465277778 21945 . "Dimethyl fumarate" "" 26
    22046.88846064815 21958 . "Teriflunomide" "" 27
    22049.80988425926 15627 20349 "Interferon Beta-1a" "" 28
    22049.80988425926 17498 17846 "Teriflunomide" "" 28
    22049.80988425926 20349 . "Teriflunomide" "" 28
    22060.767638888887 . . "" "No-treat" 29
    22047.38324074074 . . "" "No-treat" 30
    22060.670057870368 15522 17362 "Interferon Beta-1a" "" 31
    22060.670057870368 17590 . "Interferon Beta-1a" "" 31
    22047.487812500003 15301 20254 "Interferon Beta-1a" "" 32
    22047.487812500003 20286 21533 "Dimethyl fumarate" "" 32
    22047.487812500003 21570 . "Ocrelizumab" "" 32
    22047.626388888886 22453 . "Dimethyl fumarate" "" 33
    22061.35744212963 17835 21868 "Interferon Beta-1a" "" 34
    22046.872719907406 21672 21825 "Teriflunomide" "" 35
    22046.872719907406 21846 22480 "Dimethyl fumarate" "" 35
    22046.872719907406 22250 . "NA" "" 35
    22046.872719907406 22487 . "Ocrelizumab" "" 35
    22060.844837962963 16091 22238 "Interferon Beta-1a" "" 36
    22060.844837962963 22293 . "Teriflunomide" "" 36
    22163.432962962965 . . "" "No-treat" 37
    22161.850555555553 . . "" "No-treat" 38
    22060.663518518515 . . "" "No-treat" 39
    22046.832384259258 14921 22432 "Interferon Beta-1a" "" 40
    22046.832384259258 22439 . "Ocrelizumab" "" 40
    22046.86273148148 . . "" "No-treat" 41
    22047.42271990741 . . "" "No-treat" 42
    22046.87584490741 21635 . "Ocrelizumab" "" 43
    22046.845671296294 21781 . "Dimethyl fumarate" "" 44
    22046.881782407407 21476 . "Rituximab" "" 45
    22161.850613425922 21035 23072 "Teriflunomide" "" 46
    22161.850613425922 23099 . "Ofatumumab" "" 46
    22161.850613425922 . . "NA" "No-treat" 46
    22161.850613425922 . . "NA" "No-treat" 46
    22060.716770833336 16967 . "Interferon Beta-1b" "" 47
    22046.956388888888 . . "" "No-treat" 48
    22050.52585648148 22008 . "Natalizumab" "" 49
    22060.76650462963 18063 18421 "Interferon Beta-1a" "" 50
    22060.76650462963 18428 18659 "Interferon Beta-1a" "" 50
    22060.76650462963 21357 21397 "Dimethyl fumarate" "" 50
    22060.76650462963 21497 . "Natalizumab" "" 50
    22164.54167824074 21481 21579 "Dimethyl fumarate" "" 51
    22164.54167824074 21622 21689 "Glatiramer Acetate" "" 51
    22164.54167824074 21859 21879 "Glatiramer Acetate" "" 51
    22060.68136574074 . . "" "No-treat" 52
    22060.712326388886 21144 21564 "Interferon Beta-1a" "" 53
    22060.712326388886 21571 22189 "Fingolimod" "" 53
    22060.712326388886 22238 . "Natalizumab" "" 53
    22047.476793981485 14563 17448 "Interferon Beta-1a" "" 54
    22047.476793981485 17455 17485 "Natalizumab" "" 54
    22047.476793981485 17568 17920 "Mitoxantrone" "" 54
    22047.476793981485 18021 18085 "Glatiramer Acetate" "" 54
    22047.476793981485 19809 21035 "Teriflunomide" "" 54
    22047.476793981485 21089 22263 "Fingolimod" "" 54
    22047.476793981485 22294 . "Ocrelizumab" "" 54
    22046.813900462963 . . "" "No-treat" 55
    22047.430833333332 13284 21641 "Interferon Beta-1b" "" 56
    end
    format %td REFERENCE_DATE
    format %dD_m_CY date_start
    format %dD_m_CY date_stop

  • #2
    It appears you are not using version 18. I say that because alias, a reserved Stata word, is not a legal variable name in version 18, but, I believe, it was in earlier versions. I think the following code does not require version 18, but I'm not certain about the use of the -birthday()- function. I don't remember when that was introduced.

    Code:
    assert !missing(no_treat) == (missing(date_start) & missing(date_stop))
    sort id, stable
    by id: gen byte is_reference = inrange(REFERENCE_DATE, date_start, date_stop) ///
        if missing(no_treat)
        
    sort id is_reference, stable
    by id (is_reference): gen reference_rx = Alias[_N]
    by id (is_reference): gen ref_start = date_start[_N]
    by id (is_reference): gen ref_stop = date_stop[_N]
    format ref_start ref_stop %td
    
    gen ref_plus_1_yr = birthday(REFERENCE_DATE, year(REFERENCE_DATE)+1)
    by id: gen byte is_ref_plus_1 = inrange(ref_plus_1_yr, date_start, date_stop) ///
        if missing(no_treat)
    sort id is_ref_plus_1, stable
    by id (is_ref_plus_1): gen ref_plus_1_rx = Alias[_N]
    by id (is_ref_plus_1): gen ref_plus_1_start = date_start[_N]
    by id (is_ref_plus_1): gen ref_plus_1_stop = date_stop[_N]
    format ref_plus_1_start ref_plus_1_stop %td
    Notes: In your example data, the intervals from date_start to date_stop for any patient are non-overlapping, so there will never be multiple observations that cover the reference date (nor its 1 year anniversary). But if in the full data set there are situations where there is more than one observation covering the reference date (or its anniversary), this code will select one of them (the one that is listed last in the data at the start) for the purpose.

    When date_start is not missing and date_stop is missing, this code interprets that to mean that treatment began on date_start and is still continuing at present.

    If your version of Stata does not have the -birthday()- function, replace that expression with REFERENCE_DATE + 365. That will slightly mishandle leap year problems, but I imagine that for your purposes that will be a negligible difficulty. If you don't have -birthday()- and the leap year issue bites, post back and I'll suggest a lengthier workaround.

    Because alias is not a legal variable name in Stata 18 (the version I am running), I have renamed it Alias. You don't need to do that if you don't want to. You can just replace Alias by alias everywhere it occurs in the code.

    Comment


    • #3
      Thank you very much Clyde. I have version 15. I test the code. Thank you very much again

      Comment

      Working...
      X