Thank you both for your help on this. The code below from Romalpa gives me the output I want.
gen b = encounter_date bys subject_id (encounter_date encounter_num): replace b = b[_n-1] if b < b[_n-1] + 31 & _n>1 bys subject_id b: gen v4 = _n>1 rangestat (count) v5 = encounter_num, interval(encounter_date 2 14) by(subject_id) replace v5 = cond(v4,.,cond(v5 ==.,0,v5))
gen b = encounter_date bys subject_id (encounter_date encounter_num): replace b = b[_n-1] if b < b[_n-1] + 31 & _n>1 bys subject_id b: gen v4 = _n>1 rangestat (count) v5 = encounter_num, interval(encounter_date 2 14) by(subject_id) replace v5 = cond(v4,.,cond(v5 ==.,0,v5))
Comment