BookmarkSubscribeRSS Feed
ankit1
Calcite | Level 5

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.

3 REPLIES 3
art297
Opal | Level 21

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

 

LinusH
Tourmaline | Level 20
What comparison do wish to do, and what values do you want to assign?
Please show a want data output.
Data never sleeps
Ksharp
Super User

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;

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 3 replies
  • 767 views
  • 1 like
  • 4 in conversation