DATA Step, Macro, Functions and more

using macro in do loop

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 82
Accepted Solution

using macro in do loop

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.


Accepted Solutions
Solution
‎01-25-2018 05:34 PM
PROC Star
Posts: 1,351

Re: using macro in do loop

Posted in reply to novinosrin

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

 

View solution in original post


All Replies
PROC Star
Posts: 1,351

Re: using macro in do loop

A quick look at your code tells me you are missing %by instead of by

Solution
‎01-25-2018 05:34 PM
PROC Star
Posts: 1,351

Re: using macro in do loop

Posted in reply to novinosrin

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

 

Frequent Contributor
Posts: 82

Re: using macro in do loop

Posted in reply to novinosrin

I knew i was doing something silly. Thank you!

Super User
Posts: 13,084

Re: using macro in do loop

I hope you are testing that day(today()) is greater than 20 before executing else you will have an infinite loop.

Frequent Contributor
Posts: 82

Re: using macro in do loop

It will be actually1. I put 20 for illustration purpose. Thanks.
☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 5 replies
  • 156 views
  • 0 likes
  • 3 in conversation