Announcement

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

  • Counts and Sequence in Long Form Data Sets

    I am working with a long form data set. Each row is an event in an individual case. Thus, cases are repeated, events are unique each on a different date. I am trying to generate two new variables. One would be the sequence of each event for the case with oldest being 1. The other variable would be a count of the number of events that each case had.This can be repeated on each row.
    What commands would accomplish this or what general category would it fall under.

    sincerely,
    Hatem

  • #2
    This is just a standard application of by: and something like

    Code:
     
    bysort caseid (date) : gen seq = _n 
    by caseid : gen count = _N
    For more guidance, do read the help for by:, sections on by: in [U] or
    http://www.stata-journal.com/sjpdf.html?articlenum=pr0004

    Comment


    • #3
      So, you don't tell us what your variables are named. I'll assume that a variable called case identifies distinct cases, and one called event_date provides the date of each event. Also, critically, I assume that event_date is a numeric Stata date variable--not a string variable that humans read as dates. (If you have the latter, you should convert using the date() function.)

      Code:
      by case (event_date), sort: gen sequence = _n
      by case (event_date): gen event_count = _N

      Comment


      • #4
        Thank you. As you can see I am new to the Statalist. Next time I will provide greater detail.

        Comment

        Working...
        X