<?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: How to include a data step macro inside another macro with %include? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-include-a-data-step-macro-inside-another-macro-with/m-p/743444#M232775</link>
    <description>&lt;P&gt;I did try this in the first instance to include a %local loop in both #6, and the script calling it,&amp;nbsp; and also made sure the loop index names were different, not using i or j in both.&amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Mon, 24 May 2021 23:55:53 GMT</pubDate>
    <dc:creator>windlove</dc:creator>
    <dc:date>2021-05-24T23:55:53Z</dc:date>
    <item>
      <title>How to include a data step macro inside another macro with %include?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-include-a-data-step-macro-inside-another-macro-with/m-p/743240#M232654</link>
      <description>&lt;P&gt;I have a few programs doing data preparations. One of them has a macro wrap on a %do loop to subset the data.&amp;nbsp;When I try to run all those programs inside another macro with %include and %do loop, that data program with macro would always be skipped.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here "6. program.sas" is the program with macro wrap., and %run_test always skips it.&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;	%let program1 = %str(1. program.sas);
	%let program2 = %str(2. program.sas);
	%let program3 = %str(3. program.sas);
	%let program4 = %str(4. program.sas);
	%let program5 = %str(5. program.sas);

	/* report programs */
	%let program6 = %str(6. program.sas);
	%let program7 = %str(7. program.sas);

	%macro run_test;
		%local i;
		%do i = 1 %to 7;
			%include "%sysfunc(catx(\, &amp;amp;sasDir., &amp;amp;&amp;amp;program&amp;amp;i))";
		%end;

	%mend;
	%run_test;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;However, if I have the previous 5 programs run already, and the %do loop start from the 6th program. Then it would run successfully.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;	%macro run_test;
		%local i;
		%do i = 6 %to 7;
			%include "%sysfunc(catx(\, &amp;amp;sasDir., &amp;amp;&amp;amp;program&amp;amp;i))";
		%end;

	%mend;
	%run_test;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;What would be the reason for this, and how can I resolve this?&lt;/P&gt;</description>
      <pubDate>Sun, 23 May 2021 16:15:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-include-a-data-step-macro-inside-another-macro-with/m-p/743240#M232654</guid>
      <dc:creator>windlove</dc:creator>
      <dc:date>2021-05-23T16:15:18Z</dc:date>
    </item>
    <item>
      <title>Re: How to include a data step macro inside another macro with %include?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-include-a-data-step-macro-inside-another-macro-with/m-p/743242#M232656</link>
      <description>&lt;P&gt;&lt;FONT size="2"&gt;oooops.&amp;nbsp;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT size="2"&gt;After posting the question here, suddenly problem solved.&amp;nbsp;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT size="2"&gt;I just move the %include statement&amp;nbsp; after a %goto. My actual program looks like this&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT size="2"&gt;All the programs run successfully, but the 6th program now cannot be resolved in &amp;amp;&amp;amp;program&amp;amp;i.&amp;nbsp;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT size="2"&gt;I have 1-5, and 7, printed out as in&lt;/FONT&gt;&lt;/P&gt;&lt;PRE&gt;&lt;FONT size="2"&gt;&lt;CODE class=" language-sas"&gt;put #3 @5 " The SAS program '&amp;amp;&amp;amp;program&amp;amp;i.' is in the right place";&lt;/CODE&gt;&lt;/FONT&gt;&lt;/PRE&gt;&lt;P&gt;&lt;FONT size="2"&gt;, but not the 6th program.&amp;nbsp;&lt;/FONT&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro runProgram();

		%local i ;
		%do i = 1 %to 7;
			%let runfile = %sysfunc(catx(\, &amp;amp;sasDir., &amp;amp;&amp;amp;program&amp;amp;i));
			%if %sysfunc(fileexist("&amp;amp;runfile.")) = 0 %then %do;
				data _null_;
					file print;
					put #1 @5 "Error **************************************************************";
		        	put #3 @5 "Error The SAS programs  '&amp;amp;&amp;amp;program&amp;amp;i.'  does not exist"  ;
					put #4 @5 "Error Please check whether the program name or the the directory is correct";
					put #6 @5 "Error *************************************************************";
				run;
				%goto endRun;	
			%end;
			%else %do;
				data _null_ ;
					file print;
					put #1 @5 "*******************************************************";
					put #3 @5 " The SAS program '&amp;amp;&amp;amp;program&amp;amp;i.' is in the right place";
					put #5 @5 "******************************************************";
				run;
				%goto runProg;
				%runProg:
					%include "&amp;amp;runfile.";
			%end;
		%end;

		%endRun:
	%mend; 
	%runProgram;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 23 May 2021 17:00:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-include-a-data-step-macro-inside-another-macro-with/m-p/743242#M232656</guid>
      <dc:creator>windlove</dc:creator>
      <dc:date>2021-05-23T17:00:32Z</dc:date>
    </item>
    <item>
      <title>Re: How to include a data step macro inside another macro with %include?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-include-a-data-step-macro-inside-another-macro-with/m-p/743252#M232662</link>
      <description>&lt;P&gt;Hello&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/133203"&gt;@windlove&lt;/a&gt;&amp;nbsp;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have no idea why your macro stumbles upon the 6th program??&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can consider alternative code, like this one:&lt;/P&gt;
