<?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: Problem in executing a macro inside another macro to load txt files in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Problem-in-executing-a-macro-inside-another-macro-to-load-txt/m-p/784763#M250429</link>
    <description>&lt;P&gt;You are calling the macro in the middle of a DATA step.&amp;nbsp; That means the only SAS statements the macro can generate are statements that can be used to construct a data step.&amp;nbsp; Like an assignment statement, INPUT, PUT, FORMAT, LABEL, LENGTH etc.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But you macro is generating a complete data step, include another DATA statement.&lt;/P&gt;
&lt;P&gt;So your SAS code ends up looking like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
  set filenames3;
  if _n_=&amp;amp;i then do;

DATA IMPORT_OSA_&amp;amp;ORDER.;
infile "/sasdata/it_databases/myFile_20211207_&amp;amp;EXTENSION...txt" dlm=';' dsd missover;
input
        var1          : $CHAR3.
        var2          : $CHAR2.
        var3          : $CHAR5.
        var4          : ?? BEST6.
;
run;

 end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;So the DO statement in the outer data step never has an END statement. Instead you started another DATA step.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It really looks like you are just trying to read ALL of the files in a directory.&lt;/P&gt;
&lt;P&gt;Why not just do that directly and skip all of the other rigamarole?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data IMPORT_OSA ;
  infile "/sasdata/it_databases/*.txt" dlm=';' dsd truncover ;
  length var1 $3 var2 $2 var3 $5 var4 8;
  input var1-var3 var4 ?? ;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 07 Dec 2021 21:36:37 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2021-12-07T21:36:37Z</dc:date>
    <item>
      <title>Problem in executing a macro inside another macro to load txt files</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Problem-in-executing-a-macro-inside-another-macro-to-load-txt/m-p/784544#M250388</link>
      <description>&lt;P&gt;&lt;SPAN style="font-family: inherit;"&gt;Hi guys, I am running a macro inside another macro but I am receiving an stranger error when executing. Although my program run and give me the database that I want, the error msg stops the program to keep running and it stops the onther programs that I have linked in my process flow and needs the firts one.&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;So this is an example of what what I have:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I receive some txt files every day with names that is something like this:&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;"&lt;EM&gt;myFile_20211207_9825cd892ab73.txt&lt;/EM&gt;"&lt;/P&gt;&lt;P&gt;So, it means "&lt;EM&gt;myFile_YYYYMMDD_RANDOMCHARS.txt&lt;/EM&gt;".&lt;/P&gt;&lt;P&gt;Then I created a way to read the files in the directory, select only the files that I want, count them and then import those txts like this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;/*Create a database with names of all files in the directory*/
data filenames;
	length fref $8 fname $200;
	did = filename(fref, '/sasdata/it_databases');
	numero=dnum(did);
	did = dopen(fref);

	do i = 1 to dnum(did);
		fname = dread(did,i);
		extrac=substr(fname,29,17);
		output;
	end;

	did = dclose(did);
	did = filename(fref);
	keep fname extrac;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Whit this I got the database filenames like the print below:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="azambvitor_1-1638887184080.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/66459i2FC52CD6DB097668/image-size/medium?v=v2&amp;amp;px=400" role="button" title="azambvitor_1-1638887184080.png" alt="azambvitor_1-1638887184080.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;Then I calculate the number of files that I have into the filenames directory and create a macro var&amp;nbsp;&lt;EM&gt;num_obs&lt;/EM&gt; with this number.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;/*Calculate the number os filesinside the directory (table filenames)*/
data _null_;
	if 0 then
		set work.filenames nobs=n;
	call symputx('num_obs', n);
	stop;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;And after that I run the macro FILEE to import the txts into "IMPORT_OSA_&amp;amp;ORDER" databases.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;%MACRO FILEE(EXTENSION, ORDER);

