Search code examples
sassas-macro

passing date variable to macro for sysfunc processing


below is my little problem to create a macro and passing in a date variable. Without using the date variable, it works with results as below.

%macro x();
%let i=-1;
%let dts = %sysfunc(today());
%put &dts; /*ok*/
%let yymm1 = %sysfunc(intnx(MONTH,&dts,&i));
%put &yymm1; /*ok*/

%let mth_beg = %sysfunc(intnx(MONTH,&dts,&i,B),date9.);
%let mth_end = %sysfunc(intnx(MONTH,&dts,&i,E),date9.);
%put &mth_beg &mth_end; /*01JAN2018 31JAN2018*/
/*** proc sql code below ** */
%mend;
%x();

log: 21231 21185 01JAN2018 31JAN2018

Now I create a macro around it and got the following error:

%macro x(dts1);

%let i=-1;
/*%let dts = %sysfunc(today());*/
%let dts = %sysfunc(&dts1);
%put &dts; /*ok*/
%let yymm1 = %sysfunc(intnx(MONTH,&dts,&i));
%put &yymm1; /*ok*/

%let mth_beg = %sysfunc(intnx(MONTH,&dts,&i,B),date9.);
%let mth_end = %sysfunc(intnx(MONTH,&dts,&i,E),date9.);
%put &mth_beg &mth_end; /*01JAN2018 31JAN2018*/
/*** proc sql code below ** */
%mend;
%x(16JAN2018);

ERROR: Function name missing in %SYSFUNC or %QSYSFUNC macro function reference. JAN2018) ERROR: Expected close parenthesis after macro function invocation not found. )) ERROR: Expected close parenthesis after macro function invocation not found. ERROR: Expected close parenthesis after macro function invocation not found. ,B),date9.) ,E),date9.)

I am not sure how to let SAS treat the date passed in as a recognized date. I know i probably used the sysfunc(&dts) wrongly or the date passed in need to adhere to certain format. i just want the date to replace today(). Can you help? I am a SAS newbie.

thanks


Solution

  • Wrap the date in " and end with a d. That will tell SAS to convert the string to a date:

    %macro x(dts1);
    
    %let i=-1;
    /*%let dts = %sysfunc(today());*/
    %let dts = "&dts1"d; /*Change here!*/
    %put &dts; /*ok*/
    %let yymm1 = %sysfunc(intnx(MONTH,&dts,&i));
    %put &yymm1; /*ok*/
    
    %let mth_beg = %sysfunc(intnx(MONTH,&dts,&i,B),date9.);
    %let mth_end = %sysfunc(intnx(MONTH,&dts,&i,E),date9.);
    %put &mth_beg &mth_end; /*01JAN2018 31JAN2018*/
    /*** proc sql code below ** */
    %mend;
    %x(16JAN2018);