VillageCat
VillageCat

Reputation: 11

SAS macro do-loop with data step

This is my first macro, so my apologies if I missed something simple.

I need to do the same data step six (or more) times and append each one to the first, so I tried a do-loop within a macro. Everything works with the loop removed, but once the do-loop is added, I get errors that either say I have an extra %end or an extraneous %mend. All ideas welcome. Thanks!

%macro freeze_samples(orig_file=, samples= , Start_Freeze_Incr=, 
           End_Freeze_Incr= );
%do i = 1 %to &samples;

data freeze_slice_&i;  
    set &orig_file;

     (do stuff)

run;

    * If we have more than one slice, append to previous slice(s).;
    %if &i > 1 %then %do;
        proc append base = temp_1 data = temp_&i;
        run;
    %end;

%end;

%mend;

Upvotes: 0

Views: 5922

Answers (1)

Joe
Joe

Reputation: 63424

I think you either have a problem you didn't include in the text (ie, in the 'do stuff' section) or you have a bad session (ie, you fixed the problem but there's something from a previous run messing up something now). This runs fine (given I don't know what you're doing):

%macro freeze_samples(orig_file=, samples= , Start_Freeze_Incr=, 
           End_Freeze_Incr= );
%do i = 1 %to &samples;

data freeze_slice_&i;  
    set &orig_file;

     *(do stuff);

run;

    * If we have more than one slice, append to previous slice(s).;
    %if &i > 1 %then %do;
        proc append base = freeze_slice_1 data = freeze_slice_&i;  
        run;
    %end;

%end;

%mend;

%freeze_samples(orig_file=sashelp.class,samples=2,start_freeze_incr=1,end_freeze_incr=5);

I would note that you're probably better off not doing whatever you're doing this way; in SAS, there is usually a better way than splitting data off into multiple datasets. But since I don't know what you're doing I can't really suggest the better way beyond recommending reading this article and keeping it in mind (even if you're doing something different than bootstrapping, the concept applies to almost everything in SAS).

Upvotes: 4

Related Questions