DATA Step, Macro, Functions and more

one macro functions be executed inside another macro function

Reply
Occasional Contributor
Posts: 15

one macro functions be executed inside another macro function

hi all.

how can i use a macro function which should be executed  inside another macro function ,because the result of inside macro function is needed to be used in the outside macro function .is there any way to do that .if anyone knows that i will appreciate to share it with me.

thanks,

Super User
Posts: 10,511

Re: one macro functions be executed inside another macro function

With any exising macro you call it in side the other macro as normal.

 

%macro outer (< your parameters>);

 

<misc code>

%othermacro(<its parameters>);

<other code>

%mend;

 

%outer (<parameters>);

 

Some things to consider: You may need to to know the scope of macro variables. If you have the same named macro variable in the outer macro and used in the inner then you may get unexpected results. Also if a macro variable is to be available in the outer macro from the inner then you may need to specify the varaible as %global in the inner macro. Also you need to be careful about using the same names for temporary data sets.

 

Is the 'result' a single value, a macro variable or something else? You might post the code for the inner macro and indicate what part needs to be used by the outer macro.

Occasional Contributor
Posts: 15

Re: one macro functions be executed inside another macro function

hi.thanks for your reply .i need to repeat inner macro then combine the results of inner macro and use it in outer macro.

here is some part of the code.how can i have the output1 and output2 and output3 in second macro.

%macro one(x);

data mydata1;

.......output$x;

.

.

%macro two(y);

data mydata2;

set output1 output2  output3 ;

 

%mend;

%two(y);

 

Super User
Posts: 10,511

Re: one macro functions be executed inside another macro function

Insufficient information.

You need to describe you start data a bit and what you are doing. First of all, you "example" code implies that you are defining macro two inside macro one. That is an approach that without a very strong reason is usually a poor choice as changes in the outer may affect the behavior of the inner in unexpected ways. Define each macro separately and provide parameters to receive the values that the second macro may need.

 

If by combine results you need to put the multiple data sets on a set statement then pass a variable with the same index variable that created them and then use that in a do loop.

A generic loop would look somthing like

%macro two (index=);

 

data mydata;

   set

   %do i = 1 %to &index;

        output&i

   %end;

    ;  /*<= this ; ends the SET statement and has nothing to do with the macro language portion*/

run;

%mend two;

 

Occasional Contributor
Posts: 15

Re: one macro functions be executed inside another macro function

here is more detail about my cod.still is not executing.appreciate your help to solve my problem..

 

%macro macy(x,y,z,index);
%do i=1 %to &index;

ods output FitStatistics=outputt&i;
  data =exercise;
 .........
  if i=1  then repeated/type=&x ;
  else if i=2 then repeated/type=&y;
  else if i=3;then repeated/type=&z;

  end;
 output;
 %end;

   data estout;
  set outputt1 outputt2 outputt3; 
 run; 
  
 %mend macy;   

%macy(un,cs,ar,3);
Super User
Posts: 10,511

Re: one macro functions be executed inside another macro function

It really does help to show starting data and what you want at the end.

 

Is this supposed to calculate a different variable and place the results in a different data set for each value of the macro variable i?

ods output FitStatistics=outputt&i;
  data =exercise;
 .........
  if i=1  then repeated/type=&x ;
  else if i=2 then repeated/type=&y;
  else if i=3;then repeated/type=&z;

  end;
 output;

Then you have more problems. In SAS all assignments are with the receiving value on the left of an = sign.

 

The code you seem to be attempting to create: repeated/type=&x is going to do weird things.

If you want to create a variable named UN in the data set and assign the value of repeated/type it should be

&x = repeated / type;

The MACRO Variable condition would make it look like this:

%if &i=1 %then %do;

    &x= repeated / type;

%end;

 

Since you don't show a set statement or anything about the incoming data I have no understanding of why you have an ODS OUTPUT statement as that is only going to work with a procedure that generated a FitStatistics table. But there is no procedure. So it is confusing.

 

Did you have a procedure that did what you want before writing any of the macro code? If so show us and we can help you get it into a macro, or possibly determine a macro is not needed at all. If you don't have code that works without the macro then you don't know what the macro is supposed to generate. All the SAS macro language does is generate code but you have to know the code you want it to generate.

Regular Contributor
Posts: 200

Re: one macro functions be executed inside another macro function

The issues of writing a macro 'function' which returns a value are

1. naming conventions

2. scope of returned value: global, or local, and local to which?

 

a macro 'function' does not contain any SAS statement semicolons

 

your code shows your macro generating SAS statements, so yes, you want it to 'return' a value

but you cannot use function syntax to do that.

 

your choice then is to place the returned value in the scope of the calling (outside) macro

or into the global symbol table.

 

%macro max(left,right);
%if &left ge &right %then &left;
%else &right;
%mend;


*return value into global symbol table;
%let x = %max(3,4);
%put echo &=x;


%macro call_max(data=sashelp.class);
%local x;
%let x = %max(4,5);
%put echo &sysmacroname &=x;
%mend;

%call_max(data=sashelp.shoes)

*what is the value of X in the global symbol table?;

%put echo &=x;


%macro max_to_global(left,right);
%global x_max;
%if &left ge &right %then %let x_max = &left;
%else %let x_max = &right;
%mend;

%macro call_max_global(data=sashelp.class);
%local x;
%max_to_global(6,7)
%put echo global &=x_max;
%let x = &x_max;
%put echo &sysmacroname &=x;
%mend;

%call_max_global(data=sashelp.shoes)

*what is the value of X in the global symbol table?;

%put echo &=x &=x_max;

hth

Ron Fehd  macro maven

 

 

 

 

Ask a Question
Discussion stats
  • 6 replies
  • 116 views
  • 1 like
  • 3 in conversation