Desktop productivity for business analysts and programmers

Subsequent Rows Comparison for a unique id

Reply
Learner
Posts: 1

Subsequent Rows Comparison for a unique id

I have a certain data which requires me to compare a particular row with all the prior rows in a group and make an indicator accordingly. For e.g 

IDIssue_datePaid_to_dateProduct
126-Dec-1626-Dec-16ARS
126-Dec-1626-Dec-16ARS
126-Dec-1626-Dec-16ASS
126-Dec-1626-Dec-16ASS
126-Dec-1626-Dec-16ASS
228-Dec-1628-Dec-16ARS
228-Dec-1628-Dec-16ARS
228-Dec-1628-Dec-16ARS

 

This is the dataset. Now i want to compare the second number row of first id with the first row of first id. And i want to compare the third row of first id with the second and first row of first id, fourth row of first id with the first,second, third row of first id. So basically i want to compare a particular row with all the prior rows for a given id. And i want to repeat the steps for all the other ids in the dataset. I tried but i could only compare two subsequent rows and not the ones prior to them. 

Can you please help.

PROC Star
Posts: 7,431

Re: Subsequent Rows Comparison for a unique id

You can use the lag function to enable the comarison of any number of previous records. What is the maximum number of records you might want to compare, what do you want to compare, and what are you ultimatelly trying to achieve through the comparisons?

 

Art, CEO, AnalystFinder.com

 

Super User
Posts: 5,382

Re: Subsequent Rows Comparison for a unique id

What comparison do wish to do, and what values do you want to assign?
Please show a want data output.
Data never sleeps
Super User
Posts: 9,865

Re: Subsequent Rows Comparison for a unique id

The output you want ?

 

 

data have;
infile cards expandtabs truncover;
input ID	(Issue_date	Paid_to_date	Product) (:$20.);
cards;
1	26-Dec-16	26-Dec-16	ARS
1	26-Dec-16	26-Dec-16	ARS
1	26-Dec-16	26-Dec-16	ASS
1	26-Dec-16	26-Dec-16	ASS
1	26-Dec-16	26-Dec-16	ASS
2	28-Dec-16	28-Dec-16	ARS
2	28-Dec-16	28-Dec-16	ARS
2	28-Dec-16	28-Dec-16	ARS
;
run;
data temp;
 set have;
 by id;
 if first.id then n=0;
 n+1;
run;
proc sql;
create table want as
select a.*,b.id as _id,b.issue_date as _issue_date,
       b.paid_to_date as _paid,b.product as _product,
	   b.n as _n
 from temp as a,temp as b
  where a.id=b.id and a.n gt b.n
   order by a.id,a.n,b.n;
quit;
Ask a Question
Discussion stats
  • 3 replies
  • 136 views
  • 1 like
  • 4 in conversation