Search code examples
sassas-macro

Repeat several datasets and proc sql


I have program with data sets and proc sql. At the end of the program I would like to check final value and decide if the part of the program should be executed again with different parameters or the value is ok.

Is there a way I can do loop datasets and procsql ? (For example I want to repeat all the code below.)

proc sql;
    create table first as
    select * from qw;
quit;

data st;
  a = 1
run;

proc sql;
    create table st as
    select * from fir;
quit;

Many thanks for any help. Michal


Solution

  • You need to create a macro, with a do-while loop. You don't specify what the values are, where you're getting them from or how you want to stop, but hopefully this idea is enough to get you started.

    If you are looking at a minimization or optimization problem and have SAS/IML or SAS/OR you may want to see if they have procs that would be helpful for your situation.

    %macro loop (threshold);
    %do %while (&value < &threshold);
    proc sql;
        create table first as
        select * from qw;
    quit;
    
    data st;
      a = 1
    run;
    
    proc sql;
        create table st as
        select soucet +1 into :value from fir;
    quit;
    
    
    %end;
    
    %mend;
    

    Edit:

    Use end= option on the set statement. If its the last record create a macro variable that can be used to loop.

    Set end=eof; 
    if eof then do; 
        call symputx('soucet', soucet);
    end;