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

Hello,

 

I am trying to use the DATDIF function to retrieve the amount of actual days between two dates:

 

The dates I am working with are numeric and in the format eg. 170102 and 170320 (2nd Jan 2017 and 20th Mar 2017).

 

I am unsure of how to prep these dates such that when I input them into DATDIF it will be able to run properly.

 

Any help is much appreciated,

Declan

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

Is your value a numeric with the value of 170102. Or is it a date value of 20821 displayed with the SAS display format of yymmdd6.?

 

SAS data values which are required for any of the date functions to work are days offset from 1 Jan 1960. So if you date 170102 is supposed to be 2 Jan 2017 then the numeric value would have to be 20821.

 

BTW when working with SAS FORMAT has a very specific interpretation of a display, not the actual values.

 

If your value is numeric and ALL of them have 6 digits you can create a SAS date value for use with the date functions and date FORMATS with

 

newdatevar = input(put(datevar,6.),yymmdd6.);

in a data step where datevar is your existing date variable and newdatevar is the SAS date valued variable.

 

The appearance of the newdatevar would be set with a format, either at use or assign a default format at creation with a line like:

 

format newdatevar date9. ; <= displays date as 02JAN2017

format newdatevar mmddyy10. ; <= displays date as 01/02/2017 or any of many date format choices.

 

The SAS formats are very flexible in that you can use a different format in different procedures to create different groupings of your date variables without creating new variables.

 

 

A second hint: Using 2 digit years in this day an age is getting to be a poor practice. It is not obvious that 170102 is in 2017 or 1917.

And when the date in question is 010203 which is month, day and year is a complete guess.

View solution in original post

4 REPLIES 4
PeterClemmensen
Tourmaline | Level 20

Do something like this.

 

You can use the DATDIF function, but I prefer the INTCK function because it is much more flexible. 

I included examples of both, yielding the same result 🙂

 

data have;
input date1:yymmdd6. date2:yymmdd6.;
datalines;
170102 170320
;

data want;
	set have;
	format date1 date2 date9.;
	days_datdif = datdif(date1, date2, 'act/act');
	days_intck = intck('day', date1, date2);
run;
ballardw
Super User

Is your value a numeric with the value of 170102. Or is it a date value of 20821 displayed with the SAS display format of yymmdd6.?

 

SAS data values which are required for any of the date functions to work are days offset from 1 Jan 1960. So if you date 170102 is supposed to be 2 Jan 2017 then the numeric value would have to be 20821.

 

BTW when working with SAS FORMAT has a very specific interpretation of a display, not the actual values.

 

If your value is numeric and ALL of them have 6 digits you can create a SAS date value for use with the date functions and date FORMATS with

 

newdatevar = input(put(datevar,6.),yymmdd6.);

in a data step where datevar is your existing date variable and newdatevar is the SAS date valued variable.

 

The appearance of the newdatevar would be set with a format, either at use or assign a default format at creation with a line like:

 

format newdatevar date9. ; <= displays date as 02JAN2017

format newdatevar mmddyy10. ; <= displays date as 01/02/2017 or any of many date format choices.

 

The SAS formats are very flexible in that you can use a different format in different procedures to create different groupings of your date variables without creating new variables.

 

 

A second hint: Using 2 digit years in this day an age is getting to be a poor practice. It is not obvious that 170102 is in 2017 or 1917.

And when the date in question is 010203 which is month, day and year is a complete guess.

DeclanBall
Fluorite | Level 6
Yes value was numeric.

I've used your suggestions of converting the values using:
newdatevar = input(put(datevar,6.),yymmdd6.);

and then I have simply taken the difference between the two.

Many thanks!
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Days between = end date - start date.

You don't need datdif or intck for this.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 4 replies
  • 1222 views
  • 2 likes
  • 4 in conversation