Hello Everyone,
I have created a small utility that can resolve the start and end of every month. The code is running fine all the macro variable are resolving as they should have, but the issue is when I am using those macro variables later in my programming then they are not getting resolved.
Kindly find attached the code...
OPTIONS MLOGIC MPRINT SYMBOLGEN;
%MACRO MONTHLY;
%DO J=0 %TO 12;
%LET MONSTRT=STRT;
%LET MONEND=END;
DATA _NULL_;
CALL SYMPUT ("&MONSTRT.&J.",PUT(INTNX("MONTH",TODAY(),&J,"B"),DATE9.));
CALL SYMPUT ("&MONEND.&J.",PUT(INTNX("MONTH",TODAY(),&J,"E"),DATE9.));
RUN;
%END;
%PUT &STRT0 &END0 &STRT1 &END1 &STRT2 &END2 &STRT3 &END4 &STRT5 &END5 &STRT6 &END6
&STRT7 &END7 &STRT8 &END8 &STRT9 &END9 &STRT10 &END10 &STRT11 &END11 &STRT12 &END12;
%MEND MONTHLY;
%MONTHLY;
DATA TEST;
XX='&STRT0.';
RUN;
You made LOCAL macro variables, unless there is some earlier code that created STRT0 before you called the macro. You can fix that by not using the legacy CALL SYMPUT() function and instead use the more modern CALL SYMPUTX() function that allows you to make the macro variables in the GLOBAL macro variable scope.
Also the macro processor does not operate on text inside of single quotes.
No need for the %DO loop since you are already running a data step. Just use a regular DO loop instead.
%macro monthly;
data _null_;
do j=0 to 12 ;
call symputx(cats("&monstrt",j),put(intnx("month",today(),j,"b"),date9.),'g');
call symputx(cats("&monend",j),put(intnx("month",today(),j,"e"),date9.),'g');
end;
run;
%mend monthly;
%let monstrt=strt;
%let monend=end;
%monthly;
data test;
xx="&strt0.";
put xx=;
run;
If you did want to use %DO loop then get rid of the DATA step and use %SYSFUNC() instead.
%macro monthly;
%local j ;
%do j=0 %to 12 ;
%global &monstrt.&j &monend.&j ;
%let &monstrt.&j=%sysfunc(intnx(month,%sysfunc(today()),&j,b),date9.);
%let &monend.&j=%sysfunc(intnx(month,%sysfunc(today()),&j,e),date9.);
%end;
%mend monthly;
Hi @aashish_jain
I think the issue is due to the use of single quotes, which prevents the resolution of macrovariables.
XX='&STRT0.';
Please try double quotes:
XX="&STRT0.";
Best,
DATA TEST;
xx=resolve('&STRT0');
RUN;
You made LOCAL macro variables, unless there is some earlier code that created STRT0 before you called the macro. You can fix that by not using the legacy CALL SYMPUT() function and instead use the more modern CALL SYMPUTX() function that allows you to make the macro variables in the GLOBAL macro variable scope.
Also the macro processor does not operate on text inside of single quotes.
No need for the %DO loop since you are already running a data step. Just use a regular DO loop instead.
%macro monthly;
data _null_;
do j=0 to 12 ;
call symputx(cats("&monstrt",j),put(intnx("month",today(),j,"b"),date9.),'g');
call symputx(cats("&monend",j),put(intnx("month",today(),j,"e"),date9.),'g');
end;
run;
%mend monthly;
%let monstrt=strt;
%let monend=end;
%monthly;
data test;
xx="&strt0.";
put xx=;
run;
If you did want to use %DO loop then get rid of the DATA step and use %SYSFUNC() instead.
%macro monthly;
%local j ;
%do j=0 %to 12 ;
%global &monstrt.&j &monend.&j ;
%let &monstrt.&j=%sysfunc(intnx(month,%sysfunc(today()),&j,b),date9.);
%let &monend.&j=%sysfunc(intnx(month,%sysfunc(today()),&j,e),date9.);
%end;
%mend monthly;
Thank you for the quick reply the suggestion you wrote works for me.
Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!
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.
Ready to level-up your skills? Choose your own adventure.