<?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: Value returned in a macro in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Value-returned-in-a-macro/m-p/461911#M117532</link>
    <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13976"&gt;@SASKiwi&lt;/a&gt;&amp;nbsp; Thank you sir/madam for the great illustration. That was really bugging me. Cheers!&lt;/P&gt;</description>
    <pubDate>Mon, 14 May 2018 02:44:24 GMT</pubDate>
    <dc:creator>novinosrin</dc:creator>
    <dc:date>2018-05-14T02:44:24Z</dc:date>
    <item>
      <title>Value returned in a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Value-returned-in-a-macro/m-p/461868#M117516</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I am using a macro in a simliar way as a function in other languages. I want my macro to make a calculation and return value.&lt;/P&gt;
&lt;P&gt;I need to do this in that way because I will use this calculation in many places on my code and the bes solution is a maro.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;My code:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro obt_max;

proc sql;
select max(id_reg) into: max_reg
from tab1;
quit;

&amp;amp;max_reg;

%mend;

%let max = %obt_max;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I got this error:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;34         %let max = %obt_max;
NOTE: Line generated by the invoked macro "OBT_MAX".
34          proc sql; select max(id_reg) into: max_reg from tab1; quit;  &amp;amp;max_reg;
                      ______
                      180
ERROR 180-322: Statement is not valid or it is used out of proper order.

WARNING: Apparent symbolic reference MAX_REG not resolved.
NOTE: Line generated by the invoked macro "OBT_MAX".
34          proc sql; select max(id_reg) into: max_reg from tab1; quit;  &amp;amp;max_reg;
                                                                         _
                                                                         180
ERROR 180-322: Statement is not valid or it is used out of proper order.
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I think that to use that way to return a value I can't use proc sql or data step code in my macro, only macrolenguage code.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Is there any other way to return a value in a macro?, any other solution?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Any advice will be greatly appreciated.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks&lt;/P&gt;</description>
      <pubDate>Sun, 13 May 2018 21:25:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Value-returned-in-a-macro/m-p/461868#M117516</guid>
      <dc:creator>juanvg1972</dc:creator>
      <dc:date>2018-05-13T21:25:12Z</dc:date>
    </item>
    <item>
      <title>Re: Value returned in a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Value-returned-in-a-macro/m-p/461869#M117517</link>
      <description>&lt;P&gt;macro language is basically a text generator. You should try&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE class=" language-sas"&gt;&lt;CODE class="  language-sas"&gt;&lt;SPAN class="token macroname"&gt;%let&lt;/SPAN&gt; &lt;SPAN class="token function"&gt;max&lt;/SPAN&gt; &lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt; &amp;amp;&lt;SPAN class="token macroname"&gt;max_reg&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 13 May 2018 21:37:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Value-returned-in-a-macro/m-p/461869#M117517</guid>
      <dc:creator>kiranv_</dc:creator>
      <dc:date>2018-05-13T21:37:22Z</dc:date>
    </item>
    <item>
      <title>Re: Value returned in a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Value-returned-in-a-macro/m-p/461871#M117518</link>
      <description>&lt;P&gt;You are working very hard to do something very simple. Creating this macro variable &amp;amp;max_reg inside a macro simply isn't necessary.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Don't put this SQL in a macro at all. Then you can use &amp;amp;max_reg wherever you want. And then the problem of returning a macro variable value from your macro goes away.&lt;/P&gt;</description>
      <pubDate>Sun, 13 May 2018 21:43:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Value-returned-in-a-macro/m-p/461871#M117518</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2018-05-13T21:43:58Z</dc:date>
    </item>
    <item>
      <title>Re: Value returned in a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Value-returned-in-a-macro/m-p/461874#M117519</link>
      <description>&lt;P&gt;I know that I can put this SQL out of the macro, but I don't want to write it 50 times in my code in 50 different places, I prefer to call a macro, that's the reason I am using a macro. Also is possible that I would have to change the code of my nacro and the I only have ti change in one place not 50.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If is there any other way to return a value I will consider..&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thansk&lt;/P&gt;</description>
      <pubDate>Sun, 13 May 2018 21:55:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Value-returned-in-a-macro/m-p/461874#M117519</guid>
      <dc:creator>juanvg1972</dc:creator>
      <dc:date>2018-05-13T21:55:44Z</dc:date>
    </item>
    <item>
      <title>Re: Value returned in a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Value-returned-in-a-macro/m-p/461875#M117520</link>
      <description>&lt;P&gt;I know it.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This is not my question&lt;/P&gt;</description>
      <pubDate>Sun, 13 May 2018 21:56:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Value-returned-in-a-macro/m-p/461875#M117520</guid>
      <dc:creator>juanvg1972</dc:creator>
      <dc:date>2018-05-13T21:56:21Z</dc:date>
    </item>
    <item>
      <title>Re: Value returned in a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Value-returned-in-a-macro/m-p/461878#M117521</link>
      <description>&lt;P&gt;So you want to execute the code below 50 times?&lt;/P&gt;
&lt;P&gt;Why?&lt;/P&gt;
&lt;P&gt;You will get the same answer each time.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
select max(id_reg) into: max_reg
from tab1;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 13 May 2018 22:18:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Value-returned-in-a-macro/m-p/461878#M117521</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2018-05-13T22:18:09Z</dc:date>
    </item>
    <item>
      <title>Re: Value returned in a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Value-returned-in-a-macro/m-p/461882#M117522</link>
      <description>&lt;P&gt;You are correct - if you want to use a macro as a function and have it embedded in another macro statement like %LET then the macro must contain only macro statements. It will work if you just pass a value back and just use it in normal SAS code.&lt;/P&gt;</description>
      <pubDate>Sun, 13 May 2018 22:39:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Value-returned-in-a-macro/m-p/461882#M117522</guid>
      <dc:creator>SASKiwi</dc:creator>
      <dc:date>2018-05-13T22:39:49Z</dc:date>
    </item>
    <item>
      <title>Re: Value returned in a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Value-returned-in-a-macro/m-p/461885#M117524</link>
      <description>&lt;P&gt;Would PROC FCMP work instead of a macro?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/52605"&gt;@juanvg1972&lt;/a&gt; wrote:&lt;BR /&gt;
