0
votes

I am trying to create last date of the month using sas macro. I set year-month yyyymm and would like to get last date of the month

%LET YYYYMM = 201501;
%LET SASDATE = %EVAL(%SYSFUNC(INTNX('MONTH',%SYSFUNC(MDY(%SYSFUNC(MOD(&YYYYMM,100)),1,%SYSFUNC(ROUND(%EVAL(&YYYYMM/100))))),1))-1);
%PUT &SASDATE;

I am getting the following error in the log file. which I don't understand

24         %LET YYYYMM = 201501;
25         %LET SASDATE =
25       ! %EVAL(%SYSFUNC(INTNX('MONTH',%SYSFUNC(MDY(%SYSFUNC(MOD(&YYYYMM,100)),1,%SYSFUNC(ROUND(%EVAL(&YYYYMM/100))))),1))-1);
WARNING: Argument 1 to function INTNX 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.
ERROR: A character operand was found in the %EVAL function or %IF condition where a numeric operand is required. The condition was: 
       .-1 

Can you please help? Thanks.

2

2 Answers

3
votes

You can (and should!) use date formats and informats to deal with this sort of thing rather than doing your own date calculations, e.g.

%LET YYYYMM = 201501;
%put END_OF_MONTH = %sysfunc(intnx(month,%sysfunc(inputn(&YYYYMM,yymmn6.)),0,e),yymmdd10.);

Also, note that everything is text in the macro language, so you don't need to wrap text in quotes when using %sysfunc.

1
votes

%sysfunc invoked functions do not require string literal arguments to be quoted. Remove the single quotes from 'MONTH'.

%let yyyymm = 201501;
%let yyyymm_date = %sysfunc(INPUTN(&YYYYMM,YYMMN6.));
%let next_month1 = %sysfunc (INTNX(MONTH,&YYYYMM_DATE,1));
%let want_yyyymm = %eval (&NEXT_MONTH1-1);
%let date9 = %sysfunc(putn(&want_yyyymm,date9.));
%let date9_literal = "&date9"d;

%put &=yyyymm;
%put &=yyyymm_date;
%put &=next_month1;
%put &=want_yyyymm;
%put &=date9;
%put &=date9_literal;

----- LOG -----

YYYYMM=201501
YYYYMM_DATE=20089
NEXT_MONTH1=20120
WANT_YYYYMM=20119
DATE9=31JAN2015
DATE9_LITERAL="31JAN2015"d

Example use of the macro variables

title "Report for month ending &date9.";

proc print data=have;
  where date <= &date9_literal;
  ...
run;