Announcement

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

  • Read stset command data into macro

    Dear StataListers

    I am on STATA 14.

    I have searched around in the stset manual and online, but not found any way to read stset meta-data into a local macro.
    I would like to save the stset command or just the failure event with estadd and tabulate it together with the stcox estimates with esttab.

    However, neither st, stdescribe nor stsum provides scalars or locals to this regard. Where are below data stored? And can they be programmatically accessed?

    Code:
    -> stset fu3time_death, id(id) failure(fu3_death==1) scale(365.25)
    
                    id:  id
         failure event:  fu3_death == 1
    obs. time interval:  (fu3time_death[_n-1], fu3time_death]
     exit on or before:  failure
        t for analysis:  time/365.25
    Best regards Nino

  • #2
    You may have to create a log file to extract the information.

    Code:
    webuse drugtr, clear
    log using myfile.txt, replace
    stset studytime, failure(died)
    log close 
    preserve
    import delimited using myfile.txt, clear
    levelsof v1 if ustrregexm(v1, "stset"), local(wanted1) clean
    levelsof v1 if ustrregexm(v1, "failure event"), local(wanted2) clean
    restore
    display "`wanted1'"
    display "`wanted2'"
    Res.:

    Code:
    . stset studytime, failure(died)
    
         failure event:  died != 0 & died < .
    obs. time interval:  (0, studytime]
     exit on or before:  failure
    
    ------------------------------------------------------------------------------
             48  total observations
              0  exclusions
    ------------------------------------------------------------------------------
             48  observations remaining, representing
             31  failures in single-record/single-failure data
            744  total analysis time at risk and under observation
                                                    at risk from t =         0
                                         earliest observed entry t =         0
                                              last observed exit t =        39
    
    
    . display "`wanted1'"
    . stset studytime, failure(died)
    
    . 
    . display "`wanted2'"
         failure event:  died != 0 & died < .

    Comment


    • #3
      Along with creating additional variables, stset stores metadata in the dataset characteristics using the char command - see section 12.8 of the Stata User's Guide PDF documentation included in your Stata installation and accessible from Stata's Help menu.

      Now Stata does not document the interpretation of these characteristics, but fortunately the st command is implemented as an ado. Some examination of the display of
      Code:
      viewsource st.ado
      let me create the following example.
      Code:
      // based on code found in st.ado
      capture program drop st_return
      program define st_return, rclass
      if `"`_dta[st_bd]'"' != "" {
              if `"`_dta[st_ev]'"' != "" {
                      local failure `"`_dta[st_bd]'==`_dta[st_ev]'"'
              }
              else    local failure `"`_dta[st_bd]'!=0 & `_dta[st_bd]'<."'
      }
      else {
              local failure `"(assumed to fail at time=`_dta[st_bt]')"'
      }
      return local failure "`failure'"
      end
      
      webuse drugtr, clear
      
      stset studytime, failure(died)
      char list
      st_return
      display "`r(failure)'"
      
      stset studytime, failure(died==1)
      char list
      st_return
      display "`r(failure)'"
      Code:
      . webuse drugtr, clear
      (Patient survival in drug trial)
      
      .
      . stset studytime, failure(died)
      
      Survival-time data settings
      
               Failure event: died!=0 & died<.
      Observed time interval: (0, studytime]
           Exit on or before: failure
      
      --------------------------------------------------------------------------
               48  total observations
                0  exclusions
      --------------------------------------------------------------------------
               48  observations remaining, representing
               31  failures in single-record/single-failure data
              744  total analysis time at risk and under observation
                                                      At risk from t =         0
                                           Earliest observed entry t =         0
                                                Last observed exit t =        39
      
      . char list
        _dta[_dta]:                 st
        _dta[st_ver]:               2
        _dta[st_bt]:                studytime
        _dta[st_bd]:                died
        _dta[st_o]:                 0
        _dta[st_s]:                 1
        _dta[st_bs]:                1
        _dta[st_d]:                 _d
        _dta[st_t0]:                _t0
        _dta[st_t]:                 _t
      
      . st_return
      
      . display "`r(failure)'"
      died!=0 & died<.
      
      .
      . stset studytime, failure(died==1)
      
      Survival-time data settings
      
               Failure event: died==1
      Observed time interval: (0, studytime]
           Exit on or before: failure
      
      --------------------------------------------------------------------------
               48  total observations
                0  exclusions
      --------------------------------------------------------------------------
               48  observations remaining, representing
               31  failures in single-record/single-failure data
              744  total analysis time at risk and under observation
                                                      At risk from t =         0
                                           Earliest observed entry t =         0
                                                Last observed exit t =        39
      
      . char list
        _dta[_dta]:                 st
        _dta[st_ver]:               2
        _dta[st_bt]:                studytime
        _dta[st_bd]:                died
        _dta[st_ev]:                1
        _dta[st_o]:                 0
        _dta[st_s]:                 1
        _dta[st_bs]:                1
        _dta[st_d]:                 _d
        _dta[st_t0]:                _t0
        _dta[st_t]:                 _t
      
      . st_return
      
      . display "`r(failure)'"
      died==1
      
      .

      Comment


      • #4
        Dear Andrew Musau and William Lisowski thank You both for putting so much effort in answering my question.
        I was unaware of the "char list" command. Unfortunately, char list produces hundreds of lines in my large dataset, because i have previously run the destring command on it, which apparently leaves meta-data on each and every variable.
        But this can be overcome by using
        Code:
        .   qui char list
        . estadd local fail = "`_dta[st_bd]'"
        
        added macro:
                       e(fail) : "fu3_death_hf"
        Less sophisticated than your examples, admittedly


        Thank you again for your kind help.


        Best regards Nino
        Last edited by Nino Landler; 02 Feb 2023, 13:54.

        Comment


        • #5
          Code:
           qui char list
          is not needed.

          The char list command was in my examples only to show the sort of (undocumented) information that is added to the dataset characteristics by the stset command, and to show that different expressions of the command can cause different characteristics to be added.

          Comment

          Working...
          X