Date: Mon, 29 Apr 1996 18:14:05 -0700
Reply-To: Robin Way <robin@BCINC.COM>
Sender: "SAS(r) Discussion" <SAS-L@UGA.CC.UGA.EDU>
From: Robin Way <robin@BCINC.COM>
Organization: Barakat & Chamberlin, Inc.
Subject: Re: Lags with BY variables
Michael Friendly wrote:
> I'm trying to generate lags of observations in a dataset with
> a separate lagged series for each value of a BY variable (SEQ).
> I tried this:
> data lags;
> set codegen;
> by seq;
> if first.seq then do;
> lag1 = ' ';
> lag2 = ' ';
> else do;
> lag1 = lag(code);
> lag2 = lag2(code);
> But this does not work for the second and subsequent value of the
> by variable.
I remember consulting SI tech support about a similar problem. I liked Tim
Berryhill's solution to your problem, because (here's the rationale): lags
should not be invoked conditionally (i.e., should not be part of an IF
statement or IF-based DO loop). The right way to handle them is to execute
the lag statement up front, without any qualifications, (such as, xlag1 =
lag(x);) and then reset the value of the dataset var later based on
conditional logic (much like Tim suggested).
Barakat & Chamberlin, Inc.