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

I have the code below:

data want;
input mydate1 :yymmdd10. mydate2 :yymmdd10.;
format mydate1 mydate2 yymmddd10.;
datalines;

2020-08-27 2020-10-22
2020-03-01 2020-04-26
;
run;

data want2;
set want;
diff_months=intck("month",mydate1,mydate2);
diff_days=intck("day",mydate1,mydate2);
run;
proc print;run;

which provides the dataset want2

tSAS1_0-1628531748815.png

 

but I don't know why I don't have same diff_months=1 for both rows, since we have the same diff_days !

 

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

In row one, the dates  cross two month boundaries. In row two, the dates cross one month boundary.

 

Perhaps you want to use the 'C' method of the INTCK function which then counts months from the starting date, rather than crossing a boundary.

 

diff_months=intck("month",mydate1,mydate2,'c');
--
Paige Miller

View solution in original post

2 REPLIES 2
PaigeMiller
Diamond | Level 26

In row one, the dates  cross two month boundaries. In row two, the dates cross one month boundary.

 

Perhaps you want to use the 'C' method of the INTCK function which then counts months from the starting date, rather than crossing a boundary.

 

diff_months=intck("month",mydate1,mydate2,'c');
--
Paige Miller
Reeza
Super User

Because INTCK counts the number of interval boundaries that are crossed, not a calculation of number of months which actually doesn't have a unique definition. Would a month be like February, 28 or 29 days or like December with 31 days?  A 'month' doesn't have a standard definition so it's not a great unit of measure. 

 

From the doc:

Returns the number of interval boundaries of a given kind that lie between two dates, times, or datetime values.

 

So your first record starts in Month = 8 and ends in Month = 10 which is two boundaries crossed, 9 and 10.

In your second record, Month =3and ends in Month = 4 which is a single boundary crossed.

 

Some references for further details:

https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/lefunctionsref/p1md4mx2crzfaqn14va8kt7qvfhr.h...

 

https://blogs.sas.com/content/iml/2017/05/15/intck-intnx-intervals-sas.html

 

Here's a great, but longer and in depth, reference for dates and times in SAS
https://communities.sas.com/t5/SAS-Communities-Library/Working-with-Dates-and-Times-in-SAS-Tutorial/...

 


@tSAS1 wrote:

I have the code below:

data want;
input mydate1 :yymmdd10. mydate2 :yymmdd10.;
format mydate1 mydate2 yymmddd10.;
datalines;

2020-08-27 2020-10-22
2020-03-01 2020-04-26
;
run;

data want2;
set want;
diff_months=intck("month",mydate1,mydate2);
diff_days=intck("day",mydate1,mydate2);
run;
proc print;run;

which provides the dataset want2

tSAS1_0-1628531748815.png

 

but I don't know why I don't have same diff_months=1 for both rows, since we have the same diff_days !

 


 

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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