I would like to create seq_temp variable which is a count from 1,2,3, of temp (within each personid) where episode==1. See below what I would like to see in seq_temp

input byte personid float episode float temp float seq_temp
1 1 1 1
1 0 1 .
1 1 1 2
1 1 0 .
1 0 1 .
2 0 1 .
2 1 0 .
2 1 1 1

When I try this below I seem to get a conflict where STATA says "the data is not sorted":

by personid temp episode: gen seq_temp=_n if temp==1 & episode==1