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;

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 667 views
  • 0 likes
  • 4 in conversation