<?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/292429#M60696</link>
    <description>&lt;P&gt;Hi:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; One possible reason to use a macro invocation call inside another macro call is to isolate code and make it re-usable. For example, in the 3 macro definitions below,&lt;/P&gt;
&lt;P&gt;&lt;IMG src="https://communities.sas.com/t5/image/serverpage/image-id/4635iF661BEB88626FBB0/image-size/original?v=v2&amp;amp;px=-1" alt="macro_examp.png" title="macro_examp.png" border="0" /&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;There are 3 macro definitions: %dodaily, %doonce and %wkrept. Of course, the code in %dodaily and %doonce is very simple and could instead represent more complicated processes. However, both %dodaily and %doonce are invoked inside the %wkrept macro program. The final invocation %wkrept will cause the other two macro programs to be retrieved, expanded and appropriate code generated by the macro processor. It is still all generating code. This approach however, especially if the macro definitions are stored in an autocall or compiled macro library would allow people who only want the&amp;nbsp; steps in %dodaily to just run that macro and people who only want the steps in %doonce to just run that macro and people who need the more complicated logic an use the %wkrept and pick the day they want the %doonce code to be used by specifying a parameter for &amp;amp;want.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; Working this way, the code is isolated. Each macro program can be maintained separately if necessary and invoked separately, allowing flexibility in using the code. And although the same type of flexibility might be provided with %include functionality, many sites prefer autocall macro programs and/or stored compiled macro programs for the added security and central storage location, accessible to everyone.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;cynthia&lt;/P&gt;</description>
    <pubDate>Thu, 18 Aug 2016 12:42:53 GMT</pubDate>
    <dc:creator>Cynthia_sas</dc:creator>
    <dc:date>2016-08-18T12:42:53Z</dc:date>
    <item>
      <title>Macro within a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-within-a-macro/m-p/292410#M60692</link>
      <description>&lt;P&gt;How to execute a macro within a macro? Appreciate if someone give one example.&lt;/P&gt;</description>
      <pubDate>Thu, 18 Aug 2016 11:42:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-within-a-macro/m-p/292410#M60692</guid>
      <dc:creator>Babloo</dc:creator>
      <dc:date>2016-08-18T11:42:30Z</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/292415#M60693</link>
      <description>&lt;P&gt;Right, top question again. &amp;nbsp;Why? &amp;nbsp;There is rarely a need to use macros at all, and there is less reason to ever put macros within macros. &amp;nbsp;Remember macros generate text, nothing more, that text should be Base SAS code which gets executed. &amp;nbsp;If your doing coding then work in Base SAS, later on if there are repeating code blocks change them to macro. &amp;nbsp;There is never ever a need to create macro code to replace Base SAS coding. &amp;nbsp;Technically you can use %&amp;lt;macro name&amp;gt;; and &amp;amp;&amp;lt;macro variable&amp;gt; much like you can anywhere else, though if you don't know what you are doing you will just create problems for yourself. &amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you provide exmaples of your input data (datastep), and what you want to do I can show how to do it in Base SAS.&lt;/P&gt;</description>
      <pubDate>Thu, 18 Aug 2016 11:57:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-within-a-macro/m-p/292415#M60693</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2016-08-18T11:57:36Z</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/292428#M60695</link>
      <description>&lt;P&gt;You can call a macro within a macro. &amp;nbsp;Here is a trivial example.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro one(dsn);
proc print data=&amp;amp;dsn; run;
%mend one;
%macro two(dslist);
%local  i;
%do i=1 %to %sysfunc(countw(&amp;amp;dslist));
  %one(%scan(&amp;amp;dslist,&amp;amp;i));
%end;
%mend two;

%two(sashelp.class sashelp.cars);
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;You can even call the same macro recursively. &amp;nbsp;Just make sure that the recursion ends.&lt;/P&gt;</description>
      <pubDate>Thu, 18 Aug 2016 12:39:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-within-a-macro/m-p/292428#M60695</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2016-08-18T12:39:57Z</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/292429#M60696</link>
      <description>&lt;P&gt;Hi:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; One possible reason to use a macro invocation call inside another macro call is to isolate code and make it re-usable. For example, in the 3 macro definitions below,&lt;/P&gt;
