## Macro Do Loop Iteration

Solved
Frequent Contributor
Posts: 79

# 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,784

## 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

All Replies
Super User
Posts: 23,740

## 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: 79

## 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: 10,258

## 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
How to convert datasets to data steps
How to post code
Solution
‎05-26-2014 09:12 AM
Super User
Posts: 10,784

## 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

Posts: 1,270

## 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: 79

## 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.