| Date: | Thu, 16 Feb 2006 15:27:33 -0500 |
| Reply-To: | Nathaniel_Wooding@DOM.COM |
| Sender: | "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU> |
| From: | Nat Wooding <Nathaniel_Wooding@DOM.COM> |
| Subject: | Re: Help with lag |
|
| In-Reply-To: | <697B27DC3052C74FB9A3A3FACED43ED819D730@njb-ms1> |
| Content-Type: | text/plain; charset="US-ASCII" |
|---|
Here's a solution. Note that you do not need the retain in the first data
step and you will need to read the dates as SAS date values.
Nat Wooding
data x;
* retain visit;
input patient $10. visit $3. collection_date date11.;
format col: mmddyy10.;
cards;
0004 00001B0116-JAN-2002
0004 00001B0313-FEB-2002
0004 00001B0509-FEB-2002
0004 00001B0802-JUL-2002
0004 00001B1027-AUG-2002
0004 00001B9917-SEP-2003
0004 00001C0113-NOV-2002
0004 00001C0706-MAY-2003
run;
Data y;
set x;
lastvisit=lag(collection_date);
format col: lastvisit mmddyy10.;
if lastvisit gt collection_date then flag='*';
proc print;
run;
"Sridhar, Kumar"
<nsridhar@MEDAREX
.COM> To
Sent by: "SAS(r) SAS-L@LISTSERV.UGA.EDU
Discussion" cc
<SAS-L@LISTSERV.U
GA.EDU> Subject
Help with lag
02/16/2006 03:10
PM
Please respond to
"Sridhar, Kumar"
<nsridhar@MEDAREX
.COM>
Hi All:
Here's a question that a colleague asked me and since I was
stumped, I decided to ask you folks.
data x;
retain visit;
input patient $10. visit $3. collection_date $11.;
cards;
0004 00001B0116-JAN-2002
0004 00001B0313-FEB-2002
0004 00001B0509-FEB-2002
0004 00001B0802-JUL-2002
0004 00001B1027-AUG-2002
0004 00001B9917-SEP-2003
0004 00001C0113-NOV-2002
0004 00001C0706-MAY-2003
;
run;
If you observe, the third observation has a date of 09-FEB-2002 which is
earlier than the previous visit but the visit number is greater. I want
to put a flag on those observations (in this case the 3rd one) where the
date is earlier than the previous visit.
I tried the following but it puts a flag on all observations so any
input from you to correct the code would be really appreciated.
data x;
set x;
if visit>lag(visit) and collection_date<lag(collection_date)
then flg='NR';
run;
proc print;
run;
Kumar
-----------------------------------------
CONFIDENTIALITY NOTICE: This electronic message contains
information which may be legally confidential and/or privileged and
does not in any case represent a firm ENERGY COMMODITY bid or offer
relating thereto which binds the sender without an additional
express written confirmation to that effect. The information is
intended solely for the individual or entity named above and access
by anyone else is unauthorized. If you are not the intended
recipient, any disclosure, copying, distribution, or use of the
contents of this information is prohibited and may be unlawful. If
you have received this electronic transmission in error, please
reply immediately to the sender that you have received the message
in error, and delete it. Thank you.
|