BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
NewUsrStat
Lapis Lazuli | Level 10

Hi guys, 

 

could you help me to calculate the number of days between two dates in the formats: 

 

  • 01/20/2024 -22/12/2025? N. of days between the dates:

 

  • 2024-11-13 - 2025-12-02? N. of days between the dates:

 

I usually format dates in the date9. format, but in this case, I cannot convert because of standards to be compliant with.

 

Thank you in advance.

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

Variables are not "in date9. format".  They are either numeric variables with date values (number of days since start of 1960) that have the DATE9. format specification attached to them.  Or they are character variables with strings in the style produced by the DATE9. format.

 

In SAS code FORMAT has a specific meaning. 

 

A FORMAT converts values to text.  An INFORMAT converts text to values.

 

So if your four variables are numeric with date values you can simply subtract them to find the difference in days, no matter what format specification has been attached to them.

data example;
  date1 = '20JAN2024'd;
  date2 = '22DEC2025'd ;
  date3 = '13NOV2024'd;
  date4 = '02DEC2025'd ;
  format date1 mmddyy10. date2 ddmmyy10. date3 date4 yymmdd10.;
  diff1 = date2 - date1;
  diff2 = date4 - date3;
run;

And if they are character then you can first convert them to dates using the INPUT() function and an appropriate informat.

date example2;
  date1='01/20/2024';
  date2='22/12/2025';
  date3 = '2024-11-13';
  date4 = '2025-12-02';
  diff1 = input(date2,ddmmyy10.) - input(date1,mmddyy10.);
  diff2 = input(date4,yymmdd10.) - input(date3,yymmdd10.);
run;

NOTE: if you have a single character variables where the style of the strings is different on different observations it will be harder. You might try using the ANYDTDTE informat.  But make sure that you do not have ambiguous strings.  There is no way to tell whether 10/12/2025 is supposed to mean the tenth of December or October twelfth without some external knowledge about whether the fields are in DMY or MDY order.

 

 

 

View solution in original post

3 REPLIES 3
PaigeMiller
Diamond | Level 26

01/20/2024 -22/12/2025? N. of days between the dates:

So you have one date that appears as MMDDYYYY and the other date appears as DDMMYYYY? Is that right? (A rather poor way to design a data set, in my opinion).

 

If your date variables are NUMERIC, then you just do a subtraction, the format differences are irrelevant. Are they numeric, according to PROC CONTENTS?

 

I usually format dates in the date9. format, but in this case, I cannot convert because of standards to be compliant with.

Dates are stored as either numeric or character values, they are not stored as formatted values. A format can be optionally assigned to the value, but if they are numeric, they are stored as the integer number of days since 01JAN1960, and so a simple subtraction would give you answer, regardless of the format. The format changes the appearance, it does not change the value. If they are character, you can certainly create new numeric variables (as described by @Kurt_Bremser ), and then you can do the subtraction, all without affecting the original variables.


Example with numeric variables:

 

data fake; /* Note: date1 and date2 are numeric */
    date1='20JAN2024'd;
    date2='22DEC2025'd;
    format date1 mmddyy10. date2 ddmmyy10.; /* Note different formats for the two variables */
run;
proc print data=fake;
run;
data compute_difference;
    set fake;
    difference = date2-date1;
run;
proc print data=compute_difference;
run;

 

--
Paige Miller
Tom
Super User Tom
Super User

Variables are not "in date9. format".  They are either numeric variables with date values (number of days since start of 1960) that have the DATE9. format specification attached to them.  Or they are character variables with strings in the style produced by the DATE9. format.

 

In SAS code FORMAT has a specific meaning. 

 

A FORMAT converts values to text.  An INFORMAT converts text to values.

 

So if your four variables are numeric with date values you can simply subtract them to find the difference in days, no matter what format specification has been attached to them.

data example;
  date1 = '20JAN2024'd;
  date2 = '22DEC2025'd ;
  date3 = '13NOV2024'd;
  date4 = '02DEC2025'd ;
  format date1 mmddyy10. date2 ddmmyy10. date3 date4 yymmdd10.;
  diff1 = date2 - date1;
  diff2 = date4 - date3;
run;

And if they are character then you can first convert them to dates using the INPUT() function and an appropriate informat.

date example2;
  date1='01/20/2024';
  date2='22/12/2025';
  date3 = '2024-11-13';
  date4 = '2025-12-02';
  diff1 = input(date2,ddmmyy10.) - input(date1,mmddyy10.);
  diff2 = input(date4,yymmdd10.) - input(date3,yymmdd10.);
run;

NOTE: if you have a single character variables where the style of the strings is different on different observations it will be harder. You might try using the ANYDTDTE informat.  But make sure that you do not have ambiguous strings.  There is no way to tell whether 10/12/2025 is supposed to mean the tenth of December or October twelfth without some external knowledge about whether the fields are in DMY or MDY order.

 

 

 

Catch up on SAS Innovate 2026

Nearly 200 sessions are now available on demand in the Innovate Hub.

Watch Now →
Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 3 replies
  • 570 views
  • 5 likes
  • 4 in conversation