I want to be able to find the number of months between 2 dates.
The first date will always be 30 June 2014. My end date will always be the last day of the previous month. I want to then be able to use that number in other parts of code.
I.E. Between 30th June 2014 and 31st August 2020 there are 74 months. I want to then be able to call on that 74 in a later For loop.
I have tried something like this from using another code but I can't get it to work.
data _null_;
call symput('month_end', put(intnx('month',today(),0)-1,date9.));
call symput('start_date', put(intnx('month','30Jun14'd,0),date9.));
RUN;
%put &month_end.;
%put &Start_date.;
data a;
NumberOfMonths = INTCK('MONTH', '30Jun14'd, &month_end., 'D');
RUN;
First, you don't need macro variables to do this.
data a;
NumberOfMonths = INTCK('MONTH', '30Jun2014'd, intnx('month',today(),0,'e'), 'D');
RUN;
Also, if you are going to create macro variables, DO NOT format them. (Maxim 28)
data _null_;
call symputx('month_end',intnx('month',today(),0)-1);
call symputx('start_date','30Jun2014'd);
RUN;
data a;
NumberOfMonths = INTCK('MONTH', &start_date, &month_end, 'D');
RUN;
But as I said, in this simple example, macro variables are not needed, nevertheless the advice to NOT format macro variables is a general rule you should follow, with the exception of when the macro variables are going to be used in titles or labels.
Lastly, this part of your code
intnx('month',today(),0)-1
calculates the day one day previous to today (but yet you call in MONTH_END), is that what you want?
First, you don't need macro variables to do this.
data a;
NumberOfMonths = INTCK('MONTH', '30Jun2014'd, intnx('month',today(),0,'e'), 'D');
RUN;
Also, if you are going to create macro variables, DO NOT format them. (Maxim 28)
data _null_;
call symputx('month_end',intnx('month',today(),0)-1);
call symputx('start_date','30Jun2014'd);
RUN;
data a;
NumberOfMonths = INTCK('MONTH', &start_date, &month_end, 'D');
RUN;
But as I said, in this simple example, macro variables are not needed, nevertheless the advice to NOT format macro variables is a general rule you should follow, with the exception of when the macro variables are going to be used in titles or labels.
Lastly, this part of your code
intnx('month',today(),0)-1
calculates the day one day previous to today (but yet you call in MONTH_END), is that what you want?
Thank you so much for this.
I've been put on a project in SAS which is quite intermediate but I am a novice but learning as I'm going along.
There was some older code written by someone before me that included macro variables so thought I would adapt that.
Also, I didn't mean to calculate 1 day before today, that was my error. I was trying to get the last day of the previous month.
I have a follow-up question, if I use NumberOfMonths, in a For loop, will it work?
What I am actually trying to get is a list, which has the last day of every month from 30th June 2014 to the last day of the previous month.
Could you help me with that?
What I am actually trying to get is a list, which has the last day of every month from 30th June 2014 to the last day of the previous month. Could you help me with that?
data want;
date='30jun2014'd;
enddate=today();
do while (date<enddate);
output;
date=intnx('month',date,1,'e');
end;
keep date;
format date yymmdd10.;
run;
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.