<?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: Passing as variable to a macro the result of another macro in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Passing-as-variable-to-a-macro-the-result-of-another-macro/m-p/550312#M152777</link>
    <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/270253"&gt;@carles&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hi!,&lt;/P&gt;
&lt;P&gt;I am new to SAS and I was trying to concatenate functions (Macros).&lt;/P&gt;
&lt;P&gt;I have created these two functions:&lt;/P&gt;
&lt;PRE&gt;%MACRO MySUMA(number1, number2);
	%let x = &amp;amp;number1 + &amp;amp;number2;
	data p;
		result= &amp;amp;x;
	run;		
%MEND;

%MySUMA(7,3);


%Macro MyResta(number1, number2);
	%let x = &amp;amp;number1 - &amp;amp;number2;
		data p;
			result= &amp;amp;x;
		run;

&lt;/PRE&gt;
&lt;P&gt;These two functions work well separately. However, when I try to concatenate them, i dont get any result.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;%MyResta(5, %MySuma(2,2));	&lt;/PRE&gt;
&lt;P&gt;Thank you for the time and the help in advanced !&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;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;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&lt;/P&gt;
&lt;PRE&gt;%MyResta(5, %MySuma(2,2));&lt;/PRE&gt;
&lt;P&gt;The macro processor expands the %Mysuma macro call to look like:&lt;/P&gt;
&lt;PRE&gt;%MyResta(5,data p;result= 2 + 2;run;);&lt;/PRE&gt;
&lt;P&gt;Which has multiple issues such as the multiple semicolons.&lt;/P&gt;
&lt;P&gt;There is a way to keep the macro from resolving there (which I won't do into at the moment, look up MACRO Quoting)&lt;/P&gt;
&lt;P&gt;but if you had successfully done that your code would have gone through steps such as:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#ff0080" face="SAS Monospace" size="2"&gt;%let&lt;/FONT&gt;&lt;FONT face="SAS Monospace" size="2"&gt; x = &amp;amp;number1 - %MySuma(2,2);&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="SAS Monospace" size="2"&gt;resolving to&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#ff0080" face="SAS Monospace" size="2"&gt;%let&lt;/FONT&gt;&lt;FONT face="SAS Monospace" size="2"&gt; x = 5 - data p;result= &lt;/FONT&gt;&lt;FONT color="#008080" face="SAS Monospace" size="2"&gt;2&lt;/FONT&gt;&lt;FONT face="SAS Monospace" size="2"&gt; + &lt;/FONT&gt;&lt;FONT color="#008080" face="SAS Monospace" size="2"&gt;2&lt;/FONT&gt;&lt;FONT face="SAS Monospace" size="2"&gt;;&lt;/FONT&gt;&lt;FONT color="#0000ff" face="SAS Monospace" size="2"&gt;run&lt;/FONT&gt;&lt;FONT face="SAS Monospace" size="2"&gt;;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="SAS Monospace" size="2"&gt;Which the SAS processor would actually see as&lt;/FONT&gt;&lt;/P&gt;
&lt;PRE&gt;result= 2 + 2;run;
data p;
  result= &amp;amp;x;
run;&lt;/PRE&gt;
&lt;P&gt;&lt;FONT face="SAS Monospace" size="2"&gt;because of the first ; in the MySuma macro.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="SAS Monospace" size="2"&gt;So the SAS processor would almost certainly generate an error message because "result=2+2;" without a data step doesn't mean much.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="SAS Monospace" size="2"&gt;But more goes one. The data step using &amp;amp;x resolves to:&lt;/FONT&gt;&lt;/P&gt;
&lt;PRE&gt;data p;
  result= 5 - data p;
run;&lt;/PRE&gt;
&lt;P&gt;&lt;FONT face="SAS Monospace" size="2"&gt;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:&lt;/FONT&gt;&lt;/P&gt;
&lt;PRE&gt;ERROR 22-322: Syntax error, expecting one of the following: !, !!, &amp;amp;, *, **, +, -, /, &amp;lt;, &amp;lt;=, &amp;lt;&amp;gt;,
              =, &amp;gt;, &amp;gt;&amp;lt;, &amp;gt;=, AND, EQ, GE, GT, IN, LE, LT, MAX, MIN, NE, NG, NL, NOTIN, OR, ^=, |,
              ||, ~=.
&lt;/PRE&gt;
&lt;P&gt;because there was no operator indicating what should be done with that p.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Coding style comment: There is no obvious reason for the creation of variable x as you are using it. More commonly I would expect:&lt;/P&gt;
&lt;P&gt;data p;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; result= &amp;amp;number1&amp;nbsp;+ &amp;amp;number2; &lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;Though you might want to consider: result= sum(&amp;amp;number1, &amp;amp;number2); if you want a numeric value when one of the number variables is missing.&lt;/P&gt;</description>
    <pubDate>Thu, 11 Apr 2019 16:08:57 GMT</pubDate>
    <dc:creator>ballardw</dc:creator>
    <dc:date>2019-04-11T16:08:57Z</dc:date>
    <item>
      <title>Passing as variable to a macro the result of another macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Passing-as-variable-to-a-macro-the-result-of-another-macro/m-p/550143#M152741</link>
      <description>&lt;P&gt;Hi!,&lt;/P&gt;&lt;P&gt;I am new to SAS and I was trying to concatenate functions (Macros).&lt;/P&gt;&lt;P&gt;I have created these two functions:&lt;/P&gt;&lt;PRE&gt;%MACRO MySUMA(number1, number2);
	%let x = &amp;amp;number1 + &amp;amp;number2;
	data p;
		result= &amp;amp;x;
	run;		
%MEND;

%MySUMA(7,3);


%Macro MyResta(number1, number2);
	%let x = &amp;amp;number1 - &amp;amp;number2;
		data p;
			result= &amp;amp;x;
		run;

&lt;/PRE&gt;&lt;P&gt;These two functions work well separately. However, when I try to concatenate them, i dont get any result.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;%MyResta(5, %MySuma(2,2));	&lt;/PRE&gt;&lt;P&gt;Thank you for the time and the help in advanced !&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;</description>
      <pubDate>Thu, 11 Apr 2019 08:25:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Passing-as-variable-to-a-macro-the-result-of-another-macro/m-p/550143#M152741</guid>
      <dc:creator>carles</dc:creator>
      <dc:date>2019-04-11T08:25:20Z</dc:date>
    </item>
    <item>
      <title>Re: Passing as variable to a macro the result of another macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Passing-as-variable-to-a-macro-the-result-of-another-macro/m-p/550145#M152743</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;That's because macros are not functions. Note that your Myresta macro doesn't return anything.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Macro language performs text substition. So, in your macro call :&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;%MyResta(5, %MySuma(2,2));	&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;%MySuma(2,2) is replaced by the code generated by the macro call :&lt;/P&gt;
&lt;P&gt;data p;&lt;/P&gt;
&lt;P&gt;result=2-2;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;</description>
      <pubDate>Thu, 11 Apr 2019 08:33:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Passing-as-variable-to-a-macro-the-result-of-another-macro/m-p/550145#M152743</guid>
      <dc:creator>gamotte</dc:creator>
      <dc:date>2019-04-11T08:33:07Z</dc:date>
    </item>
    <item>
      <title>Re: Passing as variable to a macro the result of another macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Passing-as-variable-to-a-macro-the-result-of-another-macro/m-p/550148#M152745</link>
      <description>&lt;P&gt;If you want to define your own functions, look at proc fcmp.&lt;/P&gt;
&lt;P&gt;You find the official docs at &lt;A href="https://documentation.sas.com/?cdcId=pgmsascdc&amp;amp;cdcVersion=9.4_3.4&amp;amp;docsetId=proc&amp;amp;docsetTarget=p10b4qouzgi6sqn154ipglazix2q.htm" target="_blank"&gt;https://documentation.sas.com/?cdcId=pgmsascdc&amp;amp;cdcVersion=9.4_3.4&amp;amp;docsetId=proc&amp;amp;docsetTarget=p10b4qouzgi6sqn154ipglazix2q.htm&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 11 Apr 2019 08:51:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Passing-as-variable-to-a-macro-the-result-of-another-macro/m-p/550148#M152745</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2019-04-11T08:51:09Z</dc:date>
    </item>
    <item>
      <title>Re: Passing as variable to a macro the result of another macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Passing-as-variable-to-a-macro-the-result-of-another-macro/m-p/550149#M152746</link>
      <description>&lt;P&gt;Since macro are only text substitution, you can obtain a sort of function behaviour&lt;/P&gt;
&lt;P&gt;by having your macros resolve to the wanted result.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro sum(num1, num2);
%let s=%eval(&amp;amp;num1.+&amp;amp;num2.);
&amp;amp;s.
%mend;

%macro diff(num1, num2);
%let d=%eval(&amp;amp;num1.-&amp;amp;num2.);
&amp;amp;d.
%mend;

%let mydiff=%diff(5, %sum(2,2));

%put &amp;amp;=mydiff.;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 11 Apr 2019 08:53:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Passing-as-variable-to-a-macro-the-result-of-another-macro/m-p/550149#M152746</guid>
      <dc:creator>gamotte</dc:creator>
      <dc:date>2019-04-11T08:53:01Z</dc:date>
    </item>
    <item>
      <title>Re: Passing as variable to a macro the result of another macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Passing-as-variable-to-a-macro-the-result-of-another-macro/m-p/550312#M152777</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/270253"&gt;@carles&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hi!,&lt;/P&gt;
&lt;P&gt;I am new to SAS and I was trying to concatenate functions (Macros).&lt;/P&gt;
&lt;P&gt;I have created these two functions:&lt;/P&gt;
&lt;PRE&gt;%MACRO MySUMA(number1, number2);
	%let x = &amp;amp;number1 + &amp;amp;number2;
	data p;
		result= &amp;amp;x;
	run;		
%MEND;

%MySUMA(7,3);


%Macro MyResta(number1, number2);
	%let x = &amp;amp;number1 - &amp;amp;number2;
		data p;
			result= &amp;amp;x;
		run;

&lt;/PRE&gt;
&lt;P&gt;These two functions work well separately. However, when I try to concatenate them, i dont get any result.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;%MyResta(5, %MySuma(2,2));	&lt;/PRE&gt;
&lt;P&gt;Thank you for the time and the help in advanced !&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;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;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&lt;/P&gt;
&lt;PRE&gt;%MyResta(5, %MySuma(2,2));&lt;/PRE&gt;
&lt;P&gt;The macro processor expands the %Mysuma macro call to look like:&lt;/P&gt;
&lt;PRE&gt;%MyResta(5,data p;result= 2 + 2;run;);&lt;/PRE&gt;
&lt;P&gt;Which has multiple issues such as the multiple semicolons.&lt;/P&gt;
&lt;P&gt;There is a way to keep the macro from resolving there (which I won't do into at the moment, look up MACRO Quoting)&lt;/P&gt;
&lt;P&gt;but if you had successfully done that your code would have gone through steps such as:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#ff0080" face="SAS Monospace" size="2"&gt;%let&lt;/FONT&gt;&lt;FONT face="SAS Monospace" size="2"&gt; x = &amp;amp;number1 - %MySuma(2,2);&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="SAS Monospace" size="2"&gt;resolving to&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#ff0080" face="SAS Monospace" size="2"&gt;%let&lt;/FONT&gt;&lt;FONT face="SAS Monospace" size="2"&gt; x = 5 - data p;result= &lt;/FONT&gt;&lt;FONT color="#008080" face="SAS Monospace" size="2"&gt;2&lt;/FONT&gt;&lt;FONT face="SAS Monospace" size="2"&gt; + &lt;/FONT&gt;&lt;FONT color="#008080" face="SAS Monospace" size="2"&gt;2&lt;/FONT&gt;&lt;FONT face="SAS Monospace" size="2"&gt;;&lt;/FONT&gt;&lt;FONT color="#0000ff" face="SAS Monospace" size="2"&gt;run&lt;/FONT&gt;&lt;FONT face="SAS Monospace" size="2"&gt;;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="SAS Monospace" size="2"&gt;Which the SAS processor would actually see as&lt;/FONT&gt;&lt;/P&gt;
&lt;PRE&gt;result= 2 + 2;run;
data p;
  result= &amp;amp;x;
run;&lt;/PRE&gt;
&lt;P&gt;&lt;FONT face="SAS Monospace" size="2"&gt;because of the first ; in the MySuma macro.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="SAS Monospace" size="2"&gt;So the SAS processor would almost certainly generate an error message because "result=2+2;" without a data step doesn't mean much.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="SAS Monospace" size="2"&gt;But more goes one. The data step using &amp;amp;x resolves to:&lt;/FONT&gt;&lt;/P&gt;
&lt;PRE&gt;data p;
  result= 5 - data p;
run;&lt;/PRE&gt;
&lt;P&gt;&lt;FONT face="SAS Monospace" size="2"&gt;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:&lt;/FONT&gt;&lt;/P&gt;
&lt;PRE&gt;ERROR 22-322: Syntax error, expecting one of the following: !, !!, &amp;amp;, *, **, +, -, /, &amp;lt;, &amp;lt;=, &amp;lt;&amp;gt;,
              =, &amp;gt;, &amp;gt;&amp;lt;, &amp;gt;=, AND, EQ, GE, GT, IN, LE, LT, MAX, MIN, NE, NG, NL, NOTIN, OR, ^=, |,
              ||, ~=.
&lt;/PRE&gt;
&lt;P&gt;because there was no operator indicating what should be done with that p.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Coding style comment: There is no obvious reason for the creation of variable x as you are using it. More commonly I would expect:&lt;/P&gt;
&lt;P&gt;data p;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; result= &amp;amp;number1&amp;nbsp;+ &amp;amp;number2; &lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;Though you might want to consider: result= sum(&amp;amp;number1, &amp;amp;number2); if you want a numeric value when one of the number variables is missing.&lt;/P&gt;</description>
      <pubDate>Thu, 11 Apr 2019 16:08:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Passing-as-variable-to-a-macro-the-result-of-another-macro/m-p/550312#M152777</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2019-04-11T16:08:57Z</dc:date>
    </item>
  </channel>
</rss>

