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
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.
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;
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.
Days between = end date - start date.
You don't need datdif or intck for this.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.