BookmarkSubscribeRSS Feed
azambvitor
Calcite | Level 5

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. 

 

So this is an example of what what I have:

 

I receive some txt files every day with names that is something like this: 

 

"myFile_20211207_9825cd892ab73.txt"

So, it means "myFile_YYYYMMDD_RANDOMCHARS.txt".

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:

 

 

/*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;

Whit this I got the database filenames like the print below:

 

azambvitor_1-1638887184080.png

Then I calculate the number of files that I have into the filenames directory and create a macro var num_obs with this number.

 

/*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;

 

And after that I run the macro FILEE to import the txts into "IMPORT_OSA_&ORDER" databases.

 

 

%MACRO FILEE(EXTENSION, ORDER);

DATA IMPORT_OSA_&ORDER.;
infile "/sasdata/it_databases/myFile_20211207_&EXTENSION...txt" dlm=';' dsd missover;
input
        var1          : $CHAR3.
        var2          : $CHAR2.
        var3          : $CHAR5.
        var4          : ?? BEST6.
;
run;

%MEND;

 

 

And to finish my program, I running the code like below:

 

 

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 &num_obs;

data _null_;
	set filenames3;
	if _n_=&i then 
	do;
		%FILEE(&strName_&i., &i.);
	END;
	run;
%END;
%Mend LOOP;

%loop;

data import_osa;
	set work.import_osa_:;
run;

 

 

 

When I run the macro LOOP I got the database import_osa as I want, but it returnrs the errors below and it stop the executions of the linked programs in my project.

 

 

WARNING: Apparent symbolic reference STRNAME_ not resolved.

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".
41            DATA IMPORT_OSA_&ORDER.;   infile "/sasdata/it_databases/myFile_20211207_&EXTENSION...txt" dlm=';'
             _
             117
41       !  dsd missover; input  var1         : $CHAR3.         var2            : $CHAR2.         var3          : $CHAR5.


ERROR 117-185: There was 1 unclosed DO block.

2                                                          The SAS System                            09:17 Tuesday, December 7, 2021
NOTE: The infile "/sasdata/it_databases/myFile_20211207_0847bdc1.txt" is:
      Filename=/sasdata/it_databases/myFile_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/myFile_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
      

180: LINE and COLUMN cannot be determined.
NOTE: NOSPOOL is on. Rerunning with OPTION SPOOL might allow recovery of the LINE and COLUMN where the error has occurred.
ERROR 180-322: Statement is not valid or it is used out of proper order.
WARNING: Apparent symbolic reference STRNAME_ not resolved.

 

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?

3 REPLIES 3
PaigeMiller
Diamond | Level 26

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.

 

options mprint;

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.

--
Paige Miller
Reeza
Super User
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.
Tom
Super User Tom
Super User

You are calling the macro in the middle of a DATA step.  That means the only SAS statements the macro can generate are statements that can be used to construct a data step.  Like an assignment statement, INPUT, PUT, FORMAT, LABEL, LENGTH etc. 

 

But you macro is generating a complete data step, include another DATA statement.

So your SAS code ends up looking like this:

data _null_;
  set filenames3;
  if _n_=&i then do;

DATA IMPORT_OSA_&ORDER.;
infile "/sasdata/it_databases/myFile_20211207_&EXTENSION...txt" dlm=';' dsd missover;
input
        var1          : $CHAR3.
        var2          : $CHAR2.
        var3          : $CHAR5.
        var4          : ?? BEST6.
;
run;

 end;
run;

So the DO statement in the outer data step never has an END statement. Instead you started another DATA step.

 

It really looks like you are just trying to read ALL of the files in a directory.

Why not just do that directly and skip all of the other rigamarole?

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;

 

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 663 views
  • 0 likes
  • 4 in conversation