Hello,
I couldn't figure out what i am doing wrong in the below code but basically I am trying to get day from today's date and use that as "start" value for the do loop iteration. Could someone tell me why this isn't working? Thank You!
%macro test;
%do n = %sysfunc(day(%sysfunc(today())-1)) %to 20 by -1;
/*%do n = 24 %to 20;*/
%Let n1 = %eval(&n.- 1);
%put N1 => &n1.;
%end;
%mend;
%test;
ERROR: A character operand was found in the %EVAL function or %IF condition where a numeric operand is required. The condition was: 23 by -1
ERROR: The %TO value of the %DO N loop is invalid.
ERROR: The macro AA will stop executing.
I ran a test after the change i made, here is the log:
1185 %macro test;
1186 %do n = %sysfunc(day(%sysfunc(today())-1)) %to 20 %by -1;
1187 /*%do n = 24 %to 20;*/
1188 %Let n1 = %eval(&n.- 1);
1189 %put N1 => &n1.;
1190 %end;
1191
1192 %mend;
1193 %test;
N1 => 23
N1 => 22
N1 => 21
N1 => 20
N1 => 19
A quick look at your code tells me you are missing %by instead of by
I ran a test after the change i made, here is the log:
1185 %macro test;
1186 %do n = %sysfunc(day(%sysfunc(today())-1)) %to 20 %by -1;
1187 /*%do n = 24 %to 20;*/
1188 %Let n1 = %eval(&n.- 1);
1189 %put N1 => &n1.;
1190 %end;
1191
1192 %mend;
1193 %test;
N1 => 23
N1 => 22
N1 => 21
N1 => 20
N1 => 19
I knew i was doing something silly. Thank you!
I hope you are testing that day(today()) is greater than 20 before executing else you will have an infinite loop.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.