Date: Mon, 24 Nov 2003 17:46:47 +0000
Reply-To: John Whittington <John.W@MEDISCIENCE.CO.UK>
Sender: "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From: John Whittington <John.W@MEDISCIENCE.CO.UK>
Subject: Re: how to use lag
In-Reply-To: <E1AOL9M-0003ja-00@coumxnn01.netbenefit.co.uk>
Content-Type: text/plain; charset="us-ascii"; format=flowed
At 11:09 24/11/03 -0700, Richard Read Allen wrote:
>Helen, Below is a simple way to accomplish what you want using retain
>rather than lag. One caution about the lag function is that it's best not
>to use it in
>a conditional statement as you have done (twice). It does funky things when
>used this way.
>
>[snip]
>
>data two;
> set one;
> retain x;
> if y>. then x=y;
>run;
Richard, I'm afraid there are all sorts of reasons why this code would not
accomplish what Helen wants.
Firstly, it does not respect the ID boundaries, as Helen wants - i.e. it
would attempt to 'carry forward' data from one ID to the next. Secondly,
the logic of the code is simply not correct. As written, the value of x
will ALWAYS be missing, and will not achieve the desired 'carry
forward'. However, it is very easy to achieve Helen's desired
functionality using RETAIN rather than lag functions, something like:
data two (drop = last_y) ;
retain last_y ;
set one;
by id;
if first.id then x=y;
else if y=. then x=last_y;
else x=y;
last_y = y ;
run ;
Kind Regards,
John
----------------------------------------------------------------
Dr John Whittington, Voice: +44 (0) 1296 730225
Mediscience Services Fax: +44 (0) 1296 738893
Twyford Manor, Twyford, E-mail: John.W@mediscience.co.uk
Buckingham MK18 4EL, UK mediscience@compuserve.com
----------------------------------------------------------------
|