Announcement

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

  • Numbering sessions within events

    I have been working on this problem for a while, searching statalist and consulting chatgpt without success. I have a large dataset of events (with numeric IDs), some of which have sessions (stored as string variables). I need a way to generate session numbers for each distinct session *within each event*. In the example dataset below, I manually input the variable I would like to generate as "des_output". If there are no sessions, a session number should not be generated.

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input float event str6 session float des_output
    25 "four"   1
    25 "one"    2
    25 "one"    2
    25 "one"    2
    25 "two"    3
    40 "purple" 1
    40 "purple" 1
    40 "violet" 2
    72 "smell"  1
    72 "smell"  1
    72 "smell"  1
    72 "taste"  2
    99 ""       .
    99 ""       .
    end
    This command below
    Code:
    quietly by event session: gen sess_num = cond(_N==1,1,_n)
    creates numbering *within sessions* for each combination of event number and string session name.
    Other commands I tried counted sessions across the entire dataset. Does anyone know how to generate session numbers within events as shown in the variable des_output?

    I greatly appreciate any guidance you can offer.

  • #2
    Code:
    by event (session), sort: gen wanted = sum(session != session[_n-1])
    replace wanted = . if missing(session)

    Comment


    • #3
      Thank you, Clyde. This works perfectly!

      Comment

      Working...
      X