Lesson 11, Part 2: Macro Loops¶
- %DO-%END Construct
- %DO-%WHILE Construct
- %DO-%UNTIL Construct
DATA step loops.¶
In [2]:
*Ex13_Percent_Do_DoLoop.sas;
options nodate nonumber nonotes nosource;
ods html close;
* Do Loop in a Data Step;
data _Null_;
do i = 1 to 5;
output;
put (_All_) (=);
end;
run;
Out[2]:
The SAS System
i=1
i=2
i=3
i=4
i=5
E3969440A681A2408885998500000005
Macro Loops¶
%DO-%END Construct is used to geneate SAS code¶
- inside of a macro
- I is the macro variable becaise it is in the %DO-%END loop.
In [32]:
options nodate nonumber nosource nonotes nosymbolgen;
ods html close;
%macro runit;
%local i;
%Do i = 1 %to 5;
%put i = Test&i;
%end;
%mend runit;
%runit
Out[32]:
The SAS System
i = Test1
i = Test2
i = Test3
i = Test4
i = Test5
E3969440A681A2408885998500000010
%DO-%While Construct¶
In [33]:
options nodate nonumber nosource nonotes nosymbolgen;
ods html close;
ods html close;
%macro runit;
%local i;
%Do %while (&i lt 6);
%let i = %eval(&i + 1);
%put i = Test&i;
%end;
%mend runit;
%runit
Out[33]:
The SAS System
i = Test1
i = Test2
i = Test3
i = Test4
i = Test5
i = Test6
E3969440A681A2408885998500000011
%DO-%Until Construct¶
In [34]:
options nodate nonumber nosource nonotes nosymbolgen;
ods html close;
ods html close;
%macro runit;
%local i;
%Do %until (&i = 6);
%let i = %eval(&i + 1);
%put i = Test&i;
%end;
%mend runit;
%runit
Out[34]:
The SAS System
i = Test1
i = Test2
i = Test3
i = Test4
i = Test5
i = Test6
E3969440A681A2408885998500000012