Announcement

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

  • Select First Event Or Last Non Event

    Ciao, Here is a replication example in Stata. Here is data I have:

    clear
    Code:
     
     * Input data input float id str7 event time id  event   time 1   "." 10 1   "." 20 1   "1" 30 1   "0" 40 1   "." 50 2   "0" 10 2   "0" 20 2   "0" 30 2   "0" 40 2   "0" 50 3   "1" 10 3   "1" 20 3   "0" 30 3   "." 40 3   "." 50 4   "." 10 4   "." 20 4   "." 30 4   "." 40 4   "." 50 5   "1" 10 5   "1" 20 5   "1" 30 5   "1" 40 5   "1" 50      end
    Here is data I hope to get to:
    Code:
     
     * Input data input float id str7 event time id1 event1  time1 1   1   30 2   0   50 3   1   10 4   .   50 5   1   10  end
    Basically my aim is to take the first row for each id of a event equal to 1. If an id has no event for any time then I want to report the last time of the report.

  • #2
    Please use -dataex- to provide example data. See the FAQ 12.2 here: https://www.statalist.org/forums/help#stata
    Stata/MP 14.1 (64-bit x86-64)
    Revision 19 May 2016
    Win 8.1

    Comment


    • #3

      Carole J. Wilson gives excellent advice as always. Here's some more:

      This was cross-posted by "bvowe" and answered at https://stackoverflow.com/questions/...last-non-event

      In the FAQ Advice all members are asked to read before posting we explain our policy on cross-posting, which is that you are asked to tell us about it.
      Last edited by Nick Cox; 07 Nov 2018, 00:46.

      Comment


      • #4
        Code:
        bys id (time): gen tag = 1 if  event == “1” | _n ==_N
        bys id (tag time): keep if _n == 1
        drop tag

        Comment


        • #5
          The quotation marks should be

          Code:
          " "
          Otherwise that's in my view better than other answers posted previously on SO. I have posted a copy there.

          Comment

          Working...
          X