Hi!,
I am new to SAS and I was trying to concatenate functions (Macros).
I have created these two functions:
%MACRO MySUMA(number1, number2); %let x = &number1 + &number2; data p; result= &x; run; %MEND; %MySUMA(7,3); %Macro MyResta(number1, number2); %let x = &number1 - &number2; data p; result= &x; run;
These two functions work well separately. However, when I try to concatenate them, i dont get any result.
%MyResta(5, %MySuma(2,2));
Thank you for the time and the help in advanced !
Since macro are only text substitution, you can obtain a sort of function behaviour
by having your macros resolve to the wanted result.
%macro sum(num1, num2);
%let s=%eval(&num1.+&num2.);
&s.
%mend;
%macro diff(num1, num2);
%let d=%eval(&num1.-&num2.);
&d.
%mend;
%let mydiff=%diff(5, %sum(2,2));
%put &=mydiff.;
Hello,
That's because macros are not functions. Note that your Myresta macro doesn't return anything.
Macro language performs text substition. So, in your macro call :
%MyResta(5, %MySuma(2,2));
%MySuma(2,2) is replaced by the code generated by the macro call :
data p;
result=2-2;
run;
Since macro are only text substitution, you can obtain a sort of function behaviour
by having your macros resolve to the wanted result.
%macro sum(num1, num2);
%let s=%eval(&num1.+&num2.);
&s.
%mend;
%macro diff(num1, num2);
%let d=%eval(&num1.-&num2.);
&d.
%mend;
%let mydiff=%diff(5, %sum(2,2));
%put &=mydiff.;
If you want to define your own functions, look at proc fcmp.
You find the official docs at https://documentation.sas.com/?cdcId=pgmsascdc&cdcVersion=9.4_3.4&docsetId=proc&docsetTarget=p10b4qo...
@carles wrote:
Hi!,
I am new to SAS and I was trying to concatenate functions (Macros).
I have created these two functions:
%MACRO MySUMA(number1, number2); %let x = &number1 + &number2; data p; result= &x; run; %MEND; %MySUMA(7,3); %Macro MyResta(number1, number2); %let x = &number1 - &number2; data p; result= &x; run;These two functions work well separately. However, when I try to concatenate them, i dont get any result.
%MyResta(5, %MySuma(2,2));Thank you for the time and the help in advanced !
Here are details why yours did not work. Basically the Macro language creates text (that hopefully results in useable SAS code). So when you attempt to use
%MyResta(5, %MySuma(2,2));
The macro processor expands the %Mysuma macro call to look like:
%MyResta(5,data p;result= 2 + 2;run;);
Which has multiple issues such as the multiple semicolons.
There is a way to keep the macro from resolving there (which I won't do into at the moment, look up MACRO Quoting)
but if you had successfully done that your code would have gone through steps such as:
%let x = &number1 - %MySuma(2,2);
resolving to
%let x = 5 - data p;result= 2 + 2;run;
Which the SAS processor would actually see as
result= 2 + 2;run; data p; result= &x; run;
because of the first ; in the MySuma macro.
So the SAS processor would almost certainly generate an error message because "result=2+2;" without a data step doesn't mean much.
But more goes one. The data step using &x resolves to:
data p; result= 5 - data p; run;
which is also syntactically incorrect because of the space between data and p. A variable DATA would be attempted using a missing value but the p would generate this error:
ERROR 22-322: Syntax error, expecting one of the following: !, !!, &, *, **, +, -, /, <, <=, <>, =, >, ><, >=, AND, EQ, GE, GT, IN, LE, LT, MAX, MIN, NE, NG, NL, NOTIN, OR, ^=, |, ||, ~=.
because there was no operator indicating what should be done with that p.
Coding style comment: There is no obvious reason for the creation of variable x as you are using it. More commonly I would expect:
data p;
result= &number1 + &number2;
run;
Though you might want to consider: result= sum(&number1, &number2); if you want a numeric value when one of the number variables is missing.
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.