BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Tomcaty
Obsidian | Level 7

Hello all, 

I have a dataset with two dates Date_1 and Date_2. Date_1 has format datetime16. and informat 16. Date_2 has format $8. and informat $8. How do I calculate the date difference i.e Date_1- Date_2 in days and months. 

 

Date_1Date_2
29Aug09:14:02:0020090621

 

output: 

 

DaysMonths
692.26

 

Appreciate your help, 

Thanks!

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

A fun with functions type exercise. 

 

1. Convert dates to same type - use DATEPART() to get date portion of a DATETIME variable and use INPUT() to convert the character variable to a date variable. 

2. Use INTCK() to calculate the differences - note the rules on how it's calculated and the last parameter (fourth) that defines the rules.

 

To convert the character variable you can use:

 

date2_var = input(date_2, yymmdd8.);
format date2_var date9.;

 

Here's the link to the most recent documentation on functions:

http://support.sas.com/documentation/cdl/en/lefunctionsref/69762/HTML/default/viewer.htm#p0w6napahk6...

 

If you have difficulty with the remaining functions, post what you've tried and what errors or incorrect output you receive.

View solution in original post

2 REPLIES 2
Reeza
Super User

A fun with functions type exercise. 

 

1. Convert dates to same type - use DATEPART() to get date portion of a DATETIME variable and use INPUT() to convert the character variable to a date variable. 

2. Use INTCK() to calculate the differences - note the rules on how it's calculated and the last parameter (fourth) that defines the rules.

 

To convert the character variable you can use:

 

date2_var = input(date_2, yymmdd8.);
format date2_var date9.;

 

Here's the link to the most recent documentation on functions:

http://support.sas.com/documentation/cdl/en/lefunctionsref/69762/HTML/default/viewer.htm#p0w6napahk6...

 

If you have difficulty with the remaining functions, post what you've tried and what errors or incorrect output you receive.

Ksharp
Super User
data _null_;
infile cards expandtabs truncover;
input Date_1 : datetime32. Date_2 : yymmdd8.;
days=datepart(date_1)-date_2;
month=mod(yrdif(date_2,datepart(date_1),'act/act'),1)*12;
put days= month= ;
cards;
29Aug09:14:02:00	20090621
;
run;

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!

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
  • 2 replies
  • 1687 views
  • 1 like
  • 3 in conversation