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
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;
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.
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 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;
@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.
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.
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
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.
Ready to level-up your skills? Choose your own adventure.