Hello Sandhya,
From reading, and rereading, the messages in this thread, I think that I understand your problem well enough now for me to contribute the following code that may be of some use. Of course, there may have been some subtlety that I missed.
During testing, I used the original dataset that you provided and also the modified one that SPR provided. In both cases, the mainly PROC SQL code below generated the results that I expected. However, of course, that is no guarantee that there are no bugs lurking somewhere in it.
The code below is just something additional that you may want to look at, or consider. It complements the nice responses that others in this thread have contributed.
==========================================================
options ls=max;
data work.ds;
input id $ adate:mmddyy10. service $;
format adate mmddyy10.;
datalines;
1 12/7/2009 I
2 1/12/2009 I
2 1/13/2009 OP
2 1/13/2009 OP
3 6/4/2009 OP
3 6/5/2009 OP
4 7/29/2009 LA
5 3/23/2009 OP
;
/*--
data work.ds;
input id $ adate:mmddyy10. service $;
format adate mmddyy10.;
datalines;
1 12/7/2009 I
2 1/12/2009 I
2 1/13/2009 OP
3 6/5/2009 I
4 7/29/2009 LA
5 3/23/2009 OP
;
--*/
proc sql;
create table work.sI as
select *
from work.ds
where service = 'I';
create table work.sNI as
select *
from work.ds
where service not = 'I';
/*---------------------------------------------------------*/
/*-- Identify those records in work.sNI that have the --*/
/*-- same id as one or more records in work.sI and whose --*/
/*-- adates are either the same as one of these records --*/
/*-- or corresponds to a record in work.sI that has a --*/
/*-- date one day earlier than the corresponding work.sNI--*/
/*-- record. The records in the result set for this --*/
/*-- query are those that need to have their service --*/
/*-- changed from 'OP' to 'I'. --*/
/*---------------------------------------------------------*/
create table work.needToChange as
select *
from work.sNI
where id in (select id
from work.sI
where ((sNI.adate - sI.adate) in (0,1)))
and service = 'OP';
/*-------------------------------------------------------*/
/*-- Identify the work.sNI records that do NOT need to --*/
/*-- have their service values change from 'OP' to 'I'.--*/
/*-------------------------------------------------------*/
create table work.dontNeedToChange as
select * from work.sNI
except all
select * from work.needToChange;
/*------------------------------------------------------*/
/*-- Create the new dataset, sorted if need be. --*/
/*------------------------------------------------------*/
create table work.result as
select id, adate, service from work.sI
union all
select id, adate, 'I' from work.needToChange
union all
select id, adate, service from work.dontNeedToChange
order by id, adate, service; /* if ordering is desired */
quit;
proc print; run;
==========================================================
Sincerely,
qseries@sas