Announcement

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

  • Generate one exit variable based on different variables

    Hi Statalist

    I want to generate an exit (out of cohort) variable. I have a large dataset with a lot of different dates.
    You can exit the cohort when you turn 5 (60months), move (move) or die (death). Variable name in ().

    I am only interested in seeing the dates for these outcomes in the interval of 1jan2010 to 1jan2011. The others should just be missing values.

    My try that does not work as I still have dates outside of the interval:

    Code:
    gen exit = .
    
    {
    replace exit = min(death, move, 60months) if inrange(death, td(1jan2010), td(1jan2011)) | inrange(move, td(1jan2010), td(1jan2011)) | inrange(60months, td(1jan2010), td(1jan2011))
    }
    I do not have access to the data at the moment, sorry. I am looking for suggestions on optimizing my code or a better way of doing it.

  • #2
    Anybody with a suggestion?

    Comment


    • #3
      I think I need to see a data example. https://www.statalist.org/forums/help#stata explains -- including an explanation that fake data with the same structure are fine if your data are confidential.

      For one, 60months can't be a variable in your dataset. The name is illegal.

      Comment


      • #4
        Id Move Death Fiveyears
        1 . . 07-08-2010
        2 . . 20-11-2010
        3 15-02-2015 . 21-10-2010
        4 . . 07-05-2011
        5 . 06-05-2010 14-04-2011
        6 . . 05-03-2011
        7 16-04-2016 . 16-04-2012
        8 07-06-2010 . 21-05-2011
        9 . 18-11-2010 22-02-2012

        Hi Nick, thank you for taking a look.
        I have made a data example.

        I am interested in getting a variable that has the date for those that move, die or turn 5 within 1jan2010 to 1jan2011. Death can only be in this interval as it is a variable I have generated. The other variables can be outside this.
        I can solve it but the solution I have come up with is labor "heavy" and seems overcomplicated.

        Comment


        • #5
          OK. that's a good start but what are these variables? Strings with dates? Numeric variables with a daily date display format? What are the blanks supposed to be? Empty strings or numeric missings? Using dataex -- see link in #3 -- would clarify these points.

          Comment


          • #6
            The variables are dates for when an event occurs.
            Blanks are empty string i.e. an event has not occured.

            The data in the table is how I would see it. I do not have access to the data at the moment.

            Comment


            • #7
              When I copy and paste, I see not blanks but system missings in the (nearly) empty cells. So I surmise this is what you have and what you want.

              The use of a local macro is just to reduce my typing.

              Code:
              * Example generated by -dataex-. For more info, type help dataex
              clear
              input byte Id float(Move Death Fiveyears)
              1     .     . 18481
              2     .     . 18586
              3 20134     . 18556
              4     .     . 18754
              5     . 18388 18731
              6     .     . 18691
              7 20560     . 19099
              8 18420     . 18768
              9     . 18584 19045
              end
              format %tdDD-NN-CCYY Move
              format %tdDD-NN-CCYY Death
              format %tdDD-NN-CCYY Fiveyears
              
              local range mdy(1,1,2010), mdy(1,1,2011)
              gen exit = inrange(Move, `range') | inrange(Death, `range') | inrange(Fiveyears, `range')
              
              list 
              
                   +--------------------------------------------------+
                   | Id         Move        Death    Fiveyears   exit |
                   |--------------------------------------------------|
                1. |  1            .            .   07-08-2010      1 |
                2. |  2            .            .   20-11-2010      1 |
                3. |  3   15-02-2015            .   21-10-2010      1 |
                4. |  4            .            .   07-05-2011      0 |
                5. |  5            .   06-05-2010   14-04-2011      1 |
                   |--------------------------------------------------|
                6. |  6            .            .   05-03-2011      0 |
                7. |  7   16-04-2016            .   16-04-2012      0 |
                8. |  8   07-06-2010            .   21-05-2011      1 |
                9. |  9            .   18-11-2010   22-02-2012      1 |
                   +--------------------------------------------------+

              Comment


              • #8
                Thanks for the reply Nick.

                Comment

                Working...
                X