BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
Baki
Calcite | Level 5

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?

1 ACCEPTED SOLUTION

Accepted Solutions
yabwon
Amethyst | Level 16

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

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



View solution in original post

2 REPLIES 2
yabwon
Amethyst | Level 16

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

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



Tom
Super User Tom
Super User

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;

sas-innovate-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

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!

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
  • 2 replies
  • 145 views
  • 1 like
  • 3 in conversation