Announcement

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

  • Merge using date range.

    Dear All,
    I want to merge two patient level data sets, the first containing hospitalizations for cirrhosis and the second including liver function lab tests. Both datasets includes the patient ids.

    I want to merge the two at the patient level to create dummy for whether the patient had an abnormal lab test value within 2 weeks prior to admission date or within 1 week post admission date. I know we cannot merge using an if statement.

    Here is the dataex for the hospitalization data:


    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input long id float(admit_date disch_date)
    221 22399 22399
    221 20971     .
    221 22459 22460
    221 21519 21519
    221 22445     .
    221 22187 22187
    221 20634 20634
    221 20502 20502
    221 21681 21682
    221 21314     .
    221 21595 21595
    221 21605 21605
    221 20806 20806
    221 21322 21322
    221 20963 20964
    221 22340     .
    221 22112 22112
    221 22454 22454
    221 21720 21720
    221 20963 20964
    221 21733 21733
    221 22431 22431
    221 22420 22420
    221 21394 21394
    221 22409 22410
    221 22378 22378
    221 22394 22398
    221 20806 20806
    221 22327 22331
    221 22375 22375
    221 22424 22426
    221 21605 21605
    221 21384 21384
    221 21549     .
    221 21517 21517
    221 22283 22283
    221 20828     .
    221 22089 22089
    221 22454     .
    221 20982 20982
    221 21366 21366
    221 22214 22219
    221 21711 21712
    221 22475     .
    221 22473     .
    221 22438 22442
    221 22267 22267
    221 21117 21117
    221 21720 21720
    221 21322 21322
    221 21259 21259
    221 22133     .
    221 20744 20744
    221 21303 21307
    221 22447 22447
    221 22137 22138
    221 22092     .
    221 22155 22156
    221 20817 20820
    221 22368 22370
    221 22490 22491
    221 22209 22209
    221 22208 22208
    221 21019     .
    221 21311 21313
    221 21558 21558
    221 22404     .
    221 22098 22098
    221 21169 21171
    221 21717 21717
    221 22249 22249
    221 20549 20549
    221 22433 22433
    221 21977 21977
    221 21702 21704
    221 20590 20590
    221 22356 22357
    221 21023 21023
    221 20730 20730
    221 21606 21606
    221 21326     .
    221 20730 20730
    221 21384 21384
    355 21257 21257
    355 21503 21503
    355 21954 21954
    355 22194 22194
    355 21551 21551
    355 20506 20506
    355 22259 22259
    355 21096 21096
    355 21101 21101
    355 22358 22358
    355 20962 20962
    355 21290 21290
    355 20737 20737
    355 21165 21165
    355 21032 21032
    355 21347 21347
    355 21672 21672
    end
    format %td admit_date
    format %td disch_date
    Here is the dataex for the lab test:
    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input long id float(labdate abnormaltest)
     221 22112 0
     221 22209 0
     221 22249 0
     221 22250 0
     221 22267 0
     221 22268 0
     221 22327 0
     221 22368 0
     221 22424 0
     221 22425 0
     221 22501 0
     355 22271 1
     361 22122 0
     474 22004 .
     474 22004 1
     479 21990 .
     479 21990 1
     679 22176 0
     679 22183 0
     679 22263 1
     718 22012 0
     718 22043 0
     718 22046 0
     718 22061 0
     718 22082 0
     718 22182 .
     718 22182 0
     718 22199 0
     718 22199 .
     718 22229 0
     718 22229 .
     718 22239 0
     718 22239 .
     841 22275 0
     841 22417 0
     841 22433 0
     849 22410 0
     849 22411 0
     852 22069 0
     852 22071 0
     940 22040 0
     940 22140 0
    1055 22036 0
    1055 22036 1
    1055 22038 1
    1055 22070 0
    1055 22176 0
    1244 22495 0
    1302 22175 .
    1302 22175 0
    1383 22027 0
    1383 22259 0
    1383 22267 1
    1437 22130 0
    1437 22131 0
    1441 22020 0
    1441 22279 0
    1486 22181 0
    1525 22051 0
    1525 22329 .
    1525 22329 0
    1525 22334 0
    1642 22152 0
    1642 22161 0
    1642 22383 0
    1702 22053 0
    1702 22116 0
    1702 22117 0
    1702 22229 0
    1702 22288 0
    1702 22348 0
    1745 22329 0
    1778 22219 0
    1861 22052 0
    1861 22146 0
    1861 22237 1
    1861 22320 0
    2030 22032 0
    2057 22167 0
    2078 22147 0
    2137 22022 1
    2141 22292 1
    2141 22309 0
    2176 22130 0
    2193 22406 0
    2193 22412 0
    2201 22312 0
    2256 22284 0
    2256 22348 0
    2340 22218 1
    2341 22341 0
    2344 22418 0
    2422 22258 0
    2437 21998 0
    2437 22104 0
    2445 22242 0
    2445 22434 1
    2446 22022 0
    2490 22279 0
    2527 22000 .
    end
    format %td labdate
    ID does not uniquely identify patients in lab data, and labdate does not have to exactly match admit_date but instead simply needs to be in the date interval of 14 days prior to, to 1 week post admit_date. How may I merge best?
    Many thanks for any help you may be able to offer.
    Sincerely,
    Sumedha.

  • #2
    maybe joinby can be of use,
    Code:
    use hospitalization ,clear
    joinby id using labtest
    collapse abnormaltest if abnormaltest & inrange(labdate,admit-14,admit+7), by(id admit)
    merge 1:m id admit using hospitalization, nogen 
    replace abnormaltest = 0 if abnormaltest == .

    Comment


    • #3
      Why doesn't the patient ID uniquely ID patients? If I'm a patient and I have my own ID, I can also have your patient ID? You should likely start by making a unique ID, and then merging off that.

      Comment


      • #4
        Hi Jared Greathouse , I meant the same patient ID may have multiple lab tests and therefore rows in the data. So we cannot do a m:1 merge with the hospitalization file.

        Comment


        • #5
          https://www.statalist.org/forums/for...53#post1643453

          Thank you for your input. Perhaps I am missing something.. but should there instead be (my edit in red font):
          collapse abnormaltest if abnormaltest==1 & inrange(labdate,admit-14,admit+7), by(id admit) Many thanks again for your help.
          Sumedha.

          Comment


          • #6
            you are right, it should be abnormaltest==1

            Comment

            Working...
            X