DATA Step, Macro, Functions and more

Executing macros from a data step

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 122
Accepted Solution

Executing macros from a data step

Hi,

 

I have created a macro, and now I want to execute it from a data step. Somethig similar to:

 

data ds_val1;
set ds_val;
var5 = %macro_val(var4);
run;

The problem is that I want to execute it for each obs of dataset ds_val, var4 is a variable of the dataset.

If I execute the data step , the macro is resolved before executing the data step.

What changes do I have to do in order to execute the macro (macro_val) for each obs of ds_val?

 

Thanks in advance


Accepted Solutions
Solution
‎05-31-2017 02:32 PM
Super User
Super User
Posts: 7,076

Re: Executing macros from a data step

[ Edited ]
Posted in reply to juanvg1972

It depends on what, if any, SAS code the macro is generating.

If the macro totally consists of macro statements and just calculates and returns the result as the generated code

%macro macro_val(var);
%eval(&var + 100)
%mend;

then it could work, but you will need to do more work to be able to pass the value of a dataset variable to the macro.

data want;
  set have ;
  new_var = input(resolve(cats('%macro_val(',old_var,')'),32.);
run;

For this type of equation is might be better if the macro generate the SAS code to generate the required value.  Then you could call it with the name for variable to use in the generated code.

%macro macro1(varname);
&varname + 100
%mend;

data want;
  set have ;
  new_var1 = %macro1(old_var1);
  new_var2 = %macro1(old_var2);
run;

Or if the equation is more complex then have it generate multiple data step statements. In that case you might want to pass in both  the input and output variable names.

 

%macro macro2(inname,outname);
temp=&inname;
&outname=temp + 100;
%mend;

data want;
  set have ;
  %macro2(old_var1,new_var2)
  %macro2(old_var2,new_var2)
run;

View solution in original post


All Replies
Trusted Advisor
Posts: 1,932

Re: Executing macros from a data step

Posted in reply to juanvg1972

This ought to work if the macro %macro_val contains only syntactically correct fragments of data step code. It should operate on every observation of the data set.

 

Naturally, since you haven't shown us the code for %macro_val, we can't be more specific about what is wrong.

Frequent Contributor
Posts: 122

Re: Executing macros from a data step

Posted in reply to PaigeMiller

In the macro m,acro_val  I make some calculations using var4 (field of ds_val) as parameter of the macro and it returns a value

I don't know if I can use call execute

 

%macro macro_val(var);

/* Calculations using var as input */

&result;

%mend;
Solution
‎05-31-2017 02:32 PM
Super User
Super User
Posts: 7,076

Re: Executing macros from a data step

[ Edited ]
Posted in reply to juanvg1972

It depends on what, if any, SAS code the macro is generating.

If the macro totally consists of macro statements and just calculates and returns the result as the generated code

%macro macro_val(var);
%eval(&var + 100)
%mend;

then it could work, but you will need to do more work to be able to pass the value of a dataset variable to the macro.

data want;
  set have ;
  new_var = input(resolve(cats('%macro_val(',old_var,')'),32.);
run;

For this type of equation is might be better if the macro generate the SAS code to generate the required value.  Then you could call it with the name for variable to use in the generated code.

%macro macro1(varname);
&varname + 100
%mend;

data want;
  set have ;
  new_var1 = %macro1(old_var1);
  new_var2 = %macro1(old_var2);
run;

Or if the equation is more complex then have it generate multiple data step statements. In that case you might want to pass in both  the input and output variable names.

 

%macro macro2(inname,outname);
temp=&inname;
&outname=temp + 100;
%mend;

data want;
  set have ;
  %macro2(old_var1,new_var2)
  %macro2(old_var2,new_var2)
run;
Trusted Advisor
Posts: 1,932

Re: Executing macros from a data step

Posted in reply to juanvg1972

juanvg1972 wrote:

In the macro m,acro_val  I make some calculations using var4 (field of ds_val) as parameter of the macro and it returns a value

I don't know if I can use call execute

 

%macro macro_val(var);

/* Calculations using var as input */

&result;

%mend;

It's still not obvious what this macro is supposed to be doing, because you are still keeping us in the dark. We don't know what &result is, and if it is a number, maybe that works in your original code, but if it is alphanumeric, then it won't work. You don't need the (var) part of macro_val(var) as the macro doesn't use &var.

 

So please, tell us what the macro does, tell us what &result is, show us more code and relevant parts of the SASLOG.

Super User
Super User
Posts: 7,076

Re: Executing macros from a data step

Posted in reply to juanvg1972

Macros generate SAS code. So unless your macro generates just an expresson that can be used as part of a statement, like the assignment statement you have written, then there is no way.

 

If you want to generate macro calls for each value in a dataset then use CALL EXECUTE() to stack the macro calls to run after the current data step finishes.  There are also other ways to generate a series of macro calls from a list of values.

 

Give a complete discription of what you actually want to accomplish to get a more detailed responce.  Most likely you just need to change your way of thinking about the problem.

☑ This topic is solved.

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

Discussion stats
  • 5 replies
  • 165 views
  • 2 likes
  • 3 in conversation