&lt;P&gt;Note the comment sections (they contain some important remarks)!&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* IMPORTANT !! This program assumes that the alphabetical sort order of the     */
/* .sas programs is also the order in which the programs have to be submitted !! */

%LET directory=%STR(C:\abc);

/* Collect all .sas programs (files) in a dataset named WORK.mySASprograms_inDir */
data mySASprograms_inDir;
length fref $8 fnamesas $400;
did = filename(fref,"&amp;amp;directory.");
did = dopen(fref);
do i = 1 to dnum(did);
  fnamesas = dread(did,i);
  fnamesas = "&amp;amp;directory"!!'\'!!strip(fnamesas);
  if upcase(scan(fnamesas,-1,'.')) = 'SAS' then output;
end;
did = dclose(did);
did = filename(fref);
keep fnamesas;
run;

/* delete the .sas programs you do not want */
data mySASprograms_inDir;
 set mySASprograms_inDir;
 where fnamesas NOT CONTAINS 'PATHJSCORECODE.sas';
run;

proc sort data=mySASprograms_inDir; by fnamesas; run;

/* How many SAS programs are there?? */
data _NULL_;
 if 0 then set mySASprograms_inDir nobs=count;
 call symput('numobs',strip(put(count,8.)));
 STOP;
run;
%PUT &amp;amp;=numobs;

/* EXECUTE (SUBMIT) the first 2 .sas programs */
options source2;
data _NULL_;
 set mySASprograms_inDir;
 if _N_ &amp;lt; 3 then call execute('%INCLUDE "' !! strip(fnamesas) !! '";');