DATA IMPORT_OSA_&amp;amp;ORDER.;
infile "/sasdata/it_databases/&lt;EM&gt;myFile_20211207_&lt;/EM&gt;&amp;amp;EXTENSION...txt" dlm=';' dsd missover;
input
        var1          : $CHAR3.
        var2          : $CHAR2.
        var3          : $CHAR5.
        var4          : ?? BEST6.
;
run;

%MEND;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;And to finish my program, I running the code like below:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;data _null_;
	set filenames;
	call symputx(catx('_', 'intReg', _n_),_n_);
	call symputx(catx('_', 'strName', _n_),extrac);
run;

%MACRO LOOP;
%local i;
%do i=1 %to &amp;amp;num_obs;

data _null_;
	set filenames3;
	if _n_=&amp;amp;i then 
	do;
		%FILEE(&amp;amp;strName_&amp;amp;i., &amp;amp;i.);
	END;
	run;
%END;
%Mend LOOP;

%loop;

data import_osa;
	set work.import_osa_:;
run;&lt;/CODE&gt;&lt;/PRE&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;P&gt;When I run the macro LOOP I got the database &lt;STRONG&gt;&lt;EM&gt;import_osa&lt;/EM&gt;&lt;/STRONG&gt; as I want, but it returnrs the errors below and it stop the executions of the linked programs in my project.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;&lt;FONT color="#008080"&gt;WARNING: Apparent symbolic reference STRNAME_ not resolved.&lt;/FONT&gt;

&lt;FONT color="#339966"&gt;NOTE: The SAS System stopped processing this step because of errors.
NOTE: DATA statement used (Total process time):
      real time           0.03 seconds
      cpu time            0.01 seconds
      
NOTE: Line generated by the invoked macro "ARQUIVO".&lt;/FONT&gt;
41            DATA IMPORT_OSA_&amp;amp;ORDER.;   infile "/sasdata/it_databases/&lt;EM&gt;myFile_20211207_&lt;/EM&gt;&amp;amp;EXTENSION...txt" dlm=';'
             &lt;FONT color="#FF0000"&gt;_&lt;/FONT&gt;
             &lt;FONT color="#FF0000"&gt;117&lt;/FONT&gt;
41       !  dsd missover; input  var1         : $CHAR3.         var2            : $CHAR2.         var3          : $CHAR5.


&lt;FONT color="#FF0000"&gt;ERROR 117-185: There was 1 unclosed DO block.&lt;/FONT&gt;

&lt;FONT color="#0000FF"&gt;2                                                          The SAS System                            09:17 Tuesday, December 7, 2021&lt;/FONT&gt;
&lt;FONT color="#339966"&gt;NOTE: The infile "/sasdata/it_databases/&lt;EM&gt;myFile&lt;/EM&gt;_20211207_0847bdc1.txt" is:
      Filename=/sasdata/it_databases/&lt;EM&gt;myFile_&lt;/EM&gt;20211207_0847bdc1.txt,
      Owner Name=belibio,Group Name=staff,
      Access Permission=-rw-r--r--,
      Last Modified=07Dec2021:01:36:36,
      File Size (bytes)=432136340

NOTE: 346211 records were read from the infile "/sasdata/it_databases/&lt;EM&gt;myFile_&lt;/EM&gt;20211207_0847bdc1.txt".
      The minimum record length was 6.
      The maximum record length was 16765.
NOTE: The data set WORK.IMPORT_OSA_1 has 346211 observations and 523 variables.
NOTE: Compressing data set WORK.IMPORT_OSA_1 decreased size by 96.13 percent. 
      Compressed is 2678 pages; un-compressed would require 69243 pages.
NOTE: DATA statement used (Total process time):
      real time           2:08.40
      cpu time            10.12 seconds&lt;/FONT&gt;
      

