Can anyone please provide help me get right answer
%let date = 2012-11;
%let mydate = %sysfunc(putn(&date, YYMMD7.));
%let me = %sysfunc(intnx(month,&date,-1),yymmd7.);
%put &date;
%put &mydate;
%put &me;
My Answers:
%put &date;
2012-11
%put &mydate;
1965-06
%put &me;
1965-05
I want Answers:
%put &date;
2012-11
%put &mydate;
2012-11
%put &me;
2012-10
The 2012-11 is in a date format that SAS doesn't recognize (or at least an informat that I coudn't find).
If you remove the hyphen, then it is informat, yymmn6. So I compress out the hyphen, convert it to a date.
You were also referencing the incorrect the macro variable in your last macro variable (me), date rather than mydate.
%let date = 2012-11;
%let mydate = %sysfunc(inputn(%sysfunc(compress(&date, "-")), yymmn6.));
%put &mydate;
%let me = %sysfunc(intnx(month,&mydate,-1),yymmd7.);
%put &me;
There will be many ways to do this, but here is one suggestion.
%let date = 2012-11;
/* we could use in informat or break out the separate date components, as below */
%let y=%substr(%left(&date),1,4);
%let m=%substr(%left(&date),6,2);
%put y=&y m=&m;
/* create a numerical version of the date */
%let mydateN = %sysfunc(mdy(&m.,1,&y.));
/* create a Character (formated) version of the date */
%let mydateF= %sysfunc(putn(&mydateN.,yymmd7.));
/* decrease date and create a new Character (formated) version of the date */
%let me = %sysfunc(intnx(month,&mydateN.,-1),yymmd7.);
%put &date;
%put MyDate (Numerical) &mydateN;
%put MyDate (Formated) &mydateF;
%put &me;
Hi.
I think the main problem is in the first assignment of date:
%let date = 2012-11;
While the assumption is that SAS will know what you mean, it is hard without telling SAS that '2012-11' is actually a date, it will not just guess that.
OK, the more straightforward explanation is that SAS will not just assume that the list of characters: 2012-11 means a date. Unless YOU tell it so.
In a %let statement you can have anything: %let date = this is a date, so that means you cannot simply apply a specific date format to a string and expect SAS to know what you mean.
So, again, %let date = 2012-11; translates to a string that does not have the meaning you intended (date). It is just a string of numbers.
So,
%let date = 2012-11; needs to become something like
%let date = '01Nov2012'd;
%put &date;
%let mydate = %sysfunc(putn(&date, yymmd7.));
%put &mydate.;
%let me = %sysfunc(intnx(Month,&date., -1),yymmd7.);
%put &me.;
I hope this clarifies a bit why you are not getting the expected answers.
Good luck,
Anca
The 2012-11 is in a date format that SAS doesn't recognize (or at least an informat that I coudn't find).
If you remove the hyphen, then it is informat, yymmn6. So I compress out the hyphen, convert it to a date.
You were also referencing the incorrect the macro variable in your last macro variable (me), date rather than mydate.
%let date = 2012-11;
%let mydate = %sysfunc(inputn(%sysfunc(compress(&date, "-")), yymmn6.));
%put &mydate;
%let me = %sysfunc(intnx(month,&mydate,-1),yymmd7.);
%put &me;
Thank you all for your feedback. I've a working code now.
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.
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.