Search code examples
loopssassas-macro

loop whole code many times on SAS program causes error


I have a long code about 5000 lines. When I macro this program, and loop a few times. It executed successfully but if I changed the loop to 300 times, the code started to cause the error, I can not figure out why it causes,

%let Numberl=300;

%macro programall;
%do r=0 %to &numberl;
.
.
.
.
.
%end;
%mend Programall;
%Programall;

I assume sleep function may stop this problem https://support.sas.com/documentation/cdl/en/lefunctionsref/63354/HTML/default/viewer.htm#p0a6vn2td7bjr2n1viy8y4lgvq61.htm

Does anybody have a similar experience?

sorry about my English

Thanks


Solution

  • The code inside the loop is likely not resetting important macro variables it is using or there are nested macro calls that are using macro variables that are not properly declared as %local in their %macro definition.

    When ever you code a macro, make sure the variables it uses are declared at the top as such as follows:

    %macro foo(top=);
      %local index piece1 piece2;
      %do index = 1 %to ⊤
        %let piece1 = …;
      %end;
    %mend;
    

    Is there a consistent specific value of &r at which the failure occurs? If so, you may have suppositions about r that are incorrect.

    You can see what macro is generating by setting options before invoking %programall. These options will log a lot of information at the most detailed level:

    options mprint mtrace symbolgen;
    

    You can save the actual code that macro generates to an external file using mfile.

    filename mfile 'c:\temp\300-at-Thermopylae.sas';
    options mprint mfile;
    
    %programall
    
    %options nomfile;
    

    Note: The problem might be in the generated source code, or in the generation process it self. You can examine the saved .sas file for problems, and submit pieces of it individually, in order to locate where things have gone astray.