<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Best Option for Return Value for Macro in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Best-Option-for-Return-Value-for-Macro/m-p/981351#M379085</link>
    <description>&lt;P&gt;Have the user tell the macro where to put the value.&lt;/P&gt;
&lt;P&gt;Say you make a macro like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro MACRO_WITH_RETURN_VALUE
(mvar=return_value /* Name of macro variable to store result */
);
%if not %symexist(&amp;amp;mvar) %then %global &amp;amp;mvar;
%let &amp;amp;mvar=Value returned ;
%mend;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Now the user can call it like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%* Create the macro variable ;
%LET RETURN_VALUE =;
%* Run the macro ;
%MACRO_WITH_RETURN_VALUE(mvar=return_value)
%* See what the result was ;
%put &amp;amp;=return_value;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Mon, 29 Dec 2025 21:43:11 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2025-12-29T21:43:11Z</dc:date>
    <item>
      <title>Best Option for Return Value for Macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Best-Option-for-Return-Value-for-Macro/m-p/981347#M379082</link>
      <description>&lt;P&gt;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:&lt;/P&gt;&lt;P&gt;%LET RETURN_VALUE = %MACRO_WITH_RETURN_VALUE();&lt;/P&gt;&lt;P&gt;If this is completely impossible because macro just generates code, what are workarounds that you think are the best solution in this case?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Best regards and thanks in advance?&lt;/P&gt;</description>
      <pubDate>Mon, 29 Dec 2025 19:24:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Best-Option-for-Return-Value-for-Macro/m-p/981347#M379082</guid>
      <dc:creator>Baki</dc:creator>
      <dc:date>2025-12-29T19:24:09Z</dc:date>
    </item>
    <item>
      <title>Re: Best Option for Return Value for Macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Best-Option-for-Return-Value-for-Macro/m-p/981349#M379083</link>
      <description>&lt;P&gt;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.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can do it this way:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* f(x) = x*x + 5*x + 17 */
%macro f(x);
%local result;
%let result=%sysevalf(&amp;amp;x.*&amp;amp;x. + 5*&amp;amp;x. + 17);

&amp;amp;result.
%mend f;

%let y=%f(42);

%put &amp;amp;=y.;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;if your code is more than just calculation, e.g., you need some more complex calculations you need to make your code "pure macro".&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This is NOT pure macro:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro NP(x);
proc sql noprint; /* this is not "pure" macro */
select count(*) as n
into :result /* macro variable */
from sashelp.class
where age=&amp;amp;x.
;
quit;

&amp;amp;result.
%mend NP;

%let y=%NP(12);

%put &amp;amp;=y.;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;But if you use DoSubL() function you can "hide" 4GL inside macro language&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%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=&amp;amp;x.
;
quit;
)));
&amp;amp;result.
%mend P;

%let y=%P(12);

%put &amp;amp;=y.;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you want to implement a SAS function consider Proc FCMP.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Bart&lt;/P&gt;</description>
      <pubDate>Mon, 29 Dec 2025 19:46:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Best-Option-for-Return-Value-for-Macro/m-p/981349#M379083</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2025-12-29T19:46:22Z</dc:date>
    </item>
    <item>
      <title>Re: Best Option for Return Value for Macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Best-Option-for-Return-Value-for-Macro/m-p/981351#M379085</link>
      <description>&lt;P&gt;Have the user tell the macro where to put the value.&lt;/P&gt;
&lt;P&gt;Say you make a macro like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro MACRO_WITH_RETURN_VALUE
(mvar=return_value /* Name of macro variable to store result */
);
%if not %symexist(&amp;amp;mvar) %then %global &amp;amp;mvar;
%let &amp;amp;mvar=Value returned ;
%mend;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Now the user can call it like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%* Create the macro variable ;
%LET RETURN_VALUE =;
%* Run the macro ;
%MACRO_WITH_RETURN_VALUE(mvar=return_value)
%* See what the result was ;
%put &amp;amp;=return_value;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 29 Dec 2025 21:43:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Best-Option-for-Return-Value-for-Macro/m-p/981351#M379085</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2025-12-29T21:43:11Z</dc:date>
    </item>
  </channel>
</rss>

