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

 From "Nick Cox" To Subject st: RE: coding problem: looping through a list of ID's Date Mon, 24 Nov 2008 15:34:20 -0000

```I don't know how you propose to withdraw this if anyone objects. For the
record, I can't see any violation of protocol here, just a question that
looks unlikely to get much of an answer.

You may be lucky, but few people want to wade through code as convoluted
as this, especially when you advertise in advance that it is wrong.

I note that despite your statement that "not everybody has 148
measurements" you are jumping by 148 in your code. That sounds like a
recipe for problems.

I note also that you are not using dates in your calculations, but
counters.

I suggest you start again and show us the structure of the data and
define more precisely how height increase over a year is to be defined
from measurements irregular in time.

Nick
n.j.cox@durham.ac.uk

Leny Mathew
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!)

*
*   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/
```