&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I am using a macro in a simliar way as a function in other languages. I want my macro to make a calculation and return value.&lt;/P&gt;
&lt;P&gt;I need to do this in that way because I will use this calculation in many places on my code and the bes solution is a maro.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;My code:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro obt_max;

proc sql;
select max(id_reg) into: max_reg
from tab1;
quit;

&amp;amp;max_reg;

%mend;

%let max = %obt_max;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I got this error:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;34         %let max = %obt_max;
NOTE: Line generated by the invoked macro "OBT_MAX".
34          proc sql; select max(id_reg) into: max_reg from tab1; quit;  &amp;amp;max_reg;
                      ______
                      180
ERROR 180-322: Statement is not valid or it is used out of proper order.

WARNING: Apparent symbolic reference MAX_REG not resolved.
NOTE: Line generated by the invoked macro "OBT_MAX".
34          proc sql; select max(id_reg) into: max_reg from tab1; quit;  &amp;amp;max_reg;
                                                                         _
                                                                         180
ERROR 180-322: Statement is not valid or it is used out of proper order.
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I think that to use that way to return a value I can't use proc sql or data step code in my macro, only macrolenguage code.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Is there any other way to return a value in a macro?, any other solution?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Any advice will be greatly appreciated.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 13 May 2018 22:43:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Value-returned-in-a-macro/m-p/461885#M117524</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2018-05-13T22:43:56Z</dc:date>
    </item>
    <item>
      <title>Re: Value returned in a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Value-returned-in-a-macro/m-p/461893#M117525</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/52605"&gt;@juanvg1972&lt;/a&gt;&amp;nbsp; Very interesting question and thank you for making me want to learn something my tests below continue to fail&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%macro obt_max;&lt;BR /&gt;&lt;STRONG&gt;%global max_reg;&lt;/STRONG&gt;&lt;BR /&gt;&lt;STRONG&gt;%let max_reg=;&lt;/STRONG&gt;&lt;BR /&gt;proc sql;&lt;/P&gt;&lt;P&gt;select max(height) into : max_reg&lt;BR /&gt;from sashelp.class;&lt;BR /&gt;quit;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;%let max_reg=&amp;amp;max_reg ;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;&amp;amp;max_reg&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;%mend;&lt;/P&gt;&lt;P&gt;%let max = %obt_max;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;no luck&lt;/P&gt;&lt;P&gt;is it possible to get russ tyndall's or ron fehd attention here&amp;nbsp;for greater explanation? or whomsoever can help whats going on&lt;/P&gt;</description>
      <pubDate>Sun, 13 May 2018 23:20:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Value-returned-in-a-macro/m-p/461893#M117525</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2018-05-13T23:20:45Z</dc:date>
    </item>
    <item>
      <title>Re: Value returned in a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Value-returned-in-a-macro/m-p/461900#M117530</link>
      <description>&lt;P&gt;Calling as function doesn't work because you are trying to run normal SAS code inside a single macro statement:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro obt_max;

%let max_age=;

proc sql noprint;
select max(age) into: max_age
from sashelp.class;
quit;

&amp;amp;max_age

%mend;

%let max_age2 = %obt_max;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Printing result in %PUT or normal SAS code works:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro obt_max;

%global max_age;
%let max_age=;

proc sql noprint;
select max(age) into: max_age
from sashelp.class;
quit;

%mend;

%obt_max;

%put max_age= &amp;amp;max_age;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Example of macro that works like a function - just macro statements work OK:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro row_count (tablename =);
  %* OBS method uses SCL to open a table, get the row count, close it and return the row count
     so it works like a function.;
    %local dsid obs; 
    %let dsid = %sysfunc(open(&amp;amp;tablename)); 
    %if &amp;amp;dsid %then %let obs = %sysfunc(attrn(&amp;amp;dsid, nlobs)); 
    %let tmp_varlist = &amp;amp;obs;
    %let dsid = %sysfunc(close(&amp;amp;dsid)); 
    &amp;amp;obs
%mend row_count; 

%let rows = %row_count(tablename = sashelp.class);
%put rows = &amp;amp;rows;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 14 May 2018 00:13:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Value-returned-in-a-macro/m-p/461900#M117530</guid>
      <dc:creator>SASKiwi</dc:creator>
      <dc:date>2018-05-14T00:13:34Z</dc:date>
    </item>
    <item>
      <title>Re: Value returned in a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Value-returned-in-a-macro/m-p/461911#M117532</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13976"&gt;@SASKiwi&lt;/a&gt;&amp;nbsp; Thank you sir/madam for the great illustration. That was really bugging me. Cheers!&lt;/P&gt;</description>
      <pubDate>Mon, 14 May 2018 02:44:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Value-returned-in-a-macro/m-p/461911#M117532</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2018-05-14T02:44:24Z</dc:date>
    </item>
    <item>
      <title>Re: Value returned in a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Value-returned-in-a-macro/m-p/462048#M117555</link>
      <description>&lt;P&gt;No, I prefer a macro&lt;/P&gt;</description>
      <pubDate>Mon, 14 May 2018 14:03:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Value-returned-in-a-macro/m-p/462048#M117555</guid>
      <dc:creator>juanvg1972</dc:creator>
      <dc:date>2018-05-14T14:03:11Z</dc:date>
    </item>
  </channel>
</rss>

