DATA Step, Macro, Functions and more

Not able to resolve macro within %do loop.

Accepted Solution Solved
Reply
Contributor
Posts: 36
Accepted Solution

Not able to resolve macro within %do loop.

Hi all,

Can anyone tell me where i am going wrong? I tried to rectify the mistake but i am not able to do so.

options symbolgen mlogic;

%macro scp;

%global lob1 lob2 b;

%let lob1 = XAB;

%let lob2 = BAS;

%do l = 1 %to 2;

%let b = &&lob&l;

%macro report();

ods listing close;

ods html file = "/rpt6_&b_..xls";

data empty4;

msg = "welcome";

run;

%mend report;

%report

%end;

%mend scp;

%scp

Not able to resolve macro variable b for the first iteration and for the second iteration it is showing the value of lob2 which is BAS. I sahould have value XAB for first iteration and BAS for second iteration and i am not able to resolve macro variable b for the first iteration.

Thank you,

Harish.


Accepted Solutions
Solution
‎05-24-2012 02:59 PM
Super User
Posts: 5,071

Re: Not able to resolve macro within %do loop.

Harish,

Here's my take on what happened.  First, on your diagnosis, we can't see the messages you got and I would reserve the right to change my mind if I saw them.  I don't believe that &B couldn't be resolved the first time (unless you have messages to the contrary).  Rather, here's what I suspect.

When you define the macro like this:

%macro report();

SAS allows that there might be parameters entered. Invoking the macro like this:

%report

isn't enough to trigger the macro to execute.  SAS waits, thinking that you might still enter parameters in parentheses.  That's the result after one iteration through the %DO loop.  Then the %DO loop continues with &i=2, resetting &B and redefining %REPORT.

Finally, SAS encounters the second invocation of %REPORT.  That's enough for SAS to recognize that the first invocation is complete, and no parameters will be forthcoming.  So the first time %REPORT actually executes is at the end of the second iteration of the %DO loop, when &B has already been changed to BAS.  And the second execution of %REPORT hangs (or eventually runs with &B again equal to BAS), awaiting either parameters or some other trigger that tells SAS that there won't be any parameters.

Good luck.

View solution in original post


All Replies
Contributor
Posts: 36

Re: Not able to resolve macro within %do loop.


Got solved but i don't know the reason :

Changed %macro report(); to %macro report; and it worked.

Thank you,

Harish.

Super User
Super User
Posts: 6,495

Re: Not able to resolve macro within %do loop.

You are defining the macro variable B and referencing the macro variable B_. 

Did you mean to use rpt6_&b._.xls as the filename? 

Do you really want the extra underscore?

Also do you really want to write to the root directory of your file system by starting the filename with a slash?

Super User
Posts: 10,466

Re: Not able to resolve macro within %do loop.

I don't see any reason to define the REPORT macro within the SCP macro. You would probably be better off making in a separate definition with parameters and then calling the Report macro in the loop with &b as a parameter;

You need to provide for a close of your HTML file and reopen the listing destination;

I suspect you have trimmed a lot of your code out as the example wouldn't create any output.


Contributor
Posts: 36

Re: Not able to resolve macro within %do loop.

Hi Tom / ballarw,

No i don't need the extra underscore , yes  i would like to use it as a filename and no, i didn't included the entire file path sorry for that.

Yes ballardw , sure i will do that and yes i trimmed a lot but i am getting the output with original code.

Thank you,

Harish.


Solution
‎05-24-2012 02:59 PM
Super User
Posts: 5,071

Re: Not able to resolve macro within %do loop.

Harish,

Here's my take on what happened.  First, on your diagnosis, we can't see the messages you got and I would reserve the right to change my mind if I saw them.  I don't believe that &B couldn't be resolved the first time (unless you have messages to the contrary).  Rather, here's what I suspect.

When you define the macro like this:

%macro report();

SAS allows that there might be parameters entered. Invoking the macro like this:

%report

isn't enough to trigger the macro to execute.  SAS waits, thinking that you might still enter parameters in parentheses.  That's the result after one iteration through the %DO loop.  Then the %DO loop continues with &i=2, resetting &B and redefining %REPORT.

Finally, SAS encounters the second invocation of %REPORT.  That's enough for SAS to recognize that the first invocation is complete, and no parameters will be forthcoming.  So the first time %REPORT actually executes is at the end of the second iteration of the %DO loop, when &B has already been changed to BAS.  And the second execution of %REPORT hangs (or eventually runs with &B again equal to BAS), awaiting either parameters or some other trigger that tells SAS that there won't be any parameters.

Good luck.

☑ This topic is SOLVED.

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

Discussion stats
  • 5 replies
  • 384 views
  • 0 likes
  • 4 in conversation