DATA Step, Macro, Functions and more

do loop symputx macro variable not resolving as desired

Accepted Solution Solved
Reply
Valued Guide
Posts: 860
Accepted Solution

do loop symputx macro variable not resolving as desired

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


Accepted Solutions
Solution
‎07-01-2015 10:13 AM
Super User
Posts: 5,513

Re: do loop symputx macro variable not resolving as desired

Posted in reply to Steelers_In_DC

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


All Replies
Solution
‎07-01-2015 10:13 AM
Super User
Posts: 5,513

Re: do loop symputx macro variable not resolving as desired

Posted in reply to Steelers_In_DC

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.

Valued Guide
Posts: 860

Re: do loop symputx macro variable not resolving as desired

Posted in reply to Astounding

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;

Regular Contributor
Posts: 227

Re: do loop symputx macro variable not resolving as desired

Posted in reply to Steelers_In_DC
🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 3 replies
  • 499 views
  • 0 likes
  • 3 in conversation