<?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: call a macro in another macro in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/call-a-macro-in-another-macro/m-p/95396#M20090</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;It really depends on what type code macro B is generating. If it is just part of a statement or a few statements then you can call the macro within a data step and it will generate statements that will become part of that data step.&amp;nbsp; In that case you could pass in the variable NAMES to the macro and the generated code will reference the variable names, so when that code is run as part of a data step the values of the named variables will be used. &lt;/P&gt;&lt;P&gt;A trivial example:&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&lt;BR /&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;%macro sum(a,b); &amp;amp;a+&amp;amp;b %mend sum;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;data x ; set y(keep=n1 n2);&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&amp;nbsp;&amp;nbsp; z=%sum(n1,n2);&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;run;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Also if the macro is only executing macro code and does not generate any SAS statements at all then you could call it from within a data step using the RESOLVE() function. In that case you would pass in the values of the dataset variables.&lt;/P&gt;&lt;P&gt;Example:&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;%macro sum(a,b); %eval(&amp;amp;a+&amp;amp;b) %mend sum;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;data x ; set y(keep=n1 n2);&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&amp;nbsp;&amp;nbsp; z=input(resolve('%sum('||catx(','n1,n2),')'),best.);&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;run;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;But if the macro is generating complete steps (proc or data) then you cannot call it within a data step.&amp;nbsp; You might want to push the macro call onto the stack to execute after the current step ends using CALL EXECUTE.&amp;nbsp; Or perhaps generate the call as code in an output file and then %INC the generated code.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Mon, 10 Dec 2012 23:42:36 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2012-12-10T23:42:36Z</dc:date>
    <item>
      <title>call a macro in another macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/call-a-macro-in-another-macro/m-p/95389#M20083</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I need to call a macro( B ) in another macro(A). in macro A, two variables x1, x2 are created and used as parameters for macro B.&amp;nbsp; Thanks for your help in advance.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;%macro B (param1, param2);&lt;/P&gt;&lt;P&gt;Code;&lt;/P&gt;&lt;P&gt;%mend B;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;%macro A(x,y);&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Code… to create x1, x2;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; %B(x1,x2);&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/P&gt;&lt;P&gt;%mend A;&lt;/P&gt;&lt;P&gt;%A(n1,n2);&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 10 Dec 2012 16:15:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/call-a-macro-in-another-macro/m-p/95389#M20083</guid>
      <dc:creator>jojo</dc:creator>
      <dc:date>2012-12-10T16:15:27Z</dc:date>
    </item>
    <item>
      <title>Re: call a macro in another macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/call-a-macro-in-another-macro/m-p/95390#M20084</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;How you create x1 and x2, and possibly what they look like such as a single numeric, single string or multiple numerics or strings are likely to have an impact on how you get the values ready for the call to %B. Will X1 and X2 be in a data set? If so will it have multiple rows and does %B need to be called for each set of values?&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 10 Dec 2012 16:20:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/call-a-macro-in-another-macro/m-p/95390#M20084</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2012-12-10T16:20:21Z</dc:date>
    </item>
    <item>
      <title>Re: call a macro in another macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/call-a-macro-in-another-macro/m-p/95391#M20085</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Did that give you errors?&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 10 Dec 2012 16:20:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/call-a-macro-in-another-macro/m-p/95391#M20085</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2012-12-10T16:20:50Z</dc:date>
    </item>
    <item>
      <title>Re: call a macro in another macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/call-a-macro-in-another-macro/m-p/95392#M20086</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Two dataset variables or two macro variables.&lt;/P&gt;&lt;P&gt;To reference the value of macro variable you need to preface the name with an ampersand (&amp;amp;).&lt;/P&gt;&lt;P&gt;Also your code will probably by clearer if you use the parameter names in the macro call (note that you can reference positional parameters by name in a macro call).&lt;/P&gt;&lt;P&gt;So in your example the call to macro B would probably look like:&amp;nbsp; %B(param1=&amp;amp;x1,param2=&amp;amp;x2)&lt;/P&gt;&lt;P&gt;And then when B is running any reference to &amp;amp;PARAM1 will be replaced with the value that that was supplied when B was called.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 10 Dec 2012 16:28:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/call-a-macro-in-another-macro/m-p/95392#M20086</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2012-12-10T16:28:55Z</dc:date>
    </item>
    <item>
      <title>Re: call a macro in another macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/call-a-macro-in-another-macro/m-p/95393#M20087</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;two dataset variables. the following code gives errors.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;%macro B (param1, param2);&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; Code;&lt;/P&gt;&lt;P&gt;%mend B;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;%macro A(param1,param2);&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Code…&amp;nbsp; ***&amp;nbsp; here to create a dataset and several variables. two sets of dataset varibles x1, x2, y1, y2 (numerical value) are used for macro B;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; %B(x1,x2);&amp;nbsp; *** call macro B twice;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; %B(y1,y2);&amp;nbsp;&amp;nbsp; &lt;/P&gt;&lt;P&gt;%mend A;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;%A(param=n1,param2=n2);&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 10 Dec 2012 20:47:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/call-a-macro-in-another-macro/m-p/95393#M20087</guid>
      <dc:creator>jojo</dc:creator>
      <dc:date>2012-12-10T20:47:48Z</dc:date>
    </item>
    <item>
      <title>Re: call a macro in another macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/call-a-macro-in-another-macro/m-p/95394#M20088</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;You cannot call a macro using % with values from a table.&lt;/P&gt;&lt;P&gt;Attaching the the full code/log would make this clear.&lt;/P&gt;&lt;P&gt;To call a macro using values from a table, use call execute.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 10 Dec 2012 22:05:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/call-a-macro-in-another-macro/m-p/95394#M20088</guid>
      <dc:creator>LinusH</dc:creator>
      <dc:date>2012-12-10T22:05:03Z</dc:date>
    </item>
    <item>
      <title>Re: call a macro in another macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/call-a-macro-in-another-macro/m-p/95395#M20089</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Within your macro A you need to decide how to pass the values to macro B. The two most likely candicates are to create macro variables in a dataset and pass them to the macro hard coded or in a more flexible approach use Call execute.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Assuming the data set that has the data values to pass is called HAVE and the variables in HAVE with the values you want to pass are named A and b. Then the last part of your macro A might look like:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Data _null_;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; set Have;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; /* this assumes that there are only 2 records in Have. You can place an IF clause to make the call conditional such as "IF Z &amp;gt; 3 then " if you only wanted to call macro B when the value of hypothetical variable was&amp;nbsp; greater than 3, which is what makes this aproach very flexible.*/&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; call execute ('%b(' A ||','|| B || ')' );&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;Note that the part within the parantheses of Call Execute is just a string. The string could be created in any manner of ways. Also this uses the default formatting for A and B, which you haven't provided a default will be a Bestw.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 10 Dec 2012 22:09:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/call-a-macro-in-another-macro/m-p/95395#M20089</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2012-12-10T22:09:50Z</dc:date>
    </item>
    <item>
      <title>Re: call a macro in another macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/call-a-macro-in-another-macro/m-p/95396#M20090</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;It really depends on what type code macro B is generating. If it is just part of a statement or a few statements then you can call the macro within a data step and it will generate statements that will become part of that data step.&amp;nbsp; In that case you could pass in the variable NAMES to the macro and the generated code will reference the variable names, so when that code is run as part of a data step the values of the named variables will be used. &lt;/P&gt;&lt;P&gt;A trivial example:&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&lt;BR /&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;%macro sum(a,b); &amp;amp;a+&amp;amp;b %mend sum;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;data x ; set y(keep=n1 n2);&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&amp;nbsp;&amp;nbsp; z=%sum(n1,n2);&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;run;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Also if the macro is only executing macro code and does not generate any SAS statements at all then you could call it from within a data step using the RESOLVE() function. In that case you would pass in the values of the dataset variables.&lt;/P&gt;&lt;P&gt;Example:&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;%macro sum(a,b); %eval(&amp;amp;a+&amp;amp;b) %mend sum;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;data x ; set y(keep=n1 n2);&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&amp;nbsp;&amp;nbsp; z=input(resolve('%sum('||catx(','n1,n2),')'),best.);&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;run;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;But if the macro is generating complete steps (proc or data) then you cannot call it within a data step.&amp;nbsp; You might want to push the macro call onto the stack to execute after the current step ends using CALL EXECUTE.&amp;nbsp; Or perhaps generate the call as code in an output file and then %INC the generated code.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 10 Dec 2012 23:42:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/call-a-macro-in-another-macro/m-p/95396#M20090</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2012-12-10T23:42:36Z</dc:date>
    </item>
  </channel>
</rss>

