sasmacrossas-macrodo-loops

%by macro statement within %do loop not working as I would need


I have a problem with a %by statement within a %do-loop inside a SAS macro: when I run this piece of code

%let Today = 22;
%let last_day_prev = 31;

%MACRO Rename;
data work.TO_current_2;
set work.TO_current;
%Do curr_day= &Today. %TO curr_day = 1 %BY -1;
%let eval_day = %eval(&curr_day +(&last_day_prev));
rename Total_out_bal_odd_&curr_day. = total_out_bal_odd_&eval_day.;
rename Balance_&curr_day. = Balance_&eval_day.;
rename Team_&curr_day. = team_&eval_day.;
rename Bucket_odd_&curr_day. = Bucket_odd_&eval_day.;
rename Bucket_assig_odd_&curr_day. = Bucket_assig_odd_&eval_day.;
rename DPD_NDD_&curr_day. = DPD_NDD_&eval_day.;
%END;
run;
%MEND;
%rename;

the loop does not stop (as I would expect) at 1, but goes beyond until 0.

The work.TO_current table does indeed contain variables indexed to _0, but the whole purpose of this code should be to prevent them from being renamed. I know this question is probably silly for more experienced SAS users, but I can't wrap my head around it... thanks in advance!


Solution

  • You are expected to specifiy an integer (or a macro expression that generates an integer) to use as the final macro-variable iteration value for the %TO argument.

    Change your statement from

    %DO curr_day=&today. %TO curr_day = 1 %BY -1;
    

    to

    %DO curr_day=&today. %TO 1 %BY -1;