BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
carles
Fluorite | Level 6

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 !

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
gamotte
Rhodochrosite | Level 12

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

View solution in original post

4 REPLIES 4
gamotte
Rhodochrosite | Level 12

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;

gamotte
Rhodochrosite | Level 12

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.;
andreas_lds
Jade | Level 19

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...

 

ballardw
Super User

@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.

SAS Innovate 2025: Register Now

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!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 1499 views
  • 1 like
  • 4 in conversation