Search code examples
datesassas-macro

Unable to convert argument to date format


So I have a macro here which takes sas month in yymmn6 format to compute the next 2 months.

%macro pull(yymm);
%let month1=%sysfunc(inputn(&yymm.,yymmn6.));
%let month2=%sysfunc(inputn(intnx('month',&month1.,1),yymmn6.));
%let month3=%sysfunc(inputn(intnx('month',&month1.,2),yymmn6.));
%put &month1 &month2 &month3;
%mend;
%pull(201807);

I'm not being able to understand the error in the code because I get the following warning:

Argument 1 to function INPUTN referenced by the %SYSFUNC or %QSYSFUNC macro function is out of range.

When I check the log, &month1 comes as 21366 which should ideally have been 201807. Can somebody please help me out here?


Solution

  • Every function needs its own %SYFUNC() wrapper so your code is passing the literal string INTNX( to INPUTN() function. Also you don't need to add quotes around string values in macro code. So remove the quotes around MONTH.

    So first convert your YYYYMM string into a date. Then pass that date value to the INTNX() function. You can use the optional second parameter of the %SYSFUNC() function to specify what format to use when converting the function result into a string.

    %macro pull(yymm);
    %local date;
    %let date=%sysfunc(inputn(&yymm,yymmn6));
    %let month1=%sysfunc(intnx(month,&date,0),yymmn6.);
    %let month2=%sysfunc(intnx(month,&date,1),yymmn6.);
    %let month3=%sysfunc(intnx(month,&date,2),yymmn6.);
    %put &month1 &month2 &month3;
    %mend;
    %pull(201807);
    

    Results:

    201807 201808 201809