Solved
Contributor
Posts: 48

# 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: 9,559

## 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
How to convert datasets to data steps
How to post code

All Replies
Contributor
Posts: 48

## 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,262

## 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: 37

## Re: return number of months between 2 dates

That code worked for me.

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

Posts: 4,541

## 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: 22,823

## 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: 9,559

## 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
How to convert datasets to data steps
How to post code
Contributor
Posts: 48

## 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

Occasional Contributor
Posts: 18

## Re: return number of months between 2 dates

At first you are creating the macrovars:

``````%let StartDate = "01Jul2016"d;
%let ExtractDateII = "17Oct2016"d;``````

Then you want to use them:

``````data cnt_mth;
count_month=intck('month',&StartDate.,&ExtractDateII.);
run;
proc print;run;``````

The symbol "&" is necessary to add before macrovar.

☑ This topic is solved.