Search code examples
pythonsassas-macro

SAS %macro rerun_loop(arg) doesn't appear to be a function, but was running in SAS


I am in charge of converting a series of SAS files from an antiquated Oracle+SAS environment to Python scripts. While I understand enough SAS to perform conversions, I'm unfamiliar with some methods of performing macros in SAS. I found this in the SAS file I'm converting right now:

%macro rerun_loop(wtn);
proc sql noprint;
/*Very large Oracle SQL Query*/
proc append base=/*tablename*/
        data=/*tablename*/;
run;
%mend rerun_loop;

rerun_loop doesn't appear to be a universal or documented SAS macro function, and I can't seem to find out what it is used for, or what it means.

I'm also confused about converting the loop to Python. Unlike some SAS macros, this doesn't specify any form of iteration, or how long it should be looping. I don't know what the Python equivalent is.

One more note that I'm confused about is that rerun_loop(wtn) has wtn as what appears to be an argument. In most programming languages I'm familiar with, wtn would be a variable being passed as an argument, or the argument name itself. However in this SAS file, no such variable is defined anywhere. wtn appears to be pulled in a previous CREATE TABLE/SELECT SQL statement in the code, but is not placed into any variables or stored anywhere. Apart from its references in the SQL queries earlier, it is never mentioned until it is sent as the argument in rerun_loop.

Can someone please shed some light on what this means and how to deal with it? Thanks.


Solution

  • That's a simple macro definition. It's not a function, but it has some analogies to it. When called in open code, it will copy the text from the macro definition into the execution stream and will substitute the parameter, as if you'd typed that code out yourself in the program.

    So you might have:

    %macro rerun_loop(wtn);
    proc sql;
      create table waiter_&wtn. as
      select * from oralib.table
        where waitername="&wtn.";  *uses the wtn parameter - & precedes it, . optionally afterwards;
    quit;
    
    proc append base=waiter_all append=waiter_&wtn.;
    run;
    %mend rerun_loop;
    
    %rerun_loop(John);
    %rerun_loop(Mark);
    %rerun_loop(Brenda);
    %rerun_loop(Alice);
    

    This would then run that four times, with four different parameters. In a 'real' environment, you probably would generate those calls programmatically.

    You can turn on options mprint symbolgen; (by running that line in open code) and the SAS log will show for all of the times a macro is run, what actually is being run.

    The text "loop" in the name is just part of the name, it's not relevant syntax.