do Fib=1,2,3,5,8,13,21;
do i=Var1,Var2,Var3;
do j=BeginDate to Today() by 7;
do k=Test1-Test50;
Performing Repetitive Calculations
On January 1 of each year, $5,000 is invested in an account. Determine the value of the account after three years based on a constant annual interest rate of 7.5 percent.
data invest;
do Year=2001 to 2003;
Capital+5000;
Capital+(Capital*.075);
end;
run;
OUTPUT:
proc print data=invest noobs;
run;
Year Capital
2004 17364.61
BUT
Generate a separate observation for each year.
data invest;
do Year=2001 to 2003;
Capital+5000;
Capital+(Capital*.075);
output;
end;
run;
proc print data=invest noobs;
run;
OUTPUT:
Year Capital
2001 5375.00
2002 11153.13
2003 17364.61
prog2.growth
Num
Division Emps Increase
APTOPS 205 0.075
FINACE 198 0.040
FLTOPS 187 0.080
data forecast;
set prog2.growth(rename=(NumEmps=NewTotal));
/* COPY SAS dataset from prog2.growth, and change the NumEmps variable name to NewTotal*/
do Year=1 to 3;
NewTotal=NewTotal*(1+Increase);
output;
end;
run;
You can use DO WHILE and DO UNTIL statements to stop the loop when a condition is met rather than when the index variable exceeds a specific value.
The DO WHILE statement executes statements in a DO loop while a condition is true.
DO WHILE (expression);
END;
The DO UNTIL statement executes statements in a DO loop until a condition is true.
DO UNTIL (expression);
END;
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.
data invest;
do until(Capital>1000000);
Year+1;
Capital+5000;
Capital+(Capital*.075);
end;
run;
proc print data=invest noobs;
run;
OUTPUT:
Capital Year
1047355.91 38
You can combine DO WHILE and DO UNTIL statements with the iterative DO statement.
Determine the return of the account again. Stop the loop if 25 years is reached or more than $250,000 is accumulated.
data invest;
do Year=1 to 25 until(Capital>250000);
Capital+5000;
Capital+(Capital*.075);
end;
run;
proc print data=invest noobs;
run;
OUTPUT:
Year Capital
21 255594.86
Nested DO Loops
data invest(drop=Quarter);
do Year=1 to 5;
Capital+5000;
do Quarter=1 to 4;
Capital+(Capital*(.075/4));
end;
output;
end;
run;
proc print data=invest noobs;
run;
OUTPUT:
Year Capital
1 5385.68
2 11186.79
3 17435.37
4 24165.94
5 31415.68
No comments:
Post a Comment