Fabrizio Ferrari
Fabrizio Ferrari

Reputation: 11

%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!

Upvotes: 0

Views: 308

Answers (2)

Tom
Tom

Reputation: 51621

SAS evaluates boolean expressions to 1 for TRUE and 0 for FALSE.

Since the digit 1 does not equal the string curr_day you told the %DO loop that the target value was 0. If you want the target value to be 1 just say that directly.

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

Upvotes: 0

Kermit
Kermit

Reputation: 3117

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;

Upvotes: 0

Related Questions