DATA Step, Macro, Functions and more

Select most recent rows with changed value only

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 104
Accepted Solution

Select most recent rows with changed value only

Hi there,

 

For your kind information, I am trying to identify only those records which have been submitted with some changes. For example in the table mentioned below, TYPE has been changed in the most recent report with reference to the earliest report submitted. 

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


data want ;
format date_1 date9. ;
input id $ sp_num $ type $  date_1 yymmdd8.;
datalines;
101 a01 b 20160103
103 c03 b 20160108
;
run;

Can anybody kindly guide me to get it. 

 

Thank you in advance. 

Regards,

 

Swain

Accepted Solutions
Solution
‎06-02-2017 09:31 AM
PROC Star
Posts: 7,473

Re: Select most recent rows with changed value only

Posted in reply to DeepakSwain

I think I answered my own question. Are you looking to do something like?:

data want (drop=change);
  set have;
  by id sp_num;
  retain change;
  if first.sp_num then change=0;
  if type ne lag(type) and not first.sp_num then change=1;
  if change and last.sp_num then output;
run;

Art, CEO, AnalystFinder.com

 

View solution in original post


All Replies
PROC Star
Posts: 7,473

Re: Select most recent rows with changed value only

Posted in reply to DeepakSwain

did you also want the following record in your want dataset?:

102 b02 b 20160105

Art, CEO, AnalystFinder.com

Frequent Contributor
Posts: 104

Re: Select most recent rows with changed value only

Hi art297,

As the value of type has not changed, that record is not included.

Regards,

 

Swain
Solution
‎06-02-2017 09:31 AM
PROC Star
Posts: 7,473

Re: Select most recent rows with changed value only

Posted in reply to DeepakSwain

I think I answered my own question. Are you looking to do something like?:

data want (drop=change);
  set have;
  by id sp_num;
  retain change;
  if first.sp_num then change=0;
  if type ne lag(type) and not first.sp_num then change=1;
  if change and last.sp_num then output;
run;

Art, CEO, AnalystFinder.com

 

Frequent Contributor
Posts: 104

Re: Select most recent rows with changed value only

Hi art297,
The advice given is great !
Can you advice me further to identify all records having change in TYPE with reference to the first one.
Regards,
Swain
PROC Star
Posts: 7,473

Re: Select most recent rows with changed value only

Posted in reply to DeepakSwain

In order to identify all instances where type is different than the initial value for type, I'd suggest a slightly different approach:

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

data want (drop=change);
  set have;
  by id sp_num;
  retain first_type;
  if first.sp_num then first_type=type;
  else if type ne first_type then output;
run;

Art, CEO, AnalystFinder.com

Frequent Contributor
Posts: 104

Re: Select most recent rows with changed value only

Hi art297,

 

Thanks for taking the pain to answer my silly questions. It has been a good learning experience. 

 

Regards,

 

Swain
☑ This topic is solved.

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

Discussion stats
  • 6 replies
  • 152 views
  • 1 like
  • 2 in conversation