Announcement

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

  • #16
    After exclusion of those individuals with afib-diagnos at baseline I want to know the following:

    Time from examination date (examdate1) to diagnose (ICD10), where I want the earliest if those cases there are more than one. rowmin should be there somewhere I think. Last time rowmin only showed the lowest value, not the lowest value in those columns with I48 only.
    afibdd=afib diagnose date

    gen afibdd=0
    egen earliest = rowmin(ts_41280_0_*)
    format earliest %td
    forvalues i=0/242 {
    local var s_41270_0_`i'
    local time ts_41280_0_`i'
    recode afibdd (0=1) if `time'==earliest & substr(`var', 1, 3) =="I48"
    }

    Doesn't search for earliest time in those with I48 only like I want.

    . tab afib afibdd

    | afibdd
    afib | 0 1 | Total
    -----------+----------------------+----------
    0 | 986 0 | 986
    1 | 50 5 | 55
    -----------+----------------------+----------
    Total | 1,036 5 | 1,041
    Last edited by Andreas Rydell; 17 Feb 2023, 02:14.

    Comment


    • #17
      I found the code here on statalist for time to event from baseline:

      gen ttdm = (earliest - examdate1)/(365/12)

      As mentioned in previous post I need an if statement when making earliest. In the output now I get minus values, wich wouldn't be possible since I have excluded all individuals that have diagnose prior to baseline examination.

      Comment


      • #18
        #15 would seem equivalent to


        Code:
        gen afib = 0 
        
        foreach var of varlist s_41270_0_* {
            replace `var' = "I48" if substr(`var', 1, 3) =="I48" 
            replace afib = 1 if `var' == "I48"
        }
        
        generate exclude = 0 
        
        foreach time of var ts_41280_0_* {  
            replace exclude = 1 if afib == 1 & `time' <= examdate1
        }
        
        tab exclude
        tab afib

        Comment


        • #19
          Originally posted by Nick Cox View Post
          #15 would seem equivalent to


          Code:
          gen afib = 0
          
          foreach var of varlist s_41270_0_* {
          replace `var' = "I48" if substr(`var', 1, 3) =="I48"
          replace afib = 1 if `var' == "I48"
          }
          
          generate exclude = 0
          
          foreach time of var ts_41280_0_* {
          replace exclude = 1 if afib == 1 & `time' <= examdate1
          }
          
          tab exclude
          tab afib
          The previous code had 65 with afib vs 10 exclude.

          The code from #18 has 49 exclude. When I manually look in the data it seems that 10 is correct.

          What I'm struggling with now is to get the earliest date if more than one column with I48 diagnose:

          gen afibdd=0
          egen earliest = rowmin (ts_41280_0_*)
          format earliest %td
          forvalues i=0/242 {
          local var s_41270_0_`i'
          local time ts_41280_0_`i'
          recode afibdd (0=1) if (`time'==earliest & substr(`var', 1, 3) =="I48")
          }
          tab afib afibdd

          gen ttdm = (earliest - examdate1)/(365/12)
          gen ttdy = ttd/12
          drop if earliest<=examdate1
          tab earliest

          If only earliest could be the date with I48 or earliest date if several, Im all set for calculating time to diagnos from baseline.


          Comment


          • #20
            We aren't seeing any real(istic) data examples but the earliest date with a I48 as a diagnosis may need to be sought with a customised loop.


            Code:
            gen earliest = . 
            
            forvalues i=0/242 {
            local var s_41270_0_`i'
            local time ts_41280_0_`i'
            replace earliest = `time' if `time' < earliest & substr(`var', 1, 3) =="I48")
            }

            Comment


            • #21
              Originally posted by Nick Cox View Post
              We aren't seeing any real(istic) data examples but the earliest date with a I48 as a diagnosis may need to be sought with a customised loop.


              Code:
              gen earliest = .
              
              forvalues i=0/242 {
              local var s_41270_0_`i'
              local time ts_41280_0_`i'
              replace earliest = `time' if `time' < earliest & substr(`var', 1, 3) =="I48")
              }
              Thank you. That worked. I now have time from baseline examination to afib diagnose among those with the diagnose only. For the other individuals there is =. in the column. I did two time to diagnose (months/year, TTDM and TTDY respectively). But I also need to make a time to censor for the other one to be able to do survival analyses:

              replace ttdm = td(01jan2022-examdate1)/(365/12) if ttdy==. doesnt work in its current form. Jan 2022 was chosen just to have a follow up date.

              I previously did:

              gen ttdm = (earliest - examdate1)/(365/12)
              *gen ttdy = ttd/12
              drop if earliest<=examdate1

              None were dropped like it should since those had been removed in a previous step.

              Comment


              • #22
                Code:
                td(1jan2022) - examdate1
                is more likely to be what you want.

                Comment


                • #23
                  Originally posted by Nick Cox View Post
                  Code:
                  td(1jan2022) - examdate1
                  is more likely to be what you want.
                  Wonderful Nick. Thank you very much. Almost there now. Maybe you easily can see why my output gets wrong here?

                  I have time to event in month (ttdm) and years (ttdy) among those individuals that get an event from examination date to end of follow up. So far so good:

                  gen earliest = .
                  forvalues i=0/242 {
                  local var s_41270_0_`i'
                  local time ts_41280_0_`i'
                  replace earliest = `time' if (`time' < earliest & substr(`var', 1, 3) =="I48")
                  }
                  tab earliest
                  format earliest %td
                  gen ttdm = (earliest - examdate1)/(365/12)
                  gen ttdy = ttd/12

                  The output here is correct in month and year.


                  The problem is all those individuals that never get afib and has a "." in ttdm. I want an end date of follow up for survival analyses. For some reason I get wrong data output-format. I tried to tweak the code slightly with no success.

                  replace ttdm = td(01jan2022)-examdate1 if ttdm==.

                  ttdm
                  4764
                  4686

                  94.68493 correct
                  5343
                  4214
                  4679

                  145.0192 correct


                  replace ttdm = td(01jan2022)-examdate1/(365/12) if ttdm==.

                  same here,
                  ttdm
                  22595.62
                  89.42466
                  22596.1

                  I think it has to do with "td(01jan2022)" somehow since "(earliest-examdate1)/(365/12) get correct output.

                  Comment


                  • #24
                    td(1jan2022) is just 22646. If you're getting correct answers in some cases and not others, it's either the data or the code. You seem to be subtracting something that's not a daily date from a daily date, which seems puzzling if not wrong.

                    Comment


                    • #25
                      Your problem is that the expression to the right of the = in your replace command should be exactly like the expression in your generate command but with earliest changed to td(1jan2022)

                      Comment


                      • #26
                        When I did like this it worked

                        gen enddate= td(01jan2022)
                        format enddate %td
                        replace ttdm = (enddate-examdate1)/(365/12) if ttdm==.

                        Can't thank you guys enough for your help.🙏

                        Comment


                        • #27
                          Here we go again....

                          If these two works:

                          gen afib=0
                          foreach var of varlist s_41270_0_* {
                          recode afib (0=1) if `var'=="I48"
                          }

                          foreach var of varlist s_41270_0_* {
                          replace `var'="I48" if (`var'=="I480" | `var'=="I481" ///
                          | `var'=="I482" | `var'=="I483" | `var'=="I484" | `var'=="I485" ///
                          | `var'=="I486" | `var'=="I487" | `var'=="I488" | `var'=="I489")
                          }

                          Why do I get "typ mismatch" error on this?:

                          gen statintreatment=0
                          foreach var of varlist n_20003_0_* {
                          recode statintreatment (0=1) if `var'=="1141146234"

                          }

                          and "==invalid name" error on this?

                          foreach var of varlist n_20003_0_* {
                          replace `var'=="1141146234" if (`var'=="1141146138" | `var'=="1141192410" ///
                          | `var'=="1141192414" | `var'=="1140888648" | `var'=="1140861970" | `var'=="1140861958" ///
                          | `var'=="1140881748")
                          }

                          Comment


                          • #28
                            Why do I get "type mismatch" error on this?:
                            Code:
                            gen statintreatment=0
                            foreach var of varlist n_20003_0_* {
                            recode statintreatment (0=1) if `var'=="1141146234"
                            My guess is that your variables beginning n_20003_0_ are numeric variables rather than string variables, so you should in theory have
                            Code:
                            recode statintreatment (0=1) if `var'==1141146234
                            I will add that for this to be correct it requires that your n_20003_0_ variables be either double (preferably) or long. You have 10-digit numbers; double will hold precisely numbers up to 15 digits long, while long will hold precisely numbers up to 9 digits long, or 10 digits no larger than 2,147,483,620.

                            and "==invalid name" error on this?
                            Code:
                            foreach var of varlist n_20003_0_* {
                            replace `var'=="1141146234" if (`var'=="1141146138" | `var'=="1141192410" ///
                            | `var'=="1141192414" | `var'=="1140888648" | `var'=="1140861970" | `var'=="1140861958" ///
                            | `var'=="1140881748")
                            }
                            Because "==" is used only for comparison, as in
                            Code:
                            `var'=="1141146138"
                            "=" is needed for generate and replace, as in
                            Code:
                            replace `var'="1141146234"
                            But once you get past that error, I expect the first problem will again occur, since you are again comparing and replacing the n_20003_0_ variables using strings rather than numbers.
                            Last edited by William Lisowski; 22 Feb 2023, 10:39.

                            Comment


                            • #29
                              n2003 is type= long and 41270 is type=str6

                              I think this could be the reason it doesnt work.

                              Comment


                              • #30
                                I wrote at the same time you were posting.

                                EDIT: I found a workaround.
                                Last edited by Andreas Rydell; 24 Feb 2023, 07:46.

                                Comment

                                Working...
                                X