DATA Step, Macro, Functions and more

DATDIF Function - Format input help

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 15
Accepted Solution

DATDIF Function - Format input help

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


Accepted Solutions
Solution
‎12-08-2017 10:54 AM
Super User
Posts: 13,321

Re: DATDIF Function - Format input help

Posted in reply to DeclanBall

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


All Replies
PROC Star
Posts: 1,215

Re: DATDIF Function - Format input help

Posted in reply to DeclanBall

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 Smiley Happy

 

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;
Solution
‎12-08-2017 10:54 AM
Super User
Posts: 13,321

Re: DATDIF Function - Format input help

Posted in reply to DeclanBall

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.

Occasional Contributor
Posts: 15

Re: DATDIF Function - Format input help

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!
Super User
Super User
Posts: 9,416

Re: DATDIF Function - Format input help

Posted in reply to DeclanBall

Days between = end date - start date.

You don't need datdif or intck for this.

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 4 replies
  • 156 views
  • 2 likes
  • 4 in conversation