DATA Step, Macro, Functions and more

Macro loop skip to next iteration if error

Reply
Contributor
Posts: 24

Macro loop skip to next iteration if error

[ Edited ]

I have a batch job that reads which programs to run from an excel file. However, sometimes one of the programs gets errors, which seems to cause the entire macro loop to end. How can I get the program to simply skip to the next iteration instead?

 

After importing the file into a dataset batch, the program selects the rows flagged for running, and runs them in a loop.

 

data batch;
set batch;
where run_flg = 1;
row= _n_;
run;

proc sql noprint;
select max(row) into: stop from batch;
quit;

%macro batch;
%local i;
   %do i = 1 %to &stop. ;

proc sql noprint;
select path into: pgm from batch where row = &i.;
quit; 
%include &pgm.;
  
%end;
%mend batch;

%batch;

I want the program to end the loop if an error occors, but proceed to the next interation and the next program, so that one buggy program doesnt stop the next from running

 

Super User
Super User
Posts: 9,227

Re: Macro loop skip to next iterartion if error

Depends very much on the bug.  If it is something that will spill over into other code - mismatched quotes are one example - then not in one session can you do such a thing.  You could theoretically open a new session for each program run, using command line etc. but why?  Why is the code your running throwing problems - far better to have robust code which doesn't fall over all the time than to try to avoid it, possibly causing further unexpected issues down the line.  

Super User
Posts: 13,084

Re: Macro loop skip to next iteration if error


Ullsokk wrote:

I have a batch job that reads which programs to run from an excel file.

 

After importing the file into a dataset batch, the program selects the rows flagged for running, and runs them in a loop.


If you scan this forum for issues related to importing data from Excel you might want to consider that either how you flag them or the actual contents have issues when you modify the Excel and re-import it.

Recurring themes: The length of the variable that holds the program name changes from execution to execution: You don't say how your read the Excel file, if using proc import this problem is very likely going to recur as the procedure has to guess how long the variable should be and if bottom rows are longer than then top very likely to have the names truncated.

 

What KIND of errors are you getting will likely have some impact. If the individual programs are failing because of a dependency such as a missing data set or one with that exists but has not observations then each of those programs should have logic to check and not execute when the dependent items aren't available.

If they are failing because of syntax errors then the code for that program does not belong in batch job at all and beat the writer with wet spaghetti noodles until fixed.

 

Ask a Question
Discussion stats
  • 2 replies
  • 329 views
  • 0 likes
  • 3 in conversation