run;
/* end of program */&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Koen&lt;/P&gt;</description>
      <pubDate>Sun, 23 May 2021 18:23:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-include-a-data-step-macro-inside-another-macro-with/m-p/743252#M232662</guid>
      <dc:creator>sbxkoenk</dc:creator>
      <dc:date>2021-05-23T18:23:19Z</dc:date>
    </item>
    <item>
      <title>Re: How to include a data step macro inside another macro with %include?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-include-a-data-step-macro-inside-another-macro-with/m-p/743283#M232680</link>
      <description>&lt;P&gt;Most likely program #5 is setting the macro variable I to the value 6 so the %DO loop increments it to 7 and so skips the sixth.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The solution is to either fix program number 5 (or your %DO loop) to not modify the macro variable used for the loop.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Or don't use a macro for the loop.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
  length filename $256 ;
  do i=1 to 7;
    filename=catx('\',symget('sasdir'),symget(cats('program',i)));
    call execute('%include '||quote(trim(filename)));
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 24 May 2021 01:48:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-include-a-data-step-macro-inside-another-macro-with/m-p/743283#M232680</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-05-24T01:48:19Z</dc:date>
    </item>
    <item>
      <title>Re: How to include a data step macro inside another macro with %include?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-include-a-data-step-macro-inside-another-macro-with/m-p/743298#M232687</link>
      <description>&lt;P&gt;Thanks for both suggestions. But unfortunately it's not working.&lt;/P&gt;&lt;P&gt;I think it has something to do with the macro wrapper inside #6 program, in which it has the steps like below. This may cause non-compilation of #6 within another %do loop in another macro.&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data steps;
x
y
z
%macro data;
a
b
c
%mend;
%data;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I have fixed the problem by wrapping all 7 programs with a macro, and keep the macro name same as the file name. Such that, I can do something like. Now it is working without any issues.&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%do i = 1 %to 7;
	%let runfile = %sysfunc(catx(\, &amp;amp;sasDir., &amp;amp;&amp;amp;program&amp;amp;i));
    %let macroName = %scan(&amp;amp;&amp;amp;program&amp;amp;i., 2, " .");
	%include "&amp;amp;runfile.";
	%&amp;amp;macroName.;	
%end;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 24 May 2021 03:58:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-include-a-data-step-macro-inside-another-macro-with/m-p/743298#M232687</guid>
      <dc:creator>windlove</dc:creator>
      <dc:date>2021-05-24T03:58:36Z</dc:date>
    </item>
    <item>
      <title>Re: How to include a data step macro inside another macro with %include?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-include-a-data-step-macro-inside-another-macro-with/m-p/743400#M232741</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/133203"&gt;@windlove&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Thanks for both suggestions. But unfortunately it's not working.&lt;/P&gt;
&lt;P&gt;I think it has something to do with the macro wrapper inside #6 program, in which it has the steps like below. This may cause non-compilation of #6 within another %do loop in another macro.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data steps;
x
y
z
%macro data;
a
b
c
%mend;
%data;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I have fixed the problem by wrapping all 7 programs with a macro, and keep the macro name same as the file name. Such that, I can do something like. Now it is working without any issues.&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%do i = 1 %to 7;
	%let runfile = %sysfunc(catx(\, &amp;amp;sasDir., &amp;amp;&amp;amp;program&amp;amp;i));
    %let macroName = %scan(&amp;amp;&amp;amp;program&amp;amp;i., 2, " .");
	%include "&amp;amp;runfile.";
	%&amp;amp;macroName.;	
%end;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Are you attempting to use a MACRO as datalines?&lt;/P&gt;
&lt;P&gt;I would think anything as static as your example of the %data macro would be better off in a permanent data set and used from there. Or have you "simplified" past the point of usability?&lt;/P&gt;</description>
      <pubDate>Mon, 24 May 2021 17:09:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-include-a-data-step-macro-inside-another-macro-with/m-p/743400#M232741</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2021-05-24T17:09:09Z</dc:date>
    </item>
    <item>
      <title>Re: How to include a data step macro inside another macro with %include?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-include-a-data-step-macro-inside-another-macro-with/m-p/743416#M232755</link>
      <description>&lt;P&gt;Converting the code inside the include file will only work if you also include a %LOCAL statement to make sure the loop counter used in the calling program not overwritten.&lt;/P&gt;
&lt;P&gt;So the %LOCAL I statement in this definition of %PROGRAM5 is what is preventing %PROGRAM6 from being skipped.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro program5 ;
   %local i;
....
  %let i=6 ;
...
%mend program5;
...
%do i=5 %to 7;
  %program&amp;amp;i ;
%end;

&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 24 May 2021 18:48:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-include-a-data-step-macro-inside-another-macro-with/m-p/743416#M232755</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-05-24T18:48:29Z</dc:date>
    </item>
    <item>
      <title>Re: How to include a data step macro inside another macro with %include?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-include-a-data-step-macro-inside-another-macro-with/m-p/743444#M232775</link>
      <description>&lt;P&gt;I did try this in the first instance to include a %local loop in both #6, and the script calling it,&amp;nbsp; and also made sure the loop index names were different, not using i or j in both.&amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 24 May 2021 23:55:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-include-a-data-step-macro-inside-another-macro-with/m-p/743444#M232775</guid>
      <dc:creator>windlove</dc:creator>
      <dc:date>2021-05-24T23:55:53Z</dc:date>
    </item>
    <item>
      <title>Re: How to include a data step macro inside another macro with %include?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-include-a-data-step-macro-inside-another-macro-with/m-p/743445#M232776</link>
      <description>&lt;P&gt;No, not using macro as datalines. It was just an example to show how my program was structured. The macro is just a part of #6, which is doing some data processing in the middle of the whole program.&amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 24 May 2021 23:59:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-include-a-data-step-macro-inside-another-macro-with/m-p/743445#M232776</guid>
      <dc:creator>windlove</dc:creator>
      <dc:date>2021-05-24T23:59:23Z</dc:date>
    </item>
    <item>
      <title>Re: How to include a data step macro inside another macro with %include?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-include-a-data-step-macro-inside-another-macro-with/m-p/743453#M232780</link>
      <description>&lt;P&gt;The problems you are having are a good argument for not using nested macros.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Why not consider a different approach completely? What about creating a list of the programs you want to run then use a DATA step and then use CALL EXECUTE to do the %INCLUDEs:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt; %let Run_Pgm_List = proj1*proj2*proj3;
 %let app_saspgm_dir = C:\temp;

data _null_;
  length Run_Pgm_List 1024;
  Run_Pgm_List = symget('Run_Pgm_List');
  pgm_count = countw(Run_Pgm_List, '*');
  if pgm_count ge 1 then do pgm_num = 1 to pgm_count;
    pgm_name = scan(Run_Pgm_List, pgm_num, '*');
    call execute('%include "&amp;amp;app_saspgm_dir.\' !! strip(pgm_name) !! '.sas";');
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 25 May 2021 01:59:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-include-a-data-step-macro-inside-another-macro-with/m-p/743453#M232780</guid>
      <dc:creator>SASKiwi</dc:creator>
      <dc:date>2021-05-25T01:59:52Z</dc:date>
    </item>
    <item>
      <title>Re: How to include a data step macro inside another macro with %include?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-include-a-data-step-macro-inside-another-macro-with/m-p/743494#M232799</link>
      <description>&lt;P&gt;I was thinking about this, but reluctant to do it. As the nested macro is just part of the data flows depending on the previous data processing in #6, and I don't want to make it into a stand-alone macro function or separate it from the data processing.&amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 25 May 2021 07:06:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-include-a-data-step-macro-inside-another-macro-with/m-p/743494#M232799</guid>
      <dc:creator>windlove</dc:creator>
      <dc:date>2021-05-25T07:06:18Z</dc:date>
    </item>
  </channel>
</rss>

