<?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: macro within a macro in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/macro-within-a-macro/m-p/349036#M273519</link>
    <description>&lt;P&gt;Its really not very good programming. &amp;nbsp;Several reasons. &amp;nbsp;First the macro really doesn't do anything other than split your data out - which is rarely a good idea anyway, but in doin git this way obfuscates the code somewhat:&lt;/P&gt;
&lt;PRE&gt;data test_1 test_2 test_3;
  set kk;
  select(a);
    when 1 output test_1;
    when 2 output test_2;
    otherwise output test_3;
  end;
run;&lt;/PRE&gt;
&lt;P&gt;Is effectively the same.&lt;/P&gt;
&lt;P&gt;The code is also incorrect as you give it, unless there is a variable called a in the dataset kk, it will not work, I assumed you meant &amp;amp;a?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Now if you add a %put statement into the macro you will clearly see what the %let is doing, it is building a list of space delimited dataset names e.g.&lt;/P&gt;
&lt;P&gt;test_1 test_2 test_3&lt;/P&gt;
&lt;P&gt;I assume this is so you can use that list to process further - again, because of the programming choice to split your data up you then have to start adding lots of code to try to process multiple datasets - more work, harder to maintain etc. &amp;nbsp;Use one dataset and use by group processing - this is what it is for,&lt;/P&gt;
&lt;P&gt;Finally, you are telling SAS that the hi macro is global, and then adjusting it within a macro. &amp;nbsp;In this simple example it may work, but doing something like this can lead to very hard to debug problems later on - when code may not even be in the same file etc. &amp;nbsp;Again, not a good idea. &amp;nbsp;Unless there is a good reason, follow encapsulisation and keep scope local.&lt;/P&gt;
&lt;P&gt;This is the program (with some readability added - very important):&lt;/P&gt;
&lt;PRE&gt;%let hi=;
%macro hi(a);
  data test_&amp;amp;a.;
    set sashelp.class;
    where age=&amp;amp;a.;
  run;
   
  %let hi=&amp;amp;hi test_&amp;amp;a.;
  %put &amp;amp;hi.;
%mend;
 
%put &amp;amp;hi.;

%hi(12)
%hi(15)
%hi(16)
 &lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 11 Apr 2017 09:40:47 GMT</pubDate>
    <dc:creator>RW9</dc:creator>
    <dc:date>2017-04-11T09:40:47Z</dc:date>
    <item>
      <title>macro within a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/macro-within-a-macro/m-p/349001#M273517</link>
      <description>&lt;P&gt;I have a trouble understanding a code like below:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%let hi=;&lt;/P&gt;&lt;P&gt;%macro hi(a);&lt;/P&gt;&lt;P&gt;data test_&amp;amp;a;&lt;/P&gt;&lt;P&gt;set kk;&lt;/P&gt;&lt;P&gt;where class=a;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;U&gt;&lt;STRONG&gt;%let hi=&amp;amp;hi test_&amp;amp;a;&lt;/STRONG&gt;&lt;/U&gt;&lt;/P&gt;&lt;P&gt;%mend;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%hi(1)&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;%hi(2)&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;%hi(3)&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;what does the code in underline do?&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 11 Apr 2017 07:10:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/macro-within-a-macro/m-p/349001#M273517</guid>
      <dc:creator>irisirissy</dc:creator>
      <dc:date>2017-04-11T07:10:22Z</dc:date>
    </item>
    <item>
      <title>Re: macro within a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/macro-within-a-macro/m-p/349004#M273518</link>
      <description>&lt;P&gt;It is simple macro variable definition. it Overrides the macro variable set at the top by the same name of hi and adds another string containing the string 'test_' and the macro variable a, which is the parameter given to the macro in the parenthesis in hi(a).&lt;/P&gt;</description>
      <pubDate>Tue, 11 Apr 2017 07:37:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/macro-within-a-macro/m-p/349004#M273518</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2017-04-11T07:37:43Z</dc:date>
    </item>
    <item>
      <title>Re: macro within a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/macro-within-a-macro/m-p/349036#M273519</link>
      <description>&lt;P&gt;Its really not very good programming. &amp;nbsp;Several reasons. &amp;nbsp;First the macro really doesn't do anything other than split your data out - which is rarely a good idea anyway, but in doin git this way obfuscates the code somewhat:&lt;/P&gt;
&lt;PRE&gt;data test_1 test_2 test_3;
  set kk;
  select(a);
    when 1 output test_1;
    when 2 output test_2;
    otherwise output test_3;
  end;
run;&lt;/PRE&gt;
&lt;P&gt;Is effectively the same.&lt;/P&gt;
&lt;P&gt;The code is also incorrect as you give it, unless there is a variable called a in the dataset kk, it will not work, I assumed you meant &amp;amp;a?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Now if you add a %put statement into the macro you will clearly see what the %let is doing, it is building a list of space delimited dataset names e.g.&lt;/P&gt;
&lt;P&gt;test_1 test_2 test_3&lt;/P&gt;
&lt;P&gt;I assume this is so you can use that list to process further - again, because of the programming choice to split your data up you then have to start adding lots of code to try to process multiple datasets - more work, harder to maintain etc. &amp;nbsp;Use one dataset and use by group processing - this is what it is for,&lt;/P&gt;
&lt;P&gt;Finally, you are telling SAS that the hi macro is global, and then adjusting it within a macro. &amp;nbsp;In this simple example it may work, but doing something like this can lead to very hard to debug problems later on - when code may not even be in the same file etc. &amp;nbsp;Again, not a good idea. &amp;nbsp;Unless there is a good reason, follow encapsulisation and keep scope local.&lt;/P&gt;
&lt;P&gt;This is the program (with some readability added - very important):&lt;/P&gt;
&lt;PRE&gt;%let hi=;
%macro hi(a);
  data test_&amp;amp;a.;
    set sashelp.class;
    where age=&amp;amp;a.;
  run;
   
  %let hi=&amp;amp;hi test_&amp;amp;a.;
  %put &amp;amp;hi.;
%mend;
 
%put &amp;amp;hi.;

%hi(12)
%hi(15)
%hi(16)
 &lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 11 Apr 2017 09:40:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/macro-within-a-macro/m-p/349036#M273519</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2017-04-11T09:40:47Z</dc:date>
    </item>
  </channel>
</rss>

