# st: coding problem: looping through a list of ID's

 From "Leny Mathew" To statalist@hsphsun2.harvard.edu Subject st: coding problem: looping through a list of ID's Date Mon, 24 Nov 2008 10:23:24 -0500

```Dear Statalisters,
I've have been using stata for a while but
am a novice when it comes to loops and macros. I'm hoping that someone
on the list could help with with the following problem. I have a data
set with 74 patients each with 148 measurements of height over a
period of 10 years. The height measurements for all cases are not
necessarily at the same time and not everybody has 148 measurements.
The number 148 is a result of changing the data set into the long
form.
I'm trying to calculate the the the increase in height for a one year
(approximately) interval for each case. I developed the following code
for this purpose, but am not able to get it to work perfectly. It
loops though each patient, but at the end, 'phv' end up being the same
for everyone in the data.  Also, this code might be a totally
convoluted way of doing this and I'm hoping that someone could give me
some pointers on how to improve/ revamp this completely.
If this posting is a violation of the list protocol on type of
posting, please feel free to let me know and I'll take it off. I've
spent quite some time tweaking this and am out of ideas.
I'm using stata10.1 Any suggestions are much appreciated.

Note: 'dov'  is date of visit, used to create age.

sort pt_id dov
by pt_id: gen timec=_n

gen counter=_n

sort pt_id dov
by pt_id: gen num=_N

local j=counter
local i=1
local k=1
while pt_id <75 {
display pt_id[_n]
while (pt_id[`j']==pt_id[`j'+1]) & timec < 148{
local value= max(`j'+147, num[pt_id])
while (`value' >
max(`j',1)) & (`value' > timec[`j']) & (pt_id[`j']==`k') {
replace phv=
(pt_ht[`value']-pt_ht[counter[`i']]) if
((age[`value']-age[counter[`i']]) >0.95 &
(age[`value']-age[counter[`i']]) < 1.5) & (pt_id[`j']==`k')
local i= `i'+1
if `i'==`value' local
value= `value'-1
if `i'==`value'+1 &
pt_id[`j']==1 local i=1
if `i'==`value'+1 &
(pt_id[`j'] >1) local i=(`k'-1)*148
}
local i=`i'+148
local j=`j'+148
display `j'
local k=`k'+1
display `k'
continue
if k >74 break
}
}

**I tried to use phv[`value'] so that it would replace the value at
the certain row, but that gave me an error that weights are not
allowed. (I'm sure that that must have been a violation of stata
rules!)

Leny
*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/
```