# st: looping over observations to find a special case

 From "Cathy L. Antonakos" <[email protected]> To [email protected] Subject st: looping over observations to find a special case Date Sat, 15 Dec 2007 20:18:53 -0500 (EST)

Dear Statalist,

I am investigating reports of pain prior to and following pain medication in a special population. Observations are spread across two days, with six observations per day randomly spaced.

I've identified the observation period during each day with the minimum elapsed time since the last medication was received ("current period"). I've also identified the observation period immediately following that period.

I need help finding the preceding observation period meeting the following criteria:
(1) the time elapsed between the chosen preceding period and the current period must be greater than the time elapsed since the last medication (reported in the current period); and,
(2) the chosen preceding period must be the nearest one to the current period that meets the first criterion above.

I want to find an efficient way to loop over any and all preceding periods to find and flag the correct one. (In some cases, there may yet be data entry errors, such as in row 114 below, where lapsed time since last medication leaps up by 210 minutes though only one hour passed since the prior observation.)

id = person evnt = observation period lpsdtm = lapsed minutes reported since last medication received. lpsdmin1 = minimum lapsed time (minutes) since last medication, for the person
hour = time of day in hours

Please let me know if this is unclear. I've tried my best to be clear and I appreciate your help.

Cathy

-------------------------------------------------------------------------------

Here is a data listing for day 1.
(row 111. "preceding period" has 3 hours pass till next observation)
(row 112. "current period" has lapsed time since last med = 20 mins.)

+---------------------------------------------+
| idx evnt lpsdtm lpsdmin1 hour |
|---------------------------------------------|
109. | 23 11 . . 9 |
110. | 23 12 . . 12 |
111. | 23 13 . . 13 |<< "preceding period"
112. | 23 14 20 20 16 |<< "current period" 113. | 23 15 80 . 17 |
|---------------------------------------------|
114. | 23 16 290 . 18 |
145. | 28 11 . . 8 |
146. | 28 12 . . 9 |
147. | 28 13 . . 10 |
148. | 28 14 . . 11 |
|---------------------------------------------|
149. | 28 15 . . 14 |
150. | 28 16 6 6 17 |
205. | 39 11 22 22 8 |
206. | 39 12 310 . 13 |
207. | 39 13 370 . 14 |
|---------------------------------------------|
208. | 39 14 430 . 15 |
209. | 39 15 590 . 18 |
210. | 39 16 680 . 19 |
217. | 40 11 5 5 10 |
218. | 40 12 66 . 11 |
|---------------------------------------------|
219. | 40 13 155 . 12 |
220. | 40 14 335 . 15 |
221. | 40 15 395 . 16 |
222. | 40 16 485 . 19 |
229. | 42 11 . . 8 |
|---------------------------------------------|
230. | 42 12 . . 9 |
231. | 42 13 . . 10 |
232. | 42 14 . . 11 |
233. | 42 15 . . 14 |
234. | 42 16 470 470 17 |
|---------------------------------------------|
253. | 44 11 30 30 8 |
254. | 44 12 290 . 13 |
255. | 44 13 350 . 14 |
256. | 44 14 410 . 15 |
257. | 44 15 650 . 18 |
|---------------------------------------------|
258. | 44 16 740 . 19 |
418. | 60 11 575 . 8 |
419. | 60 12 90 . 9 |
420. | 60 13 150 . 10 |
421. | 60 14 210 . 11 |
|---------------------------------------------|
422. | 60 15 150 . 14 |
423. | 60 16 30 30 17 |
489. | 68 11 1 1 9 |
490. | 68 12 181 . 12 |
491. | 68 13 271 . 13 |
|---------------------------------------------|
492. | 68 14 421 . 16 |
493. | 68 15 35 . 17 |
494. | 68 16 125 . 18 |
512. | 71 11 80 80 10 |
513. | 71 12 140 . 11 |
|---------------------------------------------|
514. | 71 13 230 . 12 |
515. | 71 14 170 . 15 |
516. | 71 15 230 . 16 |
517. | 71 16 150 . 19 |
+---------------------------------------------+

(end of posting)
*
* 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/