&lt;FONT color="#FF0000"&gt;180: LINE and COLUMN cannot be determined.&lt;/FONT&gt;
&lt;FONT color="#339966"&gt;NOTE: NOSPOOL is on. Rerunning with OPTION SPOOL might allow recovery of the LINE and COLUMN where the error has occurred.&lt;/FONT&gt;
&lt;FONT color="#FF0000"&gt;ERROR 180-322: Statement is not valid or it is used out of proper order.&lt;/FONT&gt;
&lt;FONT color="#008080"&gt;WARNING: Apparent symbolic reference STRNAME_ not resolved.&lt;/FONT&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Can anyone help me to solve the errors? Or just give me another way to do this imports so I can continue automatically run the other programs that is linked in the project?&lt;/P&gt;</description>
      <pubDate>Tue, 07 Dec 2021 15:18:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Problem-in-executing-a-macro-inside-another-macro-to-load-txt/m-p/784544#M250388</guid>
      <dc:creator>azambvitor</dc:creator>
      <dc:date>2021-12-07T15:18:34Z</dc:date>
    </item>
    <item>
      <title>Re: Problem in executing a macro inside another macro to load txt files</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Problem-in-executing-a-macro-inside-another-macro-to-load-txt/m-p/784562#M250396</link>
      <description>&lt;P&gt;Step 1: when running macros that you are trying to debug, run this command first. This command adds information to the log that will enable you to more easily figure out what the problem is.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;options mprint;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Then run the macro again and look at the log. See if you can figure out what the problem is from the log. If not, then please show us the log.&lt;/P&gt;</description>
      <pubDate>Tue, 07 Dec 2021 16:13:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Problem-in-executing-a-macro-inside-another-macro-to-load-txt/m-p/784562#M250396</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2021-12-07T16:13:46Z</dc:date>
    </item>
    <item>
      <title>Re: Problem in executing a macro inside another macro to load txt files</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Problem-in-executing-a-macro-inside-another-macro-to-load-txt/m-p/784588#M250402</link>
      <description>Instead of a macro loop at all look into CALL EXECUTE. Much easier to test and debug and less macro variables to resolve. As long as your macro works with the correct parameters its very simple to code.</description>
      <pubDate>Tue, 07 Dec 2021 18:12:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Problem-in-executing-a-macro-inside-another-macro-to-load-txt/m-p/784588#M250402</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2021-12-07T18:12:15Z</dc:date>
    </item>
    <item>
      <title>Re: Problem in executing a macro inside another macro to load txt files</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Problem-in-executing-a-macro-inside-another-macro-to-load-txt/m-p/784763#M250429</link>
      <description>&lt;P&gt;You are calling the macro in the middle of a DATA step.&amp;nbsp; That means the only SAS statements the macro can generate are statements that can be used to construct a data step.&amp;nbsp; Like an assignment statement, INPUT, PUT, FORMAT, LABEL, LENGTH etc.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But you macro is generating a complete data step, include another DATA statement.&lt;/P&gt;
&lt;P&gt;So your SAS code ends up looking like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
  set filenames3;
  if _n_=&amp;amp;i then do;

DATA IMPORT_OSA_&amp;amp;ORDER.;
infile "/sasdata/it_databases/myFile_20211207_&amp;amp;EXTENSION...txt" dlm=';' dsd missover;
input
        var1          : $CHAR3.
        var2          : $CHAR2.
        var3          : $CHAR5.
        var4          : ?? BEST6.
;
run;

 end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;So the DO statement in the outer data step never has an END statement. Instead you started another DATA step.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It really looks like you are just trying to read ALL of the files in a directory.&lt;/P&gt;
&lt;P&gt;Why not just do that directly and skip all of the other rigamarole?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data IMPORT_OSA ;
  infile "/sasdata/it_databases/*.txt" dlm=';' dsd truncover ;
  length var1 $3 var2 $2 var3 $5 var4 8;
  input var1-var3 var4 ?? ;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 07 Dec 2021 21:36:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Problem-in-executing-a-macro-inside-another-macro-to-load-txt/m-p/784763#M250429</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-12-07T21:36:37Z</dc:date>
    </item>
  </channel>
</rss>

