Announcement

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

  • Stset data in long format

    Hello all,

    My dataset comprises of women who are being followed up and are tested for pregnancy every 3 months. Even after testing positive for pregnancy they still are followed up however on the next visit the participant's pregnancy test result is recorded as negative or is missing.

    I created variable preg_final where as long as a participant has a positive result for pregnancy recorded at any visit preg_final has it as pregnancy all visits. Wanted is the date of latest visit for those with negative pregnancy test and for those with a positive pregnancy test, its date when the test was found positive.

    I am currently trying to stset the data, however I get this output.
    [stset wanted , fail(preg_final==1) enter(firstdate) origin(firstdate) id(record_id) scale(365.25)]

    id: record_id
    failure event: preg_final == 1
    obs. time interval: (wanted[_n-1], wanted]
    enter on or after: time firstdate
    exit on or before: failure
    t for analysis: (time-origin)/365.25
    origin: time firstdate

    ------------------------------------------------------------------------------
    1,826 total observations
    1,826 multiple records at same instant PROBABLE ERROR
    (wanted[_n-1]==wanted)
    ------------------------------------------------------------------------------
    0 observations remaining, representing
    0 subjects
    0 failures in single-failure-per-subject data
    0 total analysis time at risk and under observation
    at risk from t = 0
    earliest observed entry t = .
    last observed exit t = .

    .
    What could I possibly be doing wrong?

    Please see below a sample of my data.

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input int record_id str23 redcap_event_name float(wanted firstdate) byte tpregtest_result float preg_final
     1 "m3_arm_1"        23656 23229 2 0
     1 "m9_arm_1"        23656 23229 2 0
     1 "m15_arm_1"       23656 23229 2 0
     1 "m12_arm_1"       23656 23229 2 0
     1 "m6_arm_1"        23656 23229 2 0
     1 "enrolment_arm_1" 23656 23229 . 0
    12 "m15_arm_1"       23656 23236 2 0
    12 "m12_arm_1"       23656 23236 2 0
    12 "m6_arm_1"        23656 23236 2 0
    12 "m3_arm_1"        23656 23236 2 0
    12 "m9_arm_1"        23656 23236 2 0
    12 "enrolment_arm_1" 23656 23236 . 0
    13 "m3_arm_1"        23660 23237 2 0
    13 "m15_arm_1"       23660 23237 2 0
    13 "m12_arm_1"       23660 23237 2 0
    13 "m6_arm_1"        23660 23237 2 0
    13 "m9_arm_1"        23660 23237 2 0
    13 "enrolment_arm_1" 23660 23237 . 0
    14 "m12_arm_1"       23568 23237 2 0
    14 "m9_arm_1"        23568 23237 2 0
    14 "m3_arm_1"        23568 23237 2 0
    14 "enrolment_arm_1" 23568 23237 2 0
    14 "m6_arm_1"        23568 23237 2 0
    15 "m9_arm_1"        23657 23237 2 0
    15 "m3_arm_1"        23657 23237 2 0
    15 "m15_arm_1"       23657 23237 2 0
    15 "m6_arm_1"        23657 23237 2 0
    15 "m12_arm_1"       23657 23237 2 0
    15 "enrolment_arm_1" 23657 23237 . 0
    16 "m12_arm_1"       23657 23237 2 0
    16 "m3_arm_1"        23657 23237 2 0
    16 "m15_arm_1"       23657 23237 2 0
    16 "m9_arm_1"        23657 23237 2 0
    16 "enrolment_arm_1" 23657 23237 . 0
    16 "m6_arm_1"        23657 23237 . 0
    17 "m12_arm_1"       23625 23238 1 1
    17 "m15_arm_1"       23625 23238 2 1
    17 "m6_arm_1"        23625 23238 . 1
    17 "enrolment_arm_1" 23625 23238 . 1
    17 "m3_arm_1"        23625 23238 . 1
    18 "m12_arm_1"       23572 23238 1 1
    18 "m9_arm_1"        23572 23238 2 1
    18 "m6_arm_1"        23572 23238 2 1
    18 "m3_arm_1"        23572 23238 2 1
    18 "m15_arm_1"       23572 23238 . 1
    18 "enrolment_arm_1" 23572 23238 . 1
    19 "m12_arm_1"       23659 23238 2 0
    19 "m3_arm_1"        23659 23238 2 0
    19 "m6_arm_1"        23659 23238 2 0
    19 "m15_arm_1"       23659 23238 2 0
    19 "m9_arm_1"        23659 23238 2 0
    19 "enrolment_arm_1" 23659 23238 . 0
    20 "m12_arm_1"       23659 23238 2 0
    20 "m3_arm_1"        23659 23238 2 0
    20 "m6_arm_1"        23659 23238 2 0
    20 "m9_arm_1"        23659 23238 2 0
    20 "m15_arm_1"       23659 23238 2 0
    20 "enrolment_arm_1" 23659 23238 . 0
    21 "m12_arm_1"       23659 23239 2 0
    21 "m9_arm_1"        23659 23239 2 0
    21 "m15_arm_1"       23659 23239 2 0
    21 "m3_arm_1"        23659 23239 2 0
    21 "m6_arm_1"        23659 23239 2 0
    21 "enrolment_arm_1" 23659 23239 . 0
    24 "m6_arm_1"        23657 23240 2 0
    24 "m12_arm_1"       23657 23240 2 0
    24 "m9_arm_1"        23657 23240 2 0
    24 "m15_arm_1"       23657 23240 2 0
    24 "m3_arm_1"        23657 23240 2 0
    24 "enrolment_arm_1" 23657 23240 . 0
    end
    format %td wanted
    format %td firstdate
    label values tpregtest_result tpregtest_result_
    label def tpregtest_result_ 1 "Positive", modify
    label def tpregtest_result_ 2 "Negative", modify
    label values preg_final preg_final_
    label def preg_final_ 0 "No", modify
    label def preg_final_ 1 "Yes", modify

    Thanks,

    Galenda

  • #2
    Originally posted by Galenda Nagudi View Post
    Wanted is the date of latest visit for those with negative pregnancy test and for those with a positive pregnancy test, its date when the test was found positive. . . . What could I possibly be doing wrong?
    Look at the message in the output for a clue. You're trying to stset with a time variable that doesn't vary: for each woman all of the records are at the same date. You need to stset with a time variable that varies.

    Comment


    • #3
      Thank you Joseph for pointing this out. I guess its important to visit the dataset with fresh eyes as I was completely oblivious to my error.

      I have a follow up question. the data has duplicates when it comes to those with a positive pregnancy test and I realized it with the error after running the stset command. Any idea on how I can create a variable with only one positive entry for pregnancy positive per participant?

      [stset tpregtest_date, fail(preg_res==1) enter(firstdate) exit(wanted) id(record_id) scale(365.25)]

      id: record_id
      failure event: preg_res == 1
      obs. time interval: (tpregtest_date[_n-1], tpregtest_date]
      enter on or after: time firstdate
      exit on or before: time wanted
      t for analysis: time/365.25

      ------------------------------------------------------------------------------
      1,826 total observations
      440 event time missing (tpregtest_date>=.) PROBABLE ERROR
      6 multiple records at same instant PROBABLE ERROR
      (tpregtest_date[_n-1]==tpregtest_date)
      2 observations end on or before enter()
      21 observations begin on or after exit
      ------------------------------------------------------------------------------
      1,357 observations remaining, representing
      381 subjects
      28 failures in single-failure-per-subject data
      328.301 total analysis time at risk and under observation
      at risk from t = 0
      earliest observed entry t = 63.59754
      last observed exit t = 64.8898


      Please see sample of data below. I have only included those who were reported pregnant

      Code:
      * Example generated by -dataex-. To install: ssc install dataex
      clear
      input int record_id str23 redcap_event_name float tpregtest_date byte tpregtest_result float(preg_res wanted firstdate)
       17 "m12_arm_1" 23625 1 1 23625 23238
       18 "m12_arm_1" 23572 1 1 23572 23238
      105 "m15_arm_1" 23692 1 1 23692 23268
      153 "m6_arm_1"  23455 1 1 23455 23289
      176 "m12_arm_1" 23625 1 1 23625 23294
      186 "m3_arm_1"  23384 1 1 23384 23299
      186 "m6_arm_1"  23469 1 1 23384 23299
      186 "m9_arm_1"  23547 1 1 23384 23299
      197 "m12_arm_1" 23631 1 1 23631 23302
      199 "m6_arm_1"  23474 1 1 23474 23302
      210 "m9_arm_1"  23560 1 1 23560 23308
      211 "m6_arm_1"  23479 1 1 23479 23308
      211 "m9_arm_1"  23555 1 1 23479 23308
      216 "m6_arm_1"  23477 1 1 23477 23309
      216 "m9_arm_1"  23561 1 1 23477 23309
      228 "m12_arm_1" 23656 1 1 23656 23315
      237 "m3_arm_1"  23408 1 1 23408 23321
      237 "m6_arm_1"  23484 1 1 23408 23321
      237 "m9_arm_1"  23573 1 1 23408 23321
      238 "m9_arm_1"  23573 1 1 23573 23321
      265 "m6_arm_1"  23519 1 1 23519 23336
      317 "m3_arm_1"  23460 1 1 23460 23364
      317 "m6_arm_1"  23537 1 1 23460 23364
      317 "m9_arm_1"  23611 1 1 23460 23364
      319 "m9_arm_1"  23611 1 1 23611 23365
      333 "m9_arm_1"  23628 1 1 23628 23383
      337 "m9_arm_1"  23629 1 1 23629 23383
      340 "m9_arm_1"  23630 1 1 23630 23384
      341 "m3_arm_1"  23469 1 1 23469 23384
      359 "m6_arm_1"  23562 1 1 23562 23392
      385 "m9_arm_1"  23652 1 1 23652 23401
      391 "m6_arm_1"  23572 1 1 23572 23407
      401 "m9_arm_1"  23660 1 1 23660 23409
      409 "m3_arm_1"  23488 1 1 23488 23409
      458 "m6_arm_1"  23657 1 1 23657 23434
      461 "m9_arm_1"  23688 1 1 23688 23434
      end
      format %dM_d,_CY tpregtest_date
      format %td wanted
      format %td firstdate
      label values tpregtest_result tpregtest_result_
      label def tpregtest_result_ 1 "Positive", modify


      Comment


      • #4
        Well, I'm not sure I agree with #2. In your example data, the variable you are -stset-ing as the time variable, tpregtest_date does vary. In fact, tpregtest and record_id uniquely identify observations: there are no duplicates at all! And when I run your -stset- command with your example data, it does not produce any error message at all. So something else may be going on.

        Please post back with a data example that does reproduce the error messages you received so troubleshooting will be possible.

        Comment


        • #5
          Thank you for the response Clyde. Please see sample data below. The duplicates I referred to was for example record id 211 has positive entry for pregnancy at month 6 (m6_arm_1) and month 9 (m9_arm_1). However, I have noticed when I run the stset command the failures(pregnancy) have no duplicates.

          Code:
          * Example generated by -dataex-. To install: ssc install dataex
          clear
          input int record_id str23 redcap_event_name float(ddate_of_visit tpregtest_date wanted firstdate) byte tpregtest_result float preg_res
            1 "m3_arm_1"        23314 23314 23656 23229 2 2
            1 "m9_arm_1"        23481 23481 23656 23229 2 2
            1 "m15_arm_1"       23656 23656 23656 23229 2 2
            1 "m12_arm_1"       23568 23568 23656 23229 2 2
            1 "m6_arm_1"        23401 23401 23656 23229 2 2
            1 "enrolment_arm_1" 23229     . 23656 23229 . 2
            2 "m12_arm_1"       23572 23572 23652 23232 2 2
            2 "m9_arm_1"        23482 23482 23652 23232 2 2
            2 "m3_arm_1"        23316 23316 23652 23232 2 2
            2 "m6_arm_1"        23447 23447 23652 23232 2 2
            2 "m15_arm_1"       23652 23652 23652 23232 2 2
            2 "enrolment_arm_1" 23232     . 23652 23232 . 2
            4 "m6_arm_1"        23407 23407 23683 23233 2 2
            4 "m3_arm_1"        23317 23683 23683 23233 2 2
            4 "m12_arm_1"       23567 23567 23683 23233 2 2
            4 "m15_arm_1"       23652 23652 23683 23233 2 2
            4 "m9_arm_1"        23482 23482 23683 23233 2 2
            4 "enrolment_arm_1" 23233     . 23683 23233 . 2
            5 "m12_arm_1"       23568 23568 23656 23236 2 2
            5 "m6_arm_1"        23405 23405 23656 23236 2 2
            5 "m3_arm_1"        23320 23320 23656 23236 2 2
            5 "m9_arm_1"        23482 23482 23656 23236 2 2
            5 "m15_arm_1"       23656 23656 23656 23236 2 2
            5 "enrolment_arm_1" 23236     . 23656 23236 . 2
            6 "m15_arm_1"       23656 23656 23656 23232 2 2
            6 "m6_arm_1"        23405 23405 23656 23232 2 2
            6 "m9_arm_1"        23484 23484 23656 23232 2 2
            6 "m3_arm_1"        23316 23316 23656 23232 2 2
            6 "m12_arm_1"       23567 23567 23656 23232 2 2
            6 "enrolment_arm_1" 23232     . 23656 23232 . 2
            7 "m15_arm_1"       23666 23666 23666 23240 2 2
            7 "enrolment_arm_1" 23240 23240 23666 23240 2 2
            7 "m3_arm_1"        23323 23323 23666 23240 2 2
            7 "m9_arm_1"        23490 23490 23666 23240 2 2
            7 "m12_arm_1"       23573 23573 23666 23240 2 2
            7 "m6_arm_1"        23539     . 23666 23240 . 2
           12 "m15_arm_1"       23656 23656 23656 23236 2 2
           12 "m12_arm_1"       23573 23573 23656 23236 2 2
           12 "m6_arm_1"        23408 23408 23656 23236 2 2
           12 "m3_arm_1"        23324 23324 23656 23236 2 2
           12 "m9_arm_1"        23484 23484 23656 23236 2 2
           12 "enrolment_arm_1" 23236     . 23656 23236 . 2
           13 "m3_arm_1"        23321 23321 23660 23237 2 2
           13 "m15_arm_1"       23660 23660 23660 23237 2 2
           13 "m12_arm_1"       23569 23569 23660 23237 2 2
           13 "m6_arm_1"        23405 23405 23660 23237 2 2
           13 "m9_arm_1"        23485 23454 23660 23237 2 2
           13 "enrolment_arm_1" 23237     . 23660 23237 . 2
           14 "m12_arm_1"       23568 23568 23568 23237 2 2
           14 "m9_arm_1"        23485 23485 23568 23237 2 2
           14 "m3_arm_1"        23321 23321 23568 23237 2 2
           14 "enrolment_arm_1" 23237 23321 23568 23237 2 2
           14 "m6_arm_1"        23405 23405 23568 23237 2 2
           15 "m9_arm_1"        23484 23484 23657 23237 2 2
           15 "m3_arm_1"        23321 23321 23657 23237 2 2
           15 "m15_arm_1"       23657 23657 23657 23237 2 2
           15 "m6_arm_1"        23405 23405 23657 23237 2 2
           15 "m12_arm_1"       23569 23569 23657 23237 2 2
           15 "enrolment_arm_1" 23237     . 23657 23237 . 2
           16 "m12_arm_1"       23569 23569 23657 23237 2 2
           16 "m3_arm_1"        23321 23350 23657 23237 2 2
           16 "m15_arm_1"       23657 23657 23657 23237 2 2
           16 "m9_arm_1"        23484 23484 23657 23237 2 2
           16 "enrolment_arm_1" 23237     . 23657 23237 . 2
           16 "m6_arm_1"        23539     . 23657 23237 . 2
           17 "m12_arm_1"       23625 23625 23625 23238 1 1
           17 "m15_arm_1"       23659 23659 23625 23238 2 2
           17 "m6_arm_1"        23539     . 23625 23238 . 2
           17 "enrolment_arm_1" 23238     . 23625 23238 . 2
           17 "m3_arm_1"        23539     . 23625 23238 . 2
           18 "m12_arm_1"       23572 23572 23572 23238 1 1
           18 "m9_arm_1"        23483 23483 23572 23238 2 2
           18 "m6_arm_1"        23406 23406 23572 23238 2 2
           18 "m3_arm_1"        23322 23322 23572 23238 2 2
           18 "m15_arm_1"       23659     . 23572 23238 . 2
           18 "enrolment_arm_1" 23238     . 23572 23238 . 2
           19 "m12_arm_1"       23572 23572 23659 23238 2 2
           19 "m3_arm_1"        23322 23322 23659 23238 2 2
           19 "m6_arm_1"        23406 23406 23659 23238 2 2
           19 "m15_arm_1"       23659 23659 23659 23238 2 2
           19 "m9_arm_1"        23483 23483 23659 23238 2 2
           19 "enrolment_arm_1" 23238     . 23659 23238 . 2
           20 "m12_arm_1"       23572 23572 23659 23238 2 2
           20 "m3_arm_1"        23322 23322 23659 23238 2 2
           20 "m6_arm_1"        23406 23406 23659 23238 2 2
           20 "m9_arm_1"        23483 23483 23659 23238 2 2
           20 "m15_arm_1"       23659 23659 23659 23238 2 2
           20 "enrolment_arm_1" 23238     . 23659 23238 . 2
           21 "m12_arm_1"       23576 23576 23659 23239 2 2
           21 "m9_arm_1"        23485 23485 23659 23239 2 2
           21 "m15_arm_1"       23659 23659 23659 23239 2 2
           21 "m3_arm_1"        23324 23324 23659 23239 2 2
           21 "m6_arm_1"        23411 23411 23659 23239 2 2
           21 "enrolment_arm_1" 23239     . 23659 23239 . 2
          141 "m9_arm_1"        23540 23540 23630 23287 2 2
          141 "m12_arm_1"       23630 23630 23630 23287 2 2
          141 "m3_arm_1"        23364 23364 23630 23287 2 2
          141 "enrolment_arm_1" 23287     . 23630 23287 . 2
          141 "m15_arm_1"       23701     . 23630 23287 . 2
          142 "m12_arm_1"       23622 23622 23700 23287 2 2
          142 "m3_arm_1"        23364 23364 23700 23287 2 2
          142 "m9_arm_1"        23540 23540 23700 23287 2 2
          142 "m15_arm_1"       23700 23700 23700 23287 2 2
          142 "m6_arm_1"        23455 23455 23700 23287 2 2
          142 "enrolment_arm_1" 23287     . 23700 23287 . 2
          143 "m3_arm_1"        23364 23364 23622 23287 2 2
          143 "m9_arm_1"        23540 23540 23622 23287 2 2
          143 "m6_arm_1"        23455 23455 23622 23287 2 2
          143 "m12_arm_1"       23622 23622 23622 23287 2 2
          143 "enrolment_arm_1" 23287     . 23622 23287 . 2
          144 "m15_arm_1"       23700 23700 23700 23287 2 2
          144 "m9_arm_1"        23538 23538 23700 23287 2 2
          144 "m12_arm_1"       23625 23625 23700 23287 2 2
          144 "m3_arm_1"        23364 23364 23700 23287 2 2
          144 "m6_arm_1"        23454 23454 23700 23287 2 2
          144 "enrolment_arm_1" 23287     . 23700 23287 . 2
          146 "m9_arm_1"        23547 23547 23623 23288 2 2
          146 "m6_arm_1"        23454 23454 23623 23288 2 2
          146 "m3_arm_1"        23365 23365 23623 23288 2 2
          146 "m12_arm_1"       23623 23623 23623 23288 2 2
          146 "enrolment_arm_1" 23288     . 23623 23288 . 2
          147 "m6_arm_1"        23461 23461 23701 23288 2 2
          147 "m12_arm_1"       23622 23622 23701 23288 2 2
          147 "m3_arm_1"        23365 23365 23701 23288 2 2
          147 "m9_arm_1"        23545 23545 23701 23288 2 2
          147 "m15_arm_1"       23701 23701 23701 23288 2 2
          147 "enrolment_arm_1" 23288     . 23701 23288 . 2
          148 "m3_arm_1"        23365 23365 23701 23288 2 2
          148 "m9_arm_1"        23540 23540 23701 23288 2 2
          148 "m6_arm_1"        23461 23461 23701 23288 2 2
          148 "m12_arm_1"       23624 23624 23701 23288 2 2
          148 "m15_arm_1"       23701 23701 23701 23288 2 2
          148 "enrolment_arm_1" 23288     . 23701 23288 . 2
          149 "m9_arm_1"        23534 23534 23624 23288 2 2
          149 "m6_arm_1"        23461 23461 23624 23288 2 2
          149 "m12_arm_1"       23624 23624 23624 23288 2 2
          149 "enrolment_arm_1" 23288     . 23624 23288 . 2
          149 "m3_arm_1"        23380     . 23624 23288 . 2
          150 "m6_arm_1"        23455 23455 23701 23288 2 2
          150 "m15_arm_1"       23701 23701 23701 23288 2 2
          150 "m12_arm_1"       23624 23624 23701 23288 2 2
          150 "m9_arm_1"        23546 23546 23701 23288 2 2
          150 "m3_arm_1"        23380     . 23701 23288 . 2
          150 "enrolment_arm_1" 23288     . 23701 23288 . 2
          151 "m6_arm_1"        23455 23455 23624 23288 2 2
          151 "m9_arm_1"        23533 23533 23624 23288 2 2
          151 "m12_arm_1"       23624 23624 23624 23288 2 2
          151 "enrolment_arm_1" 23288     . 23624 23288 . 2
          151 "m15_arm_1"       23701     . 23624 23288 . 2
          151 "m3_arm_1"        23379     . 23624 23288 . 2
          152 "m9_arm_1"        23538 23538 23622 23289 2 2
          152 "m6_arm_1"        23454 23454 23622 23289 2 2
          152 "m12_arm_1"       23622 23622 23622 23289 2 2
          152 "m3_arm_1"        23377 23377 23622 23289 2 2
          152 "enrolment_arm_1" 23289     . 23622 23289 . 2
          153 "m6_arm_1"        23455 23455 23455 23289 1 1
          153 "m9_arm_1"        23538 23538 23455 23289 2 2
          153 "m12_arm_1"       23622 23622 23455 23289 2 2
          153 "m3_arm_1"        23377 23377 23455 23289 2 2
          153 "enrolment_arm_1" 23289     . 23455 23289 . 2
          154 "m9_arm_1"        23538 23538 23622 23289 2 2
          154 "m6_arm_1"        23455 23455 23622 23289 2 2
          154 "m12_arm_1"       23622 23622 23622 23289 2 2
          154 "m3_arm_1"        23377 23377 23622 23289 2 2
          154 "enrolment_arm_1" 23289     . 23622 23289 . 2
          156 "m6_arm_1"        23461 23461 23622 23292 2 2
          156 "m9_arm_1"        23541 23541 23622 23292 2 2
          156 "m12_arm_1"       23622 23622 23622 23292 2 2
          156 "m3_arm_1"        23384     . 23622 23292 . 2
          156 "enrolment_arm_1" 23292     . 23622 23292 . 2
          157 "m9_arm_1"        23551 23551 23622 23292 2 2
          157 "m3_arm_1"        23383 23383 23622 23292 2 2
          157 "m6_arm_1"        23463 23463 23622 23292 2 2
          157 "m12_arm_1"       23622 23622 23622 23292 2 2
          157 "enrolment_arm_1" 23292     . 23622 23292 . 2
          158 "m9_arm_1"        23558 23558 23635 23301 2 2
          158 "m12_arm_1"       23635 23635 23635 23301 2 2
          158 "m6_arm_1"        23463 23463 23635 23301 2 2
          158 "m3_arm_1"        23383 23383 23635 23301 2 2
          158 "enrolment_arm_1" 23301     . 23635 23301 . 2
          159 "m9_arm_1"        23558 23558 23630 23301 2 2
          159 "m3_arm_1"        23385 23385 23630 23301 2 2
          159 "m6_arm_1"        23463 23463 23630 23301 2 2
          159 "m12_arm_1"       23630 23630 23630 23301 2 2
          159 "enrolment_arm_1" 23301     . 23630 23301 . 2
          160 "m3_arm_1"        23379 23379 23622 23292 2 2
          160 "m12_arm_1"       23622 23622 23622 23292 2 2
          160 "m9_arm_1"        23538 23538 23622 23292 2 2
          160 "m6_arm_1"        23460 23460 23622 23292 2 2
          160 "enrolment_arm_1" 23292     . 23622 23292 . 2
          161 "m3_arm_1"        23378 23378 23630 23292 2 2
          161 "m6_arm_1"        23460 23460 23630 23292 2 2
          161 "m9_arm_1"        23538 23538 23630 23292 2 2
          161 "m12_arm_1"       23630 23630 23630 23292 2 2
          161 "enrolment_arm_1" 23292     . 23630 23292 . 2
          163 "m6_arm_1"        23468 23468 23630 23301 2 2
          163 "m12_arm_1"       23630 23630 23630 23301 2 2
          163 "m3_arm_1"        23378 23378 23630 23301 2 2
          163 "m9_arm_1"        23558 23558 23630 23301 2 2
          163 "enrolment_arm_1" 23301     . 23630 23301 . 2
          164 "m12_arm_1"       23623 23623 23623 23292 2 2
          164 "m6_arm_1"        23460 23460 23623 23292 2 2
          164 "m3_arm_1"        23378 23378 23623 23292 2 2
          164 "m9_arm_1"        23541 23541 23623 23292 2 2
          164 "enrolment_arm_1" 23292     . 23623 23292 . 2
          165 "m9_arm_1"        23537 23537 23623 23292 2 2
          165 "m6_arm_1"        23460 23460 23623 23292 2 2
          165 "m3_arm_1"        23378 23378 23623 23292 2 2
          165 "m12_arm_1"       23623 23623 23623 23292 2 2
          165 "enrolment_arm_1" 23292     . 23623 23292 . 2
          166 "m9_arm_1"        23537 23537 23622 23292 2 2
          166 "m6_arm_1"        23460 23460 23622 23292 2 2
          166 "m3_arm_1"        23383 23383 23622 23292 2 2
          166 "m12_arm_1"       23622 23622 23622 23292 2 2
          166 "enrolment_arm_1" 23292     . 23622 23292 . 2
          169 "m3_arm_1"        23379 23379 23541 23294 2 2
          169 "m6_arm_1"        23462 23462 23541 23294 2 2
          169 "m9_arm_1"        23541 23541 23541 23294 2 2
          169 "enrolment_arm_1" 23294     . 23541 23294 . 2
          170 "m12_arm_1"       23623 23623 23623 23294 2 2
          170 "m9_arm_1"        23546 23546 23623 23294 2 2
          170 "m6_arm_1"        23462 23462 23623 23294 2 2
          170 "m3_arm_1"        23386     . 23623 23294 . 2
          170 "enrolment_arm_1" 23294     . 23623 23294 . 2
          172 "m3_arm_1"        23379 23379 23623 23294 2 2
          172 "m12_arm_1"       23623 23623 23623 23294 2 2
          172 "m9_arm_1"        23539 23539 23623 23294 2 2
          172 "m6_arm_1"        23462 23462 23623 23294 2 2
          172 "enrolment_arm_1" 23294     . 23623 23294 . 2
          173 "m6_arm_1"        23462 23462 23623 23294 2 2
          173 "m9_arm_1"        23545 23545 23623 23294 2 2
          173 "m3_arm_1"        23380 23380 23623 23294 2 2
          173 "m12_arm_1"       23623 23623 23623 23294 2 2
          173 "enrolment_arm_1" 23294     . 23623 23294 . 2
          176 "m12_arm_1"       23625 23625 23625 23294 1 1
          176 "m3_arm_1"        23379 23379 23625 23294 2 2
          176 "m6_arm_1"        23462 23462 23625 23294 2 2
          176 "m9_arm_1"        23539 23539 23625 23294 2 2
          176 "enrolment_arm_1" 23294     . 23625 23294 . 2
          177 "m6_arm_1"        23462 23462 23623 23294 2 2
          177 "m9_arm_1"        23541 23541 23623 23294 2 2
          177 "m12_arm_1"       23623 23623 23623 23294 2 2
          177 "m3_arm_1"        23379 23379 23623 23294 2 2
          177 "enrolment_arm_1" 23294     . 23623 23294 . 2
          178 "m3_arm_1"        23380 23380 23539 23294 2 2
          178 "m6_arm_1"        23462 23462 23539 23294 2 2
          178 "m9_arm_1"        23539 23539 23539 23294 2 2
          178 "enrolment_arm_1" 23294     . 23539 23294 . 2
          179 "m6_arm_1"        23462 23462 23624 23295 2 2
          179 "m12_arm_1"       23624 23624 23624 23295 2 2
          179 "m9_arm_1"        23541 23541 23624 23295 2 2
          179 "m3_arm_1"        23380 23380 23624 23295 2 2
          179 "enrolment_arm_1" 23295     . 23624 23295 . 2
          180 "m6_arm_1"        23468 23468 23624 23295 2 2
          180 "m9_arm_1"        23541 23541 23624 23295 2 2
          180 "m3_arm_1"        23384 23384 23624 23295 2 2
          180 "m12_arm_1"       23624 23624 23624 23295 2 2
          180 "enrolment_arm_1" 23295     . 23624 23295 . 2
          181 "m6_arm_1"        23462 23462 23624 23295 2 2
          181 "m3_arm_1"        23380 23380 23624 23295 2 2
          181 "m12_arm_1"       23624 23624 23624 23295 2 2
          181 "m9_arm_1"        23545 23545 23624 23295 2 2
          181 "enrolment_arm_1" 23295     . 23624 23295 . 2
          182 "m12_arm_1"       23624 23624 23624 23295 2 2
          182 "m6_arm_1"        23462 23462 23624 23295 2 2
          182 "m3_arm_1"        23380 23380 23624 23295 2 2
          182 "m9_arm_1"        23547 23547 23624 23295 2 2
          182 "enrolment_arm_1" 23295     . 23624 23295 . 2
          183 "m9_arm_1"        23553 23553 23628 23295 2 2
          183 "m3_arm_1"        23380 23380 23628 23295 2 2
          183 "m12_arm_1"       23628 23628 23628 23295 2 2
          183 "m6_arm_1"        23462 23462 23628 23295 2 2
          183 "enrolment_arm_1" 23295     . 23628 23295 . 2
          184 "m6_arm_1"        23475 23475 23547 23299 2 2
          184 "m9_arm_1"        23547 23547 23547 23299 2 2
          184 "m3_arm_1"        23475     . 23547 23299 . 2
          184 "enrolment_arm_1" 23299     . 23547 23299 . 2
          186 "m6_arm_1"        23469 23469 23384 23299 1 1
          186 "m3_arm_1"        23384 23384 23384 23299 1 1
          186 "m9_arm_1"        23547 23547 23384 23299 1 1
          186 "m12_arm_1"       23628 23628 23384 23299 2 2
          186 "enrolment_arm_1" 23299     . 23384 23299 . 2
          187 "m12_arm_1"       23628 23628 23628 23299 2 2
          187 "m9_arm_1"        23547 23547 23628 23299 2 2
          187 "m6_arm_1"        23471 23471 23628 23299 2 2
          187 "m3_arm_1"        23471     . 23628 23299 . 2
          187 "enrolment_arm_1" 23299     . 23628 23299 . 2
          188 "m12_arm_1"       23629 23629 23629 23300 2 2
          188 "m3_arm_1"        23384 23384 23629 23300 2 2
          188 "m9_arm_1"        23553 23553 23629 23300 2 2
          188 "m6_arm_1"        23468 23468 23629 23300 2 2
          188 "enrolment_arm_1" 23300     . 23629 23300 . 2
          211 "m9_arm_1"        23555 23555 23479 23308 1 1
          211 "m6_arm_1"        23479 23479 23479 23308 1 1
          211 "m3_arm_1"        23392 23392 23479 23308 2 2
          211 "m12_arm_1"       23638     . 23479 23308 . 2
          211 "enrolment_arm_1" 23308     . 23479 23308 . 2
          212 "m9_arm_1"        23567 23567 23638 23308 2 2
          212 "m6_arm_1"        23479 23479 23638 23308 2 2
          212 "m12_arm_1"       23638 23638 23638 23308 2 2
          212 "m3_arm_1"        23385 23385 23638 23308 2 2
          212 "enrolment_arm_1" 23308     . 23638 23308 . 2
          end
          format %dM_d,_CY ddate_of_visit
          format %dM_d,_CY tpregtest_date
          format %td wanted
          format %td firstdate
          label values tpregtest_result tpregtest_result_
          label def tpregtest_result_ 1 "Positive", modify
          label def tpregtest_result_ 2 "Negative", modify

          Whe I run this command [stset tpregtest_date, fail(preg_res==1) enter(firstdate) exit(wanted) id(record_id) scale(365.25)]

          Below is the stata output I get


          id: record_id
          failure event: preg_res == 1
          obs. time interval: (tpregtest_date[_n-1], tpregtest_date]
          enter on or after: time firstdate
          exit on or before: time wanted
          t for analysis: time/365.25

          ------------------------------------------------------------------------------
          1,826 total observations
          440 event time missing (tpregtest_date>=.) PROBABLE ERROR
          6 multiple records at same instant PROBABLE ERROR
          (tpregtest_date[_n-1]==tpregtest_date)
          2 observations end on or before enter()
          21 observations begin on or after exit
          ------------------------------------------------------------------------------
          1,357 observations remaining, representing
          381 subjects
          28 failures in single-failure-per-subject data
          328.301 total analysis time at risk and under observation
          at risk from t = 0
          earliest observed entry t = 63.59754
          last observed exit t = 64.8898


          The other concern of mine is after I run the stset comment I realise for those that had a negative pregnancy test have multiple entries for variables _st _d _t _t0 . My assumption would be that just like for those positive for pregnancy that have single entries of _st _d _t _t0 per record id.

          Code:
          * Example generated by -dataex-. To install: ssc install dataex
          clear
          input int record_id str23 redcap_event_name float(ddate_of_visit tpregtest_date wanted firstdate) byte tpregtest_result float preg_res byte(_st _d) double(_t _t0)
           1 "m3_arm_1"        23314 23314 23656 23229 2 2 1 0  63.83025325119781  63.59753593429158
           1 "m9_arm_1"        23481 23481 23656 23229 2 2 1 0  64.28747433264887   64.0684462696783
           1 "m15_arm_1"       23656 23656 23656 23229 2 2 1 0  64.76659822039699  64.52566735112936
           1 "m12_arm_1"       23568 23568 23656 23229 2 2 1 0  64.52566735112936  64.28747433264887
           1 "m6_arm_1"        23401 23401 23656 23229 2 2 1 0   64.0684462696783  63.83025325119781
           1 "enrolment_arm_1" 23229     . 23656 23229 . 2 0 .                  .                  .
          13 "m3_arm_1"        23321 23321 23660 23237 2 2 1 0  63.84941820670773 63.619438740588635
          13 "m15_arm_1"       23660 23660 23660 23237 2 2 1 0  64.77754962354551   64.5284052019165
          13 "m12_arm_1"       23569 23569 23660 23237 2 2 1 0   64.5284052019165  64.21355236139631
          13 "m6_arm_1"        23405 23405 23660 23237 2 2 1 0  64.07939767282683  63.84941820670773
          13 "m9_arm_1"        23485 23454 23660 23237 2 2 1 0  64.21355236139631  64.07939767282683
          13 "enrolment_arm_1" 23237     . 23660 23237 . 2 0 .                  .                  .
          14 "m12_arm_1"       23568 23568 23568 23237 2 2 1 0  64.52566735112936   64.2984257357974
          14 "m9_arm_1"        23485 23485 23568 23237 2 2 1 0   64.2984257357974  64.07939767282683
          14 "m3_arm_1"        23321 23321 23568 23237 2 2 0 .                  .                  .
          14 "enrolment_arm_1" 23237 23321 23568 23237 2 2 0 .                  .                  .
          14 "m6_arm_1"        23405 23405 23568 23237 2 2 1 0  64.07939767282683  63.84941820670773
          17 "m12_arm_1"       23625 23625 23625 23238 1 1 1 1  64.68172484599589  63.62217659137577
          17 "m15_arm_1"       23659 23659 23625 23238 2 2 0 .                  .                  .
          17 "m6_arm_1"        23539     . 23625 23238 . 2 0 .                  .                  .
          17 "enrolment_arm_1" 23238     . 23625 23238 . 2 0 .                  .                  .
          17 "m3_arm_1"        23539     . 23625 23238 . 2 0 .                  .                  .
          end
          format %dM_d,_CY ddate_of_visit
          format %dM_d,_CY tpregtest_date
          format %td wanted
          format %td firstdate
          label values tpregtest_result tpregtest_result_
          label def tpregtest_result_ 1 "Positive", modify
          label def tpregtest_result_ 2 "Negative", modify
          Thanks,

          Galenda

          Comment


          • #6
            Your -stset- command seems to be working as it should. The multiple entries for women with negative pregnancy tests are exactly what one would see given that you had multiple entries for them in the starting data. -stset- does not add or remove observations to the data: it just adds new variables that are used by survival analysis commands.

            That said, based on the example data you have sent, it seems there is no reason to have multiple entries for these women. All you really need is a single observation per woman containing her record_id, the variable named wanted, and an indicator for whether she eventually had a positive pregnancy test. You have no other variables that are relevant to the -stset-, and unless you plan on also using variables in your analyses that do change over time, the extra observations will not cause the analyses to go awry, but they will slow it down. So if you are not going to be using variables about these women that do vary over time, I would suggest:

            Code:
            by record_id (ddate_of_visit), sort: egen pos_preg_test ///
                = max(tpregtest_result == "Positive":tpregtest_result)
            by record_id: keep if _n == 1
            keep record_id wanted pos_preg_test // AND ANY OTHER NON-TIME-VARYING VARIABLES
            
            stset wanted, failure(pos_preg_test) scale(365.25)
            Again, this approach is only possible if you are not going to include in your survival analyses any variables that do change over time. If you are, then you have to stick with multiple entries per woman so that you can retain the information in those other variables. But if you aren't, then this more compact data set will allow your analyses to run a bit faster.

            Comment


            • #7
              Thank you Clyde for the suggestions. I do need the multiple entries for the women as some questions are asked in the follow up months that I will be needing for the analysis. Take for example they are asked how many new partners they have had in the last 3 months at month 3, 6, 9 etc.

              I still went ahead to run this command [by record_id (ddate_of_visit), sort: egen pos_preg_test ///
              = max(tpregtest_result == "Positive" :tpregtest_result)]

              However, I get this message after it runs "(value label dereference "Positive":tpregtest_result not found)" and the output for pos_preg_test is not right as seen in the stset command output below.

              [stset wanted, failure(pos_preg_test) scale(365.25)]

              failure event: pos_preg_test != 0 & pos_preg_test < .
              obs. time interval: (0, wanted]
              exit on or before: failure
              t for analysis: time/365.25

              ------------------------------------------------------------------------------
              2,000 total observations
              58 event time missing (wanted>=.) PROBABLE ERROR
              ------------------------------------------------------------------------------
              1,942 observations remaining, representing
              1,937 failures in single-record/single-failure data
              125,852.15 total analysis time at risk and under observation
              at risk from t = 0
              earliest observed entry t = 0
              last observed exit t = 64.95003

              I then decided to go ahead with the previous command [stset wanted, failure(preg==1) scale(365.25)] and got the output below

              failure event: preg == 1
              obs. time interval: (0, wanted]
              exit on or before: failure
              t for analysis: time/365.25

              ------------------------------------------------------------------------------
              2,000 total observations
              58 event time missing (wanted>=.) PROBABLE ERROR
              ------------------------------------------------------------------------------
              1,942 observations remaining, representing
              39 failures in single-record/single-failure data
              125,852.15 total analysis time at risk and under observation
              at risk from t = 0
              earliest observed entry t = 0
              last observed exit t = 64.95003


              My concern with this output is that there are supposed to be 32 failures and not 39. They are 39 because some of the women had pregnancy test positive input more than once (id 211,237,186).

              Code:
              * Example generated by -dataex-. To install: ssc install dataex
              clear
              input int record_id byte tpregtest_result float preg
               17 1 1
               18 1 1
              105 1 1
              111 1 1
              153 1 1
              176 1 1
              186 1 1
              186 1 1
              186 1 1
              197 1 1
              199 1 1
              210 1 1
              211 1 1
              211 1 1
              216 1 1
              216 1 1
              228 1 1
              237 1 1
              237 1 1
              237 1 1
              238 1 1
              265 1 1
              317 1 1
              317 1 1
              317 1 1
              319 1 1
              333 1 1
              337 1 1
              340 1 1
              341 1 1
              359 1 1
              367 1 1
              369 1 1
              385 1 1
              391 1 1
              401 1 1
              409 1 1
              458 1 1
              461 1 1
              end
              label values tpregtest_result tpregtest_result_
              label def tpregtest_result_ 1 "Positive", modify
              label values preg preg_
              label def preg_ 1 "Yes", modify
              Last edited by Galenda Nagudi; 06 Jan 2025, 14:12.

              Comment


              • #8
                I still went ahead to run this command [by record_id (ddate_of_visit), sort: egen pos_preg_test ///
                = max(tpregtest_result == "Positive" :tpregtest_result)]

                However, I get this message after it runs "(value label dereference "Positive":tpregtest_result not found)" and the output for pos_preg_test is not right as seen in the stset command output below.
                My error. I misread the name of the value label when I wrote that command, so Stata couldn't find the value label I named. It's (visually) subtle: the value label name ends in an underscore (_) character, which I omitted. So it's
                Code:
                by record_id (ddate_of_visit), sort: egen pos_preg_test ///
                    = max(tpregtest_result == "Positive" :tpregtest_result_)
                My concern with this output is that there are supposed to be 32 failures and not 39. They are 39 because some of the women had pregnancy test positive input more than once (id 211,237,186).
                I don't think that is the reason this happened. If you were using the code in #6, there would only be one observation for each of these women, regardless how many positive pregnancy tests she had. I think the problem is that some of the failures did not get recognized for other reasons, but not seeing the complete code and output, I can only speculate.

                Rather than pursuing that, let me revisit the original question from #1. You want to do a time to event analysis where the event is getting a positive pregnancy test. So your time variable needs to be the date of the earliest positive pregnancy test, if there is one, or the date of the last negative pregnancy test if there are no positive tests. You can accomplish that with the following code:
                Code:
                // VARIABLE INDICATING ID HAS AT LEAST 1 POSITIVE PREGNANCY TEST
                by record_id (ddate_of_visit), sort: egen pos_preg_test ///
                    = max(tpregtest_result == "Positive":tpregtest_result_)
                
                // DATE OF EARLIEST POSITIVE TEST, IF ANY--THIS WILL BE THE EVENT DATE
                by record_id: egen event_date = ///
                    min(cond(tpregtest_result == "Positive":tpregtest_result_, ddate_of_visit, .))
                
                // DATE OF LAST NEGATIVE TEST IF NO POSITIVE TESTS OCCURRED
                // THIS WILL BE THE CENSORSHIP DATE
                by record_id: egen last_neg_result = ///
                    max(cond(tpregtest_result == "Negative":tpregtest_result_, ddate_of_visit, .))
                by record_id (ddate_of_visit), sort: replace event_date = last_neg_result if pos_preg_test == 0
                format event_date %td
                
                // REDUCE TO ONE OBSERVATION PER WOMAN
                by record_id: keep if _n == 1
                
                // REMOVE VARIABLES THAT ORIGINALLY VARIED WITHIN WOMAN OVER TIME
                drop ddate_of_visit redcap_event_name tpregtest_date preg_res last_neg_result
                
                // -stset- THE DATA
                stset event_date, fail(pos_preg_test == 1) enter(firstdate) origin(firstdate) ///
                    scale(365.25)
                Added note: This code simply ignores observations for which tpregtest_result is missing. I did that because I do not understand what that means in real world terms in the context of your study. If it means that the woman was not tested on that occasion, or the test produced an uninterpretable result, then the code will still work correctly.
                Last edited by Clyde Schechter; 06 Jan 2025, 15:25.

                Comment


                • #9
                  Thank you so much Clyde, your input was immensely helpful. Since I needed to use stptime to get the incidence, I added id to the code so ended up with [stset event_date, fail(pos_preg_test == 1) enter(firstdate) origin(firstdate) /// scale(365.25) id(record_id)]

                  Comment

                  Working...
                  X