What is the best approach to create a macro that "returns a value". I get that the main idea of macro is to generate SAS code, but is there some way to implement macro that actually returns value in some sense so that I can write:
%LET RETURN_VALUE = %MACRO_WITH_RETURN_VALUE();
If this is completely impossible because macro just generates code, what are workarounds that you think are the best solution in this case?
Best regards and thanks in advance?
Macros are not code generators, they are Text(!) generators. If you write your code in such way that generated text is your "result" you can created a macro that "pretends" a function.
You can do it this way:
/* f(x) = x*x + 5*x + 17 */
%macro f(x);
%local result;
%let result=%sysevalf(&x.*&x. + 5*&x. + 17);
&result.
%mend f;
%let y=%f(42);
%put &=y.;
if your code is more than just calculation, e.g., you need some more complex calculations you need to make your code "pure macro".
This is NOT pure macro:
%macro NP(x);
proc sql noprint; /* this is not "pure" macro */
select count(*) as n
into :result /* macro variable */
from sashelp.class
where age=&x.
;
quit;
&result.
%mend NP;
%let y=%NP(12);
%put &=y.;
But if you use DoSubL() function you can "hide" 4GL inside macro language
%macro P(x);
%local result rc;
%let rc = %sysfunc(DoSubL(%str( /* this covers 4GL and behaves like pure macro code */
proc sql noprint;
select count(*) as n
into :result /* macro variable */
from sashelp.class
where age=&x.
;
quit;
)));
&result.
%mend P;
%let y=%P(12);
%put &=y.;
If you want to implement a SAS function consider Proc FCMP.
Bart
Macros are not code generators, they are Text(!) generators. If you write your code in such way that generated text is your "result" you can created a macro that "pretends" a function.
You can do it this way:
/* f(x) = x*x + 5*x + 17 */
%macro f(x);
%local result;
%let result=%sysevalf(&x.*&x. + 5*&x. + 17);
&result.
%mend f;
%let y=%f(42);
%put &=y.;
if your code is more than just calculation, e.g., you need some more complex calculations you need to make your code "pure macro".
This is NOT pure macro:
%macro NP(x);
proc sql noprint; /* this is not "pure" macro */
select count(*) as n
into :result /* macro variable */
from sashelp.class
where age=&x.
;
quit;
&result.
%mend NP;
%let y=%NP(12);
%put &=y.;
But if you use DoSubL() function you can "hide" 4GL inside macro language
%macro P(x);
%local result rc;
%let rc = %sysfunc(DoSubL(%str( /* this covers 4GL and behaves like pure macro code */
proc sql noprint;
select count(*) as n
into :result /* macro variable */
from sashelp.class
where age=&x.
;
quit;
)));
&result.
%mend P;
%let y=%P(12);
%put &=y.;
If you want to implement a SAS function consider Proc FCMP.
Bart
Have the user tell the macro where to put the value.
Say you make a macro like this:
%macro MACRO_WITH_RETURN_VALUE
(mvar=return_value /* Name of macro variable to store result */
);
%if not %symexist(&mvar) %then %global &mvar;
%let &mvar=Value returned ;
%mend;
Now the user can call it like this:
%* Create the macro variable ;
%LET RETURN_VALUE =;
%* Run the macro ;
%MACRO_WITH_RETURN_VALUE(mvar=return_value)
%* See what the result was ;
%put &=return_value;
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
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.