Hi All,
I am trying to convert a macro variable with the following value
30Nov2019
into
20191130
However, I am getting the error of :
ERROR: Argument 1 to function PUTN referenced by the %SYSFUNC or %QSYSFUNC macro function is not a number.
ERROR: Invalid arguments detected in %SYSCALL, %SYSFUNC, or %QSYSFUNC argument list. Execution of %SYSCALL statement or %SYSFUNC
or %QSYSFUNC function reference is terminated.
Below is my script
%let rpt_date=30Nov2021;
%let rpt_date2=%sysfunc(put(&rpt_date.,yymmddn8.));
%put rpt_date2 is &rpt_date2.;
As mentioned above, what I want to achieve is to convert rpt_date into a macro variable (rpt_date2) that shows character value in YYYYMMDD. However, I am getting error.
What could possible be wrong when converting the function that works in a datastep into a macro variable?
You cannot use the PUT() function with %SYSFUNC(). It is too confusing to %SYSFUNC() to figure out if the value is numeric or character, so you need to tell %SYSFUNC() what type of values to expect by choosing to use either PUTN() or PUTC().
Since the YYMMDDN format is a NUMBERIC format you need to use the PUTN() function.
But the PUTN() function operates on NUMBERS. You passed it a string with letters in it.
If you want SAS to consider that string as a date you need to convert it to a date literal by adding quotes and the letter D.
1 %let rpt_date=30Nov2021;
2 %let rpt_date2=%sysfunc(putn("&rpt_date"d,yymmddn8.));
3 %put &=rpt_date &=rpt_date2;
RPT_DATE=30Nov2021 RPT_DATE2=20211130
%let rpt_date2 = %sysfunc(inputn("&rpt_date."d,date9.),yymmddn8.);
Try this.
I am getting this:
116 %put rpt_date is what i dont want &rpt_date.;
SYMBOLGEN: Macro variable RPT_DATE resolves to 31DEC2019
rpt_date is what i dont want 31DEC2019
117 %let rpt_date2 = %sysfunc(inputn("&rpt_date."d,date9.),yymmddn8.);
SYMBOLGEN: Macro variable RPT_DATE resolves to 31DEC2019
WARNING: Argument 1 to function INPUTN referenced by the %SYSFUNC or %QSYSFUNC macro function is out of range.
NOTE: Mathematical operations could not be performed during %SYSFUNC function execution. The result of the operations have been set
to a missing value.
118 %put rpt_date2 is what i want &rpt_date2.;
SYMBOLGEN: Macro variable RPT_DATE2 resolves to .
rpt_date2 is what i want .
What could possibly be the issue here?
My bad (posted before sufficient amount of caffeine).
No date literal needed:
%let rpt_date2 = %sysfunc(inputn(&rpt_date.,date9.),yymmddn8.);
See this log:
69 %let rpt_date = 31dec2019; 70 71 %let rpt_date2 = %sysfunc(inputn(&rpt_date.,date9.),yymmddn8.); 72 73 %put &=rpt_date2.; RPT_DATE2=20191231
You cannot use the PUT() function with %SYSFUNC(). It is too confusing to %SYSFUNC() to figure out if the value is numeric or character, so you need to tell %SYSFUNC() what type of values to expect by choosing to use either PUTN() or PUTC().
Since the YYMMDDN format is a NUMBERIC format you need to use the PUTN() function.
But the PUTN() function operates on NUMBERS. You passed it a string with letters in it.
If you want SAS to consider that string as a date you need to convert it to a date literal by adding quotes and the letter D.
1 %let rpt_date=30Nov2021;
2 %let rpt_date2=%sysfunc(putn("&rpt_date"d,yymmddn8.));
3 %put &=rpt_date &=rpt_date2;
RPT_DATE=30Nov2021 RPT_DATE2=20211130
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.