Date: Sat, 9 Oct 1999 02:01:57 -0400 "Paul M. Dorfman" "SAS(r) Discussion" "Paul M. Dorfman" KInPh Re: Looking at 5 consecutive observations text/plain; charset=koi8-r

Julia,

If your data is not immense, then POINT= options lends itself quite nicely to a really simplistic solution:

%LET NCONSEQ = 5;

DATA COWCYCLE (KEEP=COW DIM PROG CYCLE); RETAIN COW DIM PROG CYCLE START; SET COWS; BY COW; IF FIRST.COW THEN DO; START = _N_; CYCLE = .; END; IF CYCLE = . AND PROG GE 500 THEN DO; CYCLE = DIM; DO PTR=_N_ + 1 TO _N_ + &NCONSEQ; SET COWS POINT=PTR; IF PROG < 500 THEN LEAVE; END; IF PTR < _N_ + &NCONSEQ THEN CYCLE = . ; END; IF LAST.COW THEN DO PTR=START TO _N_; SET COWS POINT=PTR; OUTPUT; END; RUN;

Note that if you let NCONSEQ = 1, the above automatically reduces to the solution to the problem you have posted earlier, CYCLE serving as NEWVAR.

Kind regards, ================== Paul M. Dorfman Jacksonville, FL ==================

Julia Hertl wrote: > > Hi SAS-L, > We have another question, similar to the one I posted a few days ago on > assigning values to a new variable, in cows with a progesterone level (PROG) > >=500. > > Now, we want to create a new variable, CYCLE, which will take on the value > of the first DIM (day in milk) of a cow, on which PROG>=500, but only if the > next 4 observations on the cow also have PROG>=500. We are having trouble > "looking ahead" to the 4 subsequent observations. So, the **first** time a > cow has 5 consecutive observations in which PROG>=500, the value of CYCLE > should take on the value of DIM for the first in the series. We do not care > about subsequent sequences later in lactation in which PROG>=500 for 5 or > more consecutive observations. > > Example data (what we have already): > > COW DIM PROG > 1 14 410 > 1 16 450 > 1 18 520 > 1 21 600 > 1 23 590 > 1 25 610 > 1 28 660 > 1 30 490 > 1 32 510 > 1 35 540 > 1 37 550 > 1 39 520 > 1 41 550 > 2 13 420 > 2 15 510 > 2 17 530 > 2 20 550 > 2 22 420 > 2 24 520 > 2 27 530 > 2 29 540 > 2 31 560 > 2 34 570 > 3 13 450 > 3 15 400 > 3 17 550 > 3 19 520 > 3 22 380 > 3 24 500 > 3 26 340 > What we want: > > COW DIM PROG CYCLE > 1 14 410 18 > 1 16 450 18 > 1 18 520 18 > 1 21 600 18 > 1 23 590 18 > 1 25 610 18 > 1 28 660 18 > 1 30 490 18 > 1 32 510 18 > 1 35 540 18 > 1 37 550 18 > 1 39 520 18 > 1 41 550 18 > 2 13 420 24 > 2 15 510 24 > 2 17 530 24 > 2 20 550 24 > 2 22 420 24 > 2 24 520 24 > 2 27 530 24 > 2 29 540 24 > 2 31 560 24 > 2 34 570 24 > 3 13 450 . > 3 15 400 . > 3 17 550 . > 3 19 520 . > 3 22 380 . > 3 24 500 . > 3 26 340 . > > Here, CYCLE=18 for Cow 1 because 18 is the value of DIM at which PROG>=500, > and stays >=500 for at least 5 observations in a row. Even though PROG>=500 > for another, later series of observations beginning at DIM=32, we do not > care about this. > CYCLE=24 for Cow 2 because that is the first day (DIM) on which PROG remains > above 500 for 5 consecutive observations. > CYCLE=. for Cow 3 because her PROG never goes above 500 for 5 consecutive > observations. > > We've tried various things, like using a count variable going from 1 to 5, > but we aren't getting what we want. Does anybody have any suggestions? > Thank you very much. > > Julia Hertl > 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