Desktop productivity for business analysts and programmers

Macro within macro

Reply
Contributor
Posts: 31

Macro within macro

Hello,

I am executing macro based on condition ,in that macro i want to execute another macro program, its not working ,can any one help one this .? for your referennce i given example below.

%macro A;

%if  Count=0 %then %do;

proc print data=a;

run;

%else %if Count>0 %then %do;

ods pdf file='TEST/TEST....pdf';

Proc report Data=TEST;

run;

%put 'TESTING';

%macro B;

%if  Count1=0 %then %do;

proc print data=TEST2;

run;

%else %if Count1>0 %then %do;

proc print data=TEST3;

run;

%end;

%mend B;

%B;

proc report dat=Test2 ;

run;

ods pdf close;

%end;

%mend A;

%A;

Thanks,

Ramesh

%end;

%mend A;

%A;

SAS Super FREQ
Posts: 8,719

Re: Macro within macro

Hi:

  I see a few problems and issues. First, in your %IF test, you test like this:

%if Count = 0 and %if Count1 = 0 

I am not sure what COUNT and COUNT1 are. If they are DATA SET variables, then your macro needs more work. Generally, in a %IF statement, you are testing MACRO variables, such as:

%if &COUNT=0 %then %do;

There is a huge difference between referencing COUNT and &COUNT.

  Next, I have never, in all my years of doing macro programming (and that is a LOT of years), nested macro DEFINITIONS inside each other, the way you show. I have always followed the practice of defining my macro programs separately, some thing like this:

%macro a;

...macro programming statements to generate code.... and possibly have a call to another macro ...

%b

%mend a;

%macro b;

... macro programming statements to generate code ...

%mend b;

and then either have a new macro program to run everything :

%macro runit;

%a;

%mend runit;

%runit

OR just invoke the first macro that contains the call to the second macro:

%a

But, given that I don't understand what you are trying to do in your macro programs, I wonder whether you actually need 2 macros at all. The rule of thumb is that you need to start with a working SAS program without any macro variables and get your program working first. That means making sure all your proc prints and proc reports work correctly and that your ODS PDF code generates the file you want.

Next, the best thing to do is use %LET statements to assign the values of the macro variables you think you will need and test your program using these macro variables. If you are going to generate huge chunks of code inside a macro program, then you would need to define your macro variables and use them inside a macro program with macro conditional logic. You have to iron out for yourself and figure out where COUNT or &COUNT comes from. The way things are written now, I would expect things not to work.

Also, in your code, you show this

ods pdf file='TEST/TEST....pdf';

for the name of your PDF file. Usually, you use multiple periords in a file name because you are going to use macro variables in the name. But, macro variables must be enclosed in double quotes to be resolved and you have too many periods in the name. I would expect to see something like this:

ods pdf file="mydir/&myfilename.&count..pdf"; 

with the FILE= option in double quotes and some macro variables in the name and/or path specifications.

This is a really good introduction to macro processing and may help you figure out the best way to write your macro code. http://www2.sas.com/proceedings/sugi28/056-28.pdf

cynthia

Ask a Question
Discussion stats
  • 1 reply
  • 165 views
  • 0 likes
  • 2 in conversation