BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
aashish_jain
Obsidian | Level 7

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;

 

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

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;

 

View solution in original post

5 REPLIES 5
ed_sas_member
Meteorite | Level 14

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,

Astounding
PROC Star
Double quotes instead of single quotes is half the solution.

By the time you try to use the macro variables, they no longer exist. They are local to the %monthly symbol table. You will need to add this statement after the two %LET statements:

%global &monstrt&j &monend&j;
ghosh
Barite | Level 11
DATA TEST;
xx=resolve('&STRT0');
RUN;
Tom
Super User Tom
Super User

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;

 

aashish_jain
Obsidian | Level 7

Thank you for the quick reply the suggestion you wrote works for me.

 

 

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

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!

Register Now

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 5 replies
  • 1989 views
  • 1 like
  • 5 in conversation