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

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.

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

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     

View solution in original post

8 REPLIES 8
Haydn
Quartz | Level 8

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

 

TomKari
Onyx | Level 15

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

ShenQicheng
Obsidian | Level 7

That code worked for me.

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

 

Patrick
Opal | Level 21

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;

 

Reeza
Super User

@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?

Kurt_Bremser
Super User

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     
Haydn
Quartz | Level 8

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

 

Cheers

Haydn

kanivan51
Obsidian | Level 7

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.

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 8 replies
  • 4435 views
  • 3 likes
  • 7 in conversation