|
Here is an example of do-loop and %do-loop in a macro.
As I understand,
%do-loop can only works in macro, but do-loop can works in both in data step and macro,
the difference is in the iteration variable i, as shown in the example.
%macro Doloop(var1=, var2=, var3=);
data test;
do i = 1 to 5; /* do loop works without macro variables */
do j = &var1 to &var2; /* this als work since j does not need to be a macro variable */
A=i;
B=j;
C=&var3; /* macro variable is used as it is used in data step ouside macro */
output;
end;
end;
drop i;
run;
data test2;
%do i = &var1 %to &var2; /* %do loop works with macro variables */
do j = &var1 to &var2;
* A=i; /* this line does not work, because i is a macro variable */
AA = &i+1; /* this line works, as macro is referenced */
BB = j; /* use of non-macro variable */
CC = &var3;
output;
end;
%end;
* drop i; /* you don't need to drop i, */
run;
%mend;
%doloop(var1=1, var2=5, var3=99);
proc print data=test; title 'test'; run;
proc print data=test2; title 'test2'; run;
|