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

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.

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

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

5 REPLIES 5
hari1109
Fluorite | Level 6


Got solved but i don't know the reason :

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

Thank you,

Harish.

Tom
Super User Tom
Super User

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?

ballardw
Super User

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.


hari1109
Fluorite | Level 6

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.


Astounding
PROC Star

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.

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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