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

I have the below code:

Options symbolgen mlogic mfile nomprint;

%macro bymonth;

data _null_;

%do i = - &num_month %to 0;

call symputx('bymonth',put(intnx('month',"&date9"d,&i,'e'),date9.),'g');

;

%put &bymonth;

%end;

run;

%mend;

%bymonth;

I'm starting at - &num_month which is 5, date9 resolves to 16JUN2015.  My intent is to use the intnx to count back from -5 to 0 which will pull the complete year of data.  Date9 and i are resolving as I intend but &bymonth isn't changing with each iteration.

Any guidance/help will be appreciated.  I'll paste some log below for your reading pleasure:

MLOGIC(BYMONTH):  Beginning execution.

SYMBOLGEN:  Macro variable NUM_MONTH resolves to 05

MLOGIC(BYMONTH):  %DO loop beginning; index variable I; start value is -5; stop value is 0; by value is 1. 

SYMBOLGEN:  Macro variable DATE9 resolves to 16JUN2015

SYMBOLGEN:  Macro variable I resolves to -5

MLOGIC(BYMONTH):  %PUT &bymonth

SYMBOLGEN:  Macro variable BYMONTH resolves to 30JUN2015

30JUN2015

MLOGIC(BYMONTH):  %DO loop index variable I is now -4; loop will iterate again.

SYMBOLGEN:  Macro variable DATE9 resolves to 16JUN2015

SYMBOLGEN:  Macro variable I resolves to -4

MLOGIC(BYMONTH):  %PUT &bymonth

SYMBOLGEN:  Macro variable BYMONTH resolves to 30JUN2015

30JUN2015

MLOGIC(BYMONTH):  %DO loop index variable I is now -3; loop will iterate again.

SYMBOLGEN:  Macro variable DATE9 resolves to 16JUN2015

SYMBOLGEN:  Macro variable I resolves to -3

MLOGIC(BYMONTH):  %PUT &bymonth

SYMBOLGEN:  Macro variable BYMONTH resolves to 30JUN2015

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

Mark,

Another key piece of information:  these notes do not represent the first time you tried the macro.  The first time, you got messages about &BYMONTH not being resolved.

The basic problem is that %PUT is not part of a DATA step, but CALL SYMPUTX only executes when the DATA step runs.  So the first time this ran, the order of execution of your statements was:

%PUT

%PUT

%PUT

%PUT

%PUT

%PUT

data _null_;

   call symputx

   call symputx

   call symputx

   call symputx

   call symputx

   call symputx

run;

In fact, that's the order of execution every time, not just the first time.  But after the DATA step has run once, &BYMONTH exists and the %PUT statements can write it out.  This link may help:

http://blogs.sas.com/content/publishing/2015/04/01/sas-authors-tip-macro-language-timing-is-everythi...

Good luck.

View solution in original post

3 REPLIES 3
Astounding
PROC Star

Mark,

Another key piece of information:  these notes do not represent the first time you tried the macro.  The first time, you got messages about &BYMONTH not being resolved.

The basic problem is that %PUT is not part of a DATA step, but CALL SYMPUTX only executes when the DATA step runs.  So the first time this ran, the order of execution of your statements was:

%PUT

%PUT

%PUT

%PUT

%PUT

%PUT

data _null_;

   call symputx

   call symputx

   call symputx

   call symputx

   call symputx

   call symputx

run;

In fact, that's the order of execution every time, not just the first time.  But after the DATA step has run once, &BYMONTH exists and the %PUT statements can write it out.  This link may help:

http://blogs.sas.com/content/publishing/2015/04/01/sas-authors-tip-macro-language-timing-is-everythi...

Good luck.

Steelers_In_DC
Barite | Level 11

That did help.  I was getting the right answer but I wanted to see it before, I would have needed to make this update for the next step anyway.

thanks for pointing me in the right direction,

Mark

For anyone that searches for answers before asking the forum, here's the solution:

%macro bymonth;

%do i = &num_month %to 10;

data _null_;

call symputx('bymonth',put(intnx('month',"&date9"d,&i,'e'),date9.),'g');

run;

data test&i;

test = substr("&bymonth",3,3);

run;

%end;

%mend;

%bymonth;

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 3 replies
  • 1786 views
  • 0 likes
  • 3 in conversation