BookmarkSubscribeRSS Feed
fatemeh
Quartz | Level 8

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,

6 REPLIES 6
ballardw
Super User

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.

fatemeh
Quartz | Level 8

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);

 

ballardw
Super User

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;

 

fatemeh
Quartz | Level 8

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);
ballardw
Super User

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.

Ron_MacroMaven
Lapis Lazuli | Level 10

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

 

 

 

 

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 6 replies
  • 1057 views
  • 1 like
  • 3 in conversation