Date: Sun, 21 Mar 1999 15:41:11 +0000
Reply-To: John Whittington <medisci@POWERNET.COM>
Sender: "SAS(r) Discussion" <SAS-L@UGA.CC.UGA.EDU>
From: John Whittington <medisci@POWERNET.COM>
Subject: Re: Trouble using lags to assign values?
Content-Type: text/plain; charset="us-ascii"
At 00:49 21/03/99 -0500, LINCK, JIM wrote:
>Is there some limitation to lags that they can't be used to assign values in
>a data step? For example, suppose I have a list like:
>ID Date Var1
>1 1965 2
>1 1966 .
>1 1967 .
>etc.
>where . are missing values. All I want to do is assign 1966's . a 2 and
>1967's . a 2. Thought I could do it as simply as:
>if ID=lag(ID) then do;
> if Var1=. then Var1=lag(Var1);
>end;
>No dice. Doesn't work. ....
You have hit the classic 'catch' of conditionally calling a LAG function in
SAS. However, if all you want to do is 'carry forward' real values into
subsequent missing ones, you don't need LAG functions at all - you can
simply do it by retaining the last non-missing value. Something like:
data whatever (drop = lastval) ;
retain lastval ;
<code as required>
if var1 = . then var1 = lastval ; else lastval = var1 ;
run ;
Regards
John
----------------------------------------------------------------
Dr John Whittington, Voice: +44 (0) 1296 730225
Mediscience Services Fax: +44 (0) 1296 738893
Twyford Manor, Twyford, E-mail: medisci@powernet.com
Buckingham MK18 4EL, UK mediscience@compuserve.com
----------------------------------------------------------------
|