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.
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
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
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
That code worked for me.
TargetMonths=intck('month',&StartDate,&ExtractDateII);
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;
@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?
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
Thanks to everyone who had a look at this for me and thanks KurtBremser, it was to do with the date values.
Cheers
Haydn
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.
Available on demand!
Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.
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.