# st: Compute a summary variable based on a predefined algorithm

 From "Shuaib Kauchali" To STATALIST Subject st: Compute a summary variable based on a predefined algorithm Date Sat, 17 Mar 2007 19:59:40 +0200

Dear subscribers,

Thanks for taking my query.

I have data set of birth cohort data with longitudinal follow-up of these children till they were 9months old (270days), unless they were lost to follow up or died before then.

the data structure looks like this:
Childid (repeated group variable, daily visit to the clinic)
day (day of visit)
v1 (diarrhea on that day of visit)
v2 <--this is the variable I would like to get(defined as diarrhea episodes: a string of 1's separated by at least 3 consecutive 0's is an episode)

childid day v1 v2 1 1 . . 1 2 . . 1 3 . . 1 4 . . 2 1 0 1
2 2 1 1
2 3 1 1
2 4 0 1
3 1 1 2
3 2 1 2
3 3 0 2
3 4 0 2
3 5 0 2
3 6 1 2
3 7 1 2
4 1 1 1
4 2 . 1
4 3 1 1
4 4 0 1
4 5 . 1
4 6 0 1
4 7 0 1
5 1 0 1
5 2 1 1
5 3 0 1
5 4 1 1
6 1 0 0
6 2 0 0
6 3 0 0
6 4 0 0
6 5 0 0
6 6 0 0
6 7 0 0

Note:
1. childid=4 is a bit tricky because of missing values; we assume the episode to be one as there were not more than 3 days separating 2 events.
2. childid=1 has not had any visits recorded, so he gets missing values for v2.
3. not everyone is followed-up for the same period: loss to follow-up, death, or completed the study (in my data set this should happen when the child reaches 270 days from birth. This is a birth cohort of 2500 children)

My problem is I am unable to manipulate the data in Stata to get me the summary v2 of the number of episodes of diarrhea per child by total number of days observed. I am new to stata, but have been am a good learner (I have many of the stata press books to help). One way I came across in the books was to use explicit subscriptiing; this would allow me to count the total number of days followed per child; but I ma not sure how to create the alogorithm for the v2 creation--perhaps foreach, forvalue, or even while, local macro???. I find the commands a bit intimidating for a newcomer, but am willing to spend time learning it.

Can anyone help?
Best wishes

Shuaib

*
* For searches and help try:
* http://www.stata.com/support/faqs/res/findit.html
* http://www.stata.com/support/statalist/faq
* http://www.ats.ucla.edu/stat/stata/