Statalist


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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


From   "Ada Ma" <heu034@googlemail.com>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: coding problem: looping through a list of ID's
Date   Tue, 25 Nov 2008 13:18:08 +0000

how about not using loop?  I see that you have an age variable but I
have no idea how it looks like (for instance is it all rounded up?) so
let's assume that you have a date variable and it is in Stata's date
format so that I can count the number of days elapsed in between
height being measured.  I am also assuming that you don't mind
negative height growth.  You may be able to tailor my suggestion
somehow to fit your purpose.

bysort pt_id (date): gen heightgrowth=pt_ht-pt_ht[_n-1]
bysort pt_id (date): gen timeelapsed=date-date[_n-1]
gen heightgrowthperyear=heightgrowth/timeelapsed*365.25



On Mon, Nov 24, 2008 at 3:23 PM, Leny Mathew <lenymathewc@gmail.com> wrote:
> 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/
>
*
*   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/



© Copyright 1996–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index