Hi:
Although there are many instances where you might use a macro %DO loop, when generating code, it does not seem necessary to make this code task more difficult by introducing macro variables into the mix. It will only complicate debugging the logic and output should things not work as expected.
Consider the program below:
[pre]
data usearray;
set sashelp.class;
array silly a1b a2b a3b a4b a5b;
do i = 1 to 5 by 1;
silly(i) = age +i;
end;
run;
ods listing;
proc print data=usearray;
var name age a1b a2b a3b a4b a5b;
run;
[/pre]
In my program, above, the ARRAY statement declares an ARRAY named SILLY to hold 5 variables: A1B through A5B. The variables do NOT have to be similarly named, they must only be of the same type (all numeric or all character). A SAS ARRAY is just a convenient method for referencing a group of variables.
Then I have a regular DATA step DO loop, iterate from 1 to 5. I could have used the DIM function; I could have used a DO WHILE or a DO UNTIL loop (but then I would have been responsible for iterating the counter or index variable on my own).
The statement: silly(i) = age +i;
shows the referencing method that you can use AFTER an ARRAY has been declared in a program. When the value of I is 1 then silly(i) refers to the first variable in the ARRAY list, A1B, when the value of I is 2 then silly(i) refers to the second variable in the ARRAY list...and so on.
There are many other reasons for using a macro %DO loop or using macro variable references in a program. However, a simple &i will not iterate on it's own
-AND- a simple &i reference is probably inappropriate in a DATA step program because of issues with DATA step compile time, DATA step execution time, macro code resolution time and when you want your macro variable to resolve. Simple &MACVAR references (such as &I or &A) resolve at DATA step compile time AND THEN DO NOT CHANGE. So these simple &MACVAR references are only good for providing constant values, such as data set names, constant multipliers (used as constants in the program) or items that change for every iteration of the program, but stay the same for every observation in the data set.
As I said, a SAS Array is a very simple way of referencing a collection of variables. For example, if you wanted to check ALL the numeric variables in your data and change missing values to 0, you would do something like this:
[pre]
array allnum _numeric_;
do i = 1 to dim(allnum);
if allnum(i) = . then allnum(i) = 0;
end;
[/pre]
A technique like this saves you coding time, because you do NOT have to name all the variables in separate IF statements. There are other naming constructs you can use with your variable lists and the SAS documentation outlines all the different ways you can name and list variables.
If you needed to generate a PROC PRINT step 100 times or generate an IF statement 100 times, or generate a list of 1000 variable names -- to use in a program, those might be some of the instances where you would use a MACRO %DO loop inside a MACRO program. But you should know that a MACRO %DO loop is rarely a substitute for an ARRAY statement for simple iteration through a group of variables.
cynthia