Marcel extended the example by asking: Is there a way to make sure that the data set is sorted to be sure that the assumption: "* We assume that the order of visits in set12 is chronological, holds" ?
I recommend having a time in the "visits record", preferably: e.g. datevisit, but also a sequential number in patient follow up studies, e.g.visit 1=start 2=4monts 3=12 months 4=2years etc.
In real empirical data it usually happens that one or more visits are either missed or taking place at a point in time not exactly being on time. E.g. how to designate a visit 5 monts after initial treatment in a study where visits are supposed to be at 4 and 6 months. Sequence often indicates certain tests (e.g. blood samples) which vary by visit, e.g. more expensive ones less often, such as x-rays. By using as well the date and the sequence we can know exactly what are the data, which variables are missing and what is the time difference from "supposed time of visit".
Therefore careful planning obviously remedies this by having the possibility to sort as well on sequence and date.
sort id visit
In the control of such data I would always:
read rawdata *suppose we need 4 visits per person, numbered 1 2 3 4 * 1+2+3+4=10 aggregate studyid /sum="visit" /close gen i ok=(sumvisit=10) list studyid visit if ok <> 1
* or the same in "epi6" mode: read rawdata aggregate .... as above... define ok # let ok = 0 if (sumvisit = 10) then ok = 1 select if ok <> 1 list studyid select