Iam trying to get the next month of a date.
%let date =2017-01-12;
%let et=%sysfunc(intnx(month,&date.,1),yymmdd10.);
%put &et.;
But result is different. What went wrong
1 OPTIONS NONOTES NOSTIMER NOSOURCE NOSYNTAXCHECK;
61
62 %put &et.;
1965-07-01
63
64 OPTIONS NONOTES NOSTIMER NOSOURCE NOSYNTAXCHECK;
77
Try this
%let date =2017-01-12;
%let et=%sysfunc(intnx(month,%sysfunc(inputn(&date.,yymmdd10.)),1),yymmdd10.);
%put &et.;
For same dates instead of 1st of month-->
%let date =2017-01-12;
%let et=%sysfunc(intnx(month,%sysfunc(inputn(&date.,yymmdd10.)),1,s),yymmdd10.);
%put &et.;
SAS dates need to be specified as date literals to be automatically recognized as dates.
How is the compiler supposed to know if you want 2017-01-12 = 2017-1-12 = 2017-13=2004
or that it's a date?
The way to do that is specify dates in a date literal format:
%let date = '12Jan2017'd;
%let et=%sysfunc(intnx(month,&date.,1),yymmdd10.);
%put &et.;
This works. If you want to specify the date in a different format, then YOU need to handle the conversion, which is demonstrated in @novinosrin code using the INPUTN() function. (INPUT() does not work with %SYSFUNC, but INPUTN() does)
2017-01-12 used as a date parameter will be evaluated as 2004 (2017 minus 1 minus 12), which means 2004 days after 01jan1960.
Use a proper informat as @novinosrin suggests.
Hint: macro variables to be used as dates should contain raw date values (5-digit numbers as of now); human-readable form is not needed. Makes everything easier.
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.