Macro Do Loop Iteration

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 75
Accepted Solution

Macro Do Loop Iteration

All -


I knew that we can't perform arithmetic operation directly on the macro variables, if we have to perform the same then we need to use either %EVAL or %SYSEVALF macro functions.


%LET A=3;

%LET B=4;

%LET SUM=&A+&B;

%PUT SUM=&SUM.;

%LET SUM=%EVAL(&A+&B);

%PUT SUM=&SUM.;

But if you look at the below code, the do loop iterating 10 times through the %put statement without even incrementing the counter i by 1. Can anyone explain how this is happening.

%MACRO LOOPCHK;

%DO I=1 %TO 10;

%PUT I=&I;

%END;

%MEND LOOPCHK;

%LOOPCHK;


Accepted Solutions
Solution
‎05-26-2014 09:12 AM
Super User
Posts: 10,044

Re: Macro Do Loop Iteration

By Default, SAS will take %DO I=1 %TO 10;  I as Integer  just like   %DO  %eval( I=1 ) %TO  %eval(10) ; You can find it in Macro Documentation. if I is double or float , this wouldn't work . you need %sysevalf() around it.

Xia Keshan

View solution in original post


All Replies
Super User
Posts: 19,865

Re: Macro Do Loop Iteration

You have something else wrong somewhere, all of your code above works as expected..

1    %MACRO LOOPCHK;

2

3    %DO I=1 %TO 10;

4    %PUT I=&I;

5    %END;

6

7    %MEND LOOPCHK;

8

9    %LOOPCHK;

I=1

I=2

I=3

I=4

I=5

I=6

I=7

I=8

I=9

I=10

10   %LET A=3;

11   %LET B=4;

12   %LET SUM=&A+&B;

13   %PUT SUM=&SUM.;

SUM=3+4

14

15   %LET SUM=%EVAL(&A+&B);

16   %PUT SUM=&SUM.;

SUM=7

Frequent Contributor
Posts: 75

Re: Macro Do Loop Iteration

Reeza,


I don't have problem with my code. My question is when we need macro functions like evalf & sysevalf to perform arithmetic operation, how the same is performed without any macro functions in %do loop macro statement.

Super User
Posts: 7,854

Re: Macro Do Loop Iteration

That is just a function of the iterative %do. It does the increment implicitly.

You probably miss the constructs you would expect in a C language "for" statement. (initialize, increment, end condition)

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Solution
‎05-26-2014 09:12 AM
Super User
Posts: 10,044

Re: Macro Do Loop Iteration

By Default, SAS will take %DO I=1 %TO 10;  I as Integer  just like   %DO  %eval( I=1 ) %TO  %eval(10) ; You can find it in Macro Documentation. if I is double or float , this wouldn't work . you need %sysevalf() around it.

Xia Keshan

Trusted Advisor
Posts: 1,228

Re: Macro Do Loop Iteration

Hi,

Try this, may help in understanding the concept.

Thanks,

Naeem

options symbolgen;
%MACRO LOOPCHK;
%DO I=1 %TO 10;
%PUT I=&I;
%END;
%MEND LOOPCHK;
%LOOPCHK;


options symbolgen;
%MACRO LOOPCHK;
%let I=1;
%DO  %while (&I<=10);
%PUT I=&I;
%let I=%eval(&I+1);
%END;
%MEND LOOPCHK;
%LOOPCHK;

Frequent Contributor
Posts: 75

Re: Macro Do Loop Iteration

Thank you Ksharp, stat@sas, Reeza and KurtBremser for your replies and insight on how this implicit looping works.

🔒 This topic is solved and locked.

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

Discussion stats
  • 6 replies
  • 343 views
  • 3 likes
  • 5 in conversation