Announcement

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

  • Collapsing survival data

    Dear all,

    I have a very big data set with a survival structure that I want to compress/collapse. Right now the data look like this:
    ID t0 t1 y x1 x2
    1 0 1 0 0 0
    1 1 2 0 0 0
    1 2 3 0 3 1
    1 3 4 0 2 1
    1 4 5 0 2 1
    1 5 6 1 2 0
    1 6 7 0 1 0
    1 7 8 0 2 1
    1 8 9 1 2 1
    t0 and t1 specify the start and end time, y is some outcome and x1 and x2 are time-varying variables.
    What I want to do is to collapse spells of t0 t1 where none of the time-varying variables (including y) vary for a period.

    In other words, I want to transform the data above to look like this:
    ID t0 t1 y x1 x2
    1 0 2 0 0 0
    1 2 3 0 3 1
    1 3 5 0 2 1
    1 5 6 1 2 0
    1 6 7 0 1 0
    1 7 8 0 2 1
    1 8 9 1 2 1

    Example code:
    Code:
    input str1 id     t0         t1         y         x1         x2
            1        0        1        0        0        0    
            1        1        2        0        0        0
            1        2        3        0        3        1
            1        3        4        0        2        1
            1        4        5        0        2        1
            1        5        6        1        2        0
            1        6        7        0        1        0
            1        7        8        0        2        1
            1        8        9        1        2        1
    end

  • #2
    Your example data is not helpful because:

    1. The tableaux that you show are, for some reason, not coming into the clipboard as tables when I copy them, so I am unable to paste them into the Stata editor and come out with a usable data set.

    2. The code you show at the bottom, which I guess is your homebrew version of -dataex- output, is the result you want, not what you started with. To test code, an example of the starting data is needed.

    But I think I can see the solution in this case without usable example data:

    Code:
    collapse (min) t0 (max) t1, by(id y x1 x2)
    If this does not do what you want, please post back with example data from your starting data set posted using the -dataex- command, and a clearer explanation of what you want.

    If you are running version 16 or a fully updated version 15.1 or 14.2, -dataex- is already part of your official Stata installation. If not, run -ssc install dataex- to get it. Either way, run -help dataex- to read the simple instructions for using it. -dataex- will save you time; it is easier and quicker than typing out tables. It includes complete information about aspects of the data that are often critical to answering your question but cannot be seen from tabular displays or screenshots. It also makes it possible for those who want to help you to create a faithful representation of your example to try out their code, which in turn makes it more likely that their answer will actually work in your data.

    Comment

    Working...
    X