Desktop productivity for business analysts and programmers

return number of months between 2 dates

Accepted Solution Solved
Reply
Contributor
Posts: 47
Accepted Solution

return number of months between 2 dates

[ Edited ]

I would like to count the number of months between 2 dates. Dates are coded as below

 

%let StartDate = "01Jul2016"d;

%let ExtractDateII = "17Oct2016"d;

 

The StartDate for this financial year will always be 1st of July 2016. The extract date will generally be around the 15th of a month.

 

The result I'm after using the above two dates would be 3. July 2016, August 2016 and September 2016.

 

 


Accepted Solutions
Solution
‎10-20-2016 06:13 PM
Super User
Posts: 7,465

Re: return number of months between 2 dates

Make sure that variables startdate and extractdateii are SAS date values (numeric, counting number of days since 01/01/1960, having a date format).

 

Because this works:

data test;
startdate = '01Jul2016'd;
extractdateii = '30oct2016'd;
format startdate extractdateii date9.;
targetmonths = intck('month',startdate,extractdateii);
run;
proc print;run;

Result:

Obs    startdate    extractdateii    targetmonths

 1     01JUL2016      30OCT2016            3     
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers

View solution in original post


All Replies
Contributor
Posts: 47

Re: return number of months between 2 dates

I can get this to work 

TargetMonths=intck('month','01Jul2016'd,'30oct2016'd);

 

which returns me 3.

 

But this does not work

 

TargetMonths=intck('month',StartDate,ExtractDateII);

 

I gues I missing something obvious

 

PROC Star
Posts: 1,146

Re: return number of months between 2 dates

One thing that's a possibility is that the numbers in your variables are DateTime quantities, not Date quantities. It's a bit odd in SAS, but they are very different.

 

Tom

Contributor
Posts: 29

Re: return number of months between 2 dates

That code worked for me.

TargetMonths=intck('month',&StartDate,&ExtractDateII);

 

Respected Advisor
Posts: 4,138

Re: return number of months between 2 dates

[ Edited ]

Just expressing in code what @TomKari wrote. 

data sample;

  format start_dt end_dt date9.;
  format start_dttm end_dttm datetime21.;

  start_dt='01Jul2016'd;
  end_dt='30oct2016'd;
  start_dttm='01Jul2016:00:00:00'dt;
  end_dttm='30oct2016:23:59:59'dt;

  TargetMonths_dt   =intck('month',start_dt,end_dt);
  TargetMonths_dttm =intck('dtmonth',start_dttm,end_dttm);

run;

 

Super User
Posts: 19,194

Re: return number of months between 2 dates


Haydn wrote:

I can get this to work 

 

But this does not work

 

TargetMonths=intck('month',StartDate,ExtractDateII);

 

 


What does this mean? How does it not work, do you get missing or a different number? Is there an error in the log?

Solution
‎10-20-2016 06:13 PM
Super User
Posts: 7,465

Re: return number of months between 2 dates

Make sure that variables startdate and extractdateii are SAS date values (numeric, counting number of days since 01/01/1960, having a date format).

 

Because this works:

data test;
startdate = '01Jul2016'd;
extractdateii = '30oct2016'd;
format startdate extractdateii date9.;
targetmonths = intck('month',startdate,extractdateii);
run;
proc print;run;

Result:

Obs    startdate    extractdateii    targetmonths

 1     01JUL2016      30OCT2016            3     
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Contributor
Posts: 47

Re: return number of months between 2 dates

Thanks to everyone who had a look at this for me and thanks KurtBremser, it was to do with the date values.

 

Cheers

Haydn

☑ This topic is solved.

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

Discussion stats
  • 7 replies
  • 633 views
  • 3 likes
  • 6 in conversation