Académique Documents
Professionnel Documents
Culture Documents
2
Repetitive Coding
3
Repetitive Coding
data compound;
Amount=50000;
Rate=.075;
Yearly=Amount*Rate;
Quarterly+((Quarterly+Amount)*Rate/4);
Quarterly+
((Quarterly+Amount)*Rate/4);
Quarterly+((Quarterly+Amount)*Rate/4);
Quarterly+((Quarterly+Amount)*Rate/4);
run;
4
Repetitive Coding
data compound(drop=Qtr);
Amount=50000;
Rate=.075;
Yearly=Amount*Rate;
do Qtr=1 to 4;
Quarterly+((Quarterly+Amount)*Rate/4);
end;
run;
6
The Iterative DO Statement
The iterative DO statement executes
statements between DO and END statements
repetitively, based on the value of an index
variable.
DO
DO index-variable=specification-1
index-variable=specification-1 <,specification-n>;
<,specification-n>;
<additional
<additional SAS
SAS statements>
statements>
END;
END;
do i=1 to 12;
1 2 3 4 5 6 7 8 9 10 11 12 13
do j=2 to 10 by 2;
2 4 6 8 10 12
do k=14 to 2 by 2;
14 12 10 8 6 4 2 0
do m=3.6 to 3.8 by .05;
3.60 3.65 3.70 3.75 3.80 3.85
10
The Iterative DO Statement
DO
DO index-variable=item-1
index-variable=item-1 <,item-n>;
<,item-n>;
11
The Iterative DO Statement
How many times will each DO loop
execute?
do Month='JAN','FEB','MAR'; 3 times.
do Fib=1,2,3,5,8,13,21; 7 times.
do i=Var1,Var2,Var3; 3 times.
do j=BeginDate to Today() by 7;
The number of iterations depends
on the values of BeginDate and Today().
do k=Test1-Test50;
k is determined
1 time. A single value of
by subtracting Test50 from Test1. Use TO not
12 -.
Performing Repetitive Calculations
Year Capital
2004 17364.61
15
Performing Repetitive
Calculations
Generate a separate observation for each year.
data invest;
do Year=2001 to 2003;
Capital+5000;
Capital+(Capital*.075);
output;
end;
run; Explicit output
16
Performing Repetitive
Calculations
PROC PRINT Output
Year Capital
2001 5375.00
2002 11153.13
2003 17364.61
17
Reducing Redundant Code
The example that forecast the
growth of each division of an airline.
Partial Listing of the data set:
mylib.growth
Num
Division Emps Increase
18
Reducing Redundant Code
A Forecasting of Airline Growth
data forecast;
set mylib.growth(rename=(NumEmps=NewTotal));
Year=1;
NewTotal=NewTotal*(1+Increase);
output;
Year=2;
NewTotal=NewTotal*(1+Increase);
output;
Year=3;
NewTotal=NewTotal*(1+Increase);
output;
run;
20
Reducing Redundant Code
proc print data=forecast noobs;
run;
New
Division Total Increase Year
22
The DO WHILE Statement
The DO WHILE statement executes statements
in a DO loop while a condition is true.
DO
DO WHILE
WHILE (expression);
(expression);
<additional
<additional SAS
SAS
statements>
statements>
END;
END;
expression is evaluated at the top of the
loop.
The statements in the loop never execute if
the expression is initially false.
23
The DO UNTIL Statement
The DO UNTIL statement executes statements in a
DO loop until a condition is true.
DO
DO UNTIL
UNTIL (expression);
(expression);
<additional
<additional SAS
SAS
statements>
statements>
END;
END;
expression is evaluated at the bottom of the loop.
The statements in the loop are executed at least
once.
24
Conditional Iterative Processing
Task:
Determine the number of years it
would take for an account to
exceed $1,000,000 if $5,000 is
invested annually at 7.5 percent.
25
Conditional Iterative
Processing
data invest;
do until(Capital>1000000);
Year+1;
Capital+5000;
Capital+(Capital*.075);
end;
run;
26
Conditional Iterative
Processing
PROC PRINT Output
Capital Year
1047355.91 38
27
The Iterative DO Statement with a
Conditional Clause
DO
DO index-variable=start
index-variable=start TO
TO stop
stop <BY
<BY increment>
increment>
WHILE
WHILE || UNTIL
UNTIL (expression);
(expression);
<additional
<additional SAS
SAS statements>
statements>
END;
END;
Task:
Determine the return of the
account again.
Stop the loop if 25 years is reached
or more than $250,000 is
accumulated.
29
The Iterative DO Statement with a
Conditional Clause
data invest;
do Year=1 to 25 until(Capital>250000);
Capital+5000;
Capital+(Capital*.075);
end;
run;
30
The Iterative DO Statement with a
Conditional Clause
Year Capital
21 255594.86
31
Nested DO Loops
32
Nested DO Loops
Task:
Create one observation per year
for five years and show the
earnings if you invest $5,000 per
year with 7.5 percent annual
interest compounded quarterly.
33
Nested DO Loops
data invest(drop=Quarter);
do Year=1 to 5;
Capital+5000;
do Quarter=1 to 4;
5x 4x Capital+(Capital*(.075/4));
end;
output;
end;
run;
34
Nested DO Loops
PROC PRINT Output
Year Capital
1 5385.68
2 11186.79
3 17435.37
4 24165.94
5 31415.68
35
Nested DO Loops
Task:
Compare the final results of
investing $5,000 a year for five
years in three different banks that
compound quarterly. Assume each
bank has a fixed interest rate.
mylib.Banks
Name Rate
36
Nested DO Loops
37
Nested DO Loops
proc print data=invest noobs;
run;
38