BookmarkSubscribeRSS Feed
Eugenio211
Quartz | Level 8

Hi - I'm trying to get the date difference (number of days) of the loan dates of each customer.  The idea is to get the number of days: loan 1 - loan 2, then loan 1 - loan 3, and then loan 1 - loan 4.

 

below is my example data:

 

CUSTOMER LOAN # LOANDATE Date Diff
Cust A 1 4-Jan-20  
Cust A 2 11-Feb-20  
Cust A 3 27-Mar-20  
Cust A 4 4-Apr-20  
Cust B 1 21-Jun-20  
Cust B 2 18-Jul-20  
Cust B 3 15-Aug-20  
Cust B 4 9-Sep-20  

 

Thank you so much!

3 REPLIES 3
novinosrin
Tourmaline | Level 20

Hi @Eugenio211  An expected sample of output would help, or do you mean this?-

 



data have;
input CUSTOMER & $12.	LOAN 	LOANDATE :date9.;
format loandate date9.;
cards;
Cust A	1	4-Jan-20	 
Cust A	2	11-Feb-20	 
Cust A	3	27-Mar-20	 
Cust A	4	4-Apr-20	 
Cust B	1	21-Jun-20	 
Cust B	2	18-Jul-20	 
Cust B	3	15-Aug-20	 
Cust B	4	9-Sep-20
;

data want;
 set have;
 by customer;
 if first.customer then _iorc_=loandate;
 else datediff=intck('day',_iorc_,loandate);
run;
CUSTOMER LOAN LOANDATE datediff
Cust A 1 04JAN2020 .
Cust A 2 11FEB2020 38
Cust A 3 27MAR2020 83
Cust A 4 04APR2020 91
Cust B 1 21JUN2020 .
Cust B 2 18JUL2020 27
Cust B 3 15AUG2020 55
Cust B 4 09SEP2020 80

 

me55
Quartz | Level 8

hi eugenio...

 

i think you have two issues, how to select the dates then how to subtract them.  

 

so, you can use into to put a loan into a variable and use it later so...

 

proc sql;

select LOANDATE

    into :LNDT01

  from TABLE

  where CUSTOMER=CUSTA and LOAN=1;

quit;

 

so that puts it into a variable LNDT01 and using that you can put each date into a variable like that then to subtract them, here is a page describing that...

 

SAS Doc Date Intervals 

 

by combing that, you can do what you are looking for.  

    

Tom
Super User Tom
Super User

SAS stores dates as number of days. So to find the difference in days just subtract.

To find the difference between observations you can use the DIF() function.  

Since you have different CUSTOMERs you need to use BY group processing.  Is it important to run DIF() on every observation.  So find the difference first and then set it missing when you are starting a new customer.

data want;
  set have;
  by customer loan_number;
  date_diff = dif(loandate);
  if first.customer then date_diff=.;
run;

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

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
  • 476 views
  • 0 likes
  • 4 in conversation