DATA Step, Macro, Functions and more

Select most recent row with changed value along with the related earliest record for comparison

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 96
Accepted Solution

Select most recent row with changed value along with the related earliest record for comparison

Hi there,

 

For your kind information, I am trying to identify only those records which have been submitted with some changes along with the related earliest record for comparison. For example in the table mentioned below, TYPE has been changed in the most recent report with reference to the earliest report submitted in case of id 101 so it will be available in WANT table whereas id 103 will not be available in WANT table as earliest and latest value for TYPE is same.

data have ;
format date_1 date9. ;
input id $ sp_num $ type $  date_1 yymmdd8.;
datalines;101 a01 p 20160102
101 a01 b 20160102
101 a01 b 20160105
102 b02 b 20160104
102 b02 b 20160105
103 c03 p 20160106
103 c03 b 20160107
103 c03 p 20160108
104 d04 p 20160102
104 d04 b 20160104
;
run;


data want ;
format date_1 date9. ;
input id $ sp_num $ type $  date_1 yymmdd8.;
datalines;
101 a01 p 20160102
101 a01 b 20160105
104 d04 p 20160102
104 d04 b 20160104
;
run;

Can anybody kindly guide me to get it. 

 

Thank you in advance. 

Regards,

Swain

Accepted Solutions
Solution
‎06-05-2017 10:03 AM
Super User
Posts: 9,682

Re: Select most recent row with changed value along with the related earliest record for comparison

data have ;
input id $ sp_num $ type $  date_1 yymmdd8.;
format date_1 date9. ;
datalines;
101 a01 p 20160102
101 a01 b 20160103
101 a01 b 20160105
102 b02 b 20160104
102 b02 b 20160105
103 c03 p 20160106
103 c03 b 20160107
103 c03 p 20160108
104 d04 p 20160102
104 d04 b 20160104
;
run;
data temp;
 set have;
 by id;
 if first.id or last.id;
run;
proc sql;
create table want as
 select *
  from temp
   group by id
    having count(distinct type)=2;
quit;

View solution in original post


All Replies
PROC Star
Posts: 253

Re: Select most recent row with changed value along with the related earliest record for comparison

what happend when you similar min dates for more than one observation for an id. what happend when you similar max dates for more than one observation for an id.

 

Frequent Contributor
Posts: 96

Re: Select most recent row with changed value along with the related earliest record for comparison

Hi kiranv_, 

the multiple records within same date will be sorted based on time stamp and even if changes are found within records within same date will be considered for flagging. 

 

Regards,

 

Swain
PROC Star
Posts: 253

Re: Select most recent row with changed value along with the related earliest record for comparison

[ Edited ]

Modified data just to keep one min and max. I think this should work.

 

data have ;

input id $ sp_num $ type $  date_1 yymmdd8.;
format date_1 date9. ;
datalines;
101 a01 p 20160102
101 a01 b 20160103
101 a01 b 20160105
102 b02 b 20160104
102 b02 b 20160105
103 c03 p 20160106
103 c03 b 20160107
103 c03 p 20160108
104 d04 p 20160102
104 d04 b 20160104
;
run;

/*picking up min and max records*/
proc sql;
create table inter as
select *, monotonic() as rnum from want a
where date_1=(select  max(date_1) as max_d
               from want b
			   where a.id =b.id
			   group by id)
or date_1=(select  min(date_1) as max_d
               from want b
			   where a.id =b.id
			   group by id)
order by id, date_1;
quit;
/* picking up the rows which have change in type you can extent it to others if you want*/
proc sql;
create table want as 
select id, sp_num, type, date_1 from inter a
inner join
inter b 
on a.id = b.id
and a.type <> b.type
where (a.rnum+1 =b.rnum
or b.rnum +1 =a.rnum);
quit;
Solution
‎06-05-2017 10:03 AM
Super User
Posts: 9,682

Re: Select most recent row with changed value along with the related earliest record for comparison

data have ;
input id $ sp_num $ type $  date_1 yymmdd8.;
format date_1 date9. ;
datalines;
101 a01 p 20160102
101 a01 b 20160103
101 a01 b 20160105
102 b02 b 20160104
102 b02 b 20160105
103 c03 p 20160106
103 c03 b 20160107
103 c03 p 20160108
104 d04 p 20160102
104 d04 b 20160104
;
run;
data temp;
 set have;
 by id;
 if first.id or last.id;
run;
proc sql;
create table want as
 select *
  from temp
   group by id
    having count(distinct type)=2;
quit;
☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 4 replies
  • 119 views
  • 2 likes
  • 3 in conversation