```Date: Thu, 21 Oct 1999 15:04:39 -0400 Reply-To: Julia Hertl Sender: "SAS(r) Discussion" From: Julia Hertl Subject: Area under the curve problems Content-Type: text/plain; charset="us-ascii" Hello SAS-L, The purpose of this exercise is to calculate a specified area of the progesterone curve (variable = PROG in data set). This area is specified to lie between the time interval from when the **first** PROG value goes above 500 until the value goes below 500 again. In the data set below (at end of message) this would be the area between Obs 97 and Obs 102 (DIM 41-52). (Note DIM 41= DIM1_CYC (this is days in milk at first cycle)). The area for each of the single intervals is given by the trapezoidal formula =(dim-(previous dim))*((prog at dim + (prog at previous dim))/2). We have tried to calculate the single areas by using the lag function in this way: if dim>=cycle and prog>500 then do; area=(dim-lag(dim))*((prog+lag(prog))/2); cnt=0; if first.cow then area=.; end; cnt+1; if prog<500 then area=.; if last.cow then cnt=0; run; In the code above, AREA and CNT are variables being created for the 1st time; all the other variables (dim, cycle, prog, cow) already exist. Problem 1) The area between obs 96 and 97 is calculated to be (41-205)*(679 + 2901)/2 = -293560, instead of the correct value which is supposed to be (41-38)*(679 + 0)/2 = 1018.5. This because, in our current code, SAS goes back to the last obs for the **previous** cow to do the calculation. We have tried several ways to correct this without being successful so far. Suggestions would be highly appreciated. Problem 2) Next we want to sum up the single areas, but only for those within obs 97 to 102, omitting the areas calculated after that time (from obs 107). The start of the interval is defined by DIM1_CYC (=41) and the interval is supposed to end when PROG < 500 (DIM = 52). Here is a portion of our data, in which AREA has been generated by the above (incorrect) code: OBS COW DIM1_CYC DIM PROG AREA 82 2778 102 203 2684 7846.5 83 2778 102 205 2901 5585.0 84 2780 41 10 34 . 85 2780 41 13 95 . 86 2780 41 15 21 . 87 2780 41 17 51 . 88 2780 41 20 9 . 89 2780 41 22 26 . 90 2780 41 24 24 . 91 2780 41 27 9 . 92 2780 41 29 202 . 93 2780 41 31 587 . 94 2780 41 34 183 . 95 2780 41 36 75 . 96 2780 41 38 0 . 97 2780 41 41 679 -293560.0 98 2780 41 43 1002 1681.0 99 2780 41 45 1174 2176.0 100 2780 41 48 1669 4264.5 101 2780 41 50 1662 3331.0 102 2780 41 52 1884 3546.0 103 2780 41 55 16 . 104 2780 41 57 0 . 105 2780 41 59 19 . 106 2780 41 62 732 13080.0 107 2780 41 64 1555 2287.0 108 2780 41 66 1639 3194.0 109 2780 41 69 2639 6417.0 110 2780 41 71 2721 5360.0 111 2780 41 73 2676 5397.0 112 2780 41 76 2835 8266.5 113 2780 41 78 2460 5295.0 Thank you for your help. Julia Hertl and Olav Reksen Section of Epidemiology Dept. of Population Medicine and Diagnostic Sciences College of Veterinary Medicine Cornell University Ithaca, NY 14853 ```

Back to: Top of message | Previous page | Main SAS-L page