&lt;P&gt;&lt;IMG src="https://communities.sas.com/t5/image/serverpage/image-id/4635iF661BEB88626FBB0/image-size/original?v=v2&amp;amp;px=-1" alt="macro_examp.png" title="macro_examp.png" border="0" /&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;There are 3 macro definitions: %dodaily, %doonce and %wkrept. Of course, the code in %dodaily and %doonce is very simple and could instead represent more complicated processes. However, both %dodaily and %doonce are invoked inside the %wkrept macro program. The final invocation %wkrept will cause the other two macro programs to be retrieved, expanded and appropriate code generated by the macro processor. It is still all generating code. This approach however, especially if the macro definitions are stored in an autocall or compiled macro library would allow people who only want the&amp;nbsp; steps in %dodaily to just run that macro and people who only want the steps in %doonce to just run that macro and people who need the more complicated logic an use the %wkrept and pick the day they want the %doonce code to be used by specifying a parameter for &amp;amp;want.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; Working this way, the code is isolated. Each macro program can be maintained separately if necessary and invoked separately, allowing flexibility in using the code. And although the same type of flexibility might be provided with %include functionality, many sites prefer autocall macro programs and/or stored compiled macro programs for the added security and central storage location, accessible to everyone.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;cynthia&lt;/P&gt;</description>
      <pubDate>Thu, 18 Aug 2016 12:42:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-within-a-macro/m-p/292429#M60696</guid>
      <dc:creator>Cynthia_sas</dc:creator>
      <dc:date>2016-08-18T12:42:53Z</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/439381#M109637</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am giving &amp;amp;cpd value externally. For suppose I am doing&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%let cpd=50; to run the macro.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;U&gt;I am getting the below error:&lt;/U&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Line generated by the invoked macro "LOADCPDDATA".&lt;BR /&gt;3 select CPDCOBRS_RTRN_VLU_1 into :&amp;amp;FieldName&lt;BR /&gt;3 ! from cpdlogic where CPDCOBRS_GRP_T=&amp;amp;Group; quit;&lt;BR /&gt;--------&lt;BR /&gt;22&lt;BR /&gt;76&lt;BR /&gt;ERROR 22-322: Syntax error, expecting one of the following: ',', -, FROM, SEPARATED, THROUGH,&lt;BR /&gt;THRU, TRIMMED.&lt;/P&gt;&lt;P&gt;ERROR 76-322: Syntax error, statement will be ignored.&lt;/P&gt;</description>
      <pubDate>Thu, 22 Feb 2018 17:38:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-within-a-macro/m-p/439381#M109637</guid>
      <dc:creator>sai99</dc:creator>
      <dc:date>2018-02-22T17:38:39Z</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/439382#M109638</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;libname loaddata 'C:\Users\lidpupm\Desktop\Provider Directory\Modular_configuration';


data cpdlogic (sortedby=CPDCOBRS_VAR_NM CPDCOBRS_GRP_T);
 set loaddata.cpdlogic;
	 if CPDCOBRS_CATG_T='CPD_logic' then do;
		FieldCounterChar = left(put(_n_,6.));
		FieldCounter = _n_;
		output;
		end;
		run;

		proc sql noprint;
		select max(FieldCounter) into :TotalFieldCount
		from cpdlogic ;
		quit;
		%put &amp;amp;TotalFieldCount;

		
%global &amp;amp;FieldName;
	%macro LoadCPDData (Group,Field_Num);
		 proc sql noprint; 
	        select CPDCOBRS_VAR_NM into :FieldName
			from cpdlogic
			where CPDCOBRS_GRP_T=&amp;amp;Group
	  		  and FieldCounter=&amp;amp;Field_Num;
		  quit; 

		  
	%put [&amp;amp;Field_Num];

			proc sql noprint;
			select CPDCOBRS_RTRN_VLU_1 into :&amp;amp;FieldName 
			from cpdlogic 
			where CPDCOBRS_GRP_T=&amp;amp;Group;
		quit;	
    %mend LoadCPDData;

	
	%macro loading;
	%if &amp;amp;TotalFieldCount &amp;gt; 1 %then %do;
	%do i=1 %to &amp;amp;TotalFieldCount;
		%LoadCPDData(&amp;amp;cpd,&amp;amp;i);
	%end;
	%end;
	%mend loading;

		

	%macro UnLoadCPDData (Group,Field_Num);
		 proc sql noprint; 
	        select 
	 			CPDCOBRS_VAR_NM into :FieldName
			from cpdlogic
			where CPDCOBRS_GRP_T=&amp;amp;Group
	  		  and FieldCounter=&amp;amp;Field_Num
			;
		  quit; 
 
	%put &amp;amp;Field_Num;

	%global &amp;amp;FieldName;
	
		proc sql noprint;
			select 
			' ' into :&amp;amp;FieldName 
			from cpdlogic 
			where CPDCOBRS_GRP_T=&amp;amp;Group
		;
		quit;	
    %mend UnLoadCPDData;

	options mlogic;
	%macro unloading;
	%if &amp;amp;TotalFieldCount &amp;gt; 1 %then %do;
	%do i=1 %to &amp;amp;TotalFieldCount;
		%UnLoadCPDData(&amp;amp;cpd,&amp;amp;i);
	%end;
	%end;
	%mend unloading;

	/*****END**********/

	&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am giving &amp;amp;cpd value externally. For suppose I am doing&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%let cpd=50; to run the macro.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;U&gt;I am getting the below error:&lt;/U&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Line generated by the invoked macro "LOADCPDDATA".&lt;BR /&gt;3 select CPDCOBRS_RTRN_VLU_1 into :&amp;amp;FieldName&lt;BR /&gt;3 ! from cpdlogic where CPDCOBRS_GRP_T=&amp;amp;Group; quit;&lt;BR /&gt;--------&lt;BR /&gt;22&lt;BR /&gt;76&lt;BR /&gt;ERROR 22-322: Syntax error, expecting one of the following: ',', -, FROM, SEPARATED, THROUGH,&lt;BR /&gt;THRU, TRIMMED.&lt;/P&gt;&lt;P&gt;ERROR 76-322: Syntax error, statement will be ignored.&lt;/P&gt;</description>
      <pubDate>Thu, 22 Feb 2018 17:40:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-within-a-macro/m-p/439382#M109638</guid>
      <dc:creator>sai99</dc:creator>
      <dc:date>2018-02-22T17:40:12Z</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/439391#M109645</link>
      <description>&lt;P&gt;Your code seems incomplete as there are no calls to execute any of the macro definitions.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 22 Feb 2018 18:11:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-within-a-macro/m-p/439391#M109645</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2018-02-22T18:11:36Z</dc:date>
    </item>
  </channel>
</rss>

