Thursday, April 10, 2008

The Iterative DO Statement

do Month='JAN','FEB','MAR';
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

The DATASETS Procedure

You can use the DATASETS procedure to modify a variable’s
name
label
format
informat.

PROC DATASETS LIBRARY=libref ;
MODIFY SAS-data-set ;
RENAME old-name-1=new-name-1
<. . . old-name-n=new-name-n>;
LABEL variable-1='label-1' <. . . variable-n='label-n'>;
FORMAT variable-list-1 format-1 <. . . variable-list-n format-n>;
INFORMAT variable-list-1 informat-1 <. . . variable-list-n informat-n>;
RUN;

Use the DATASETS procedure to change the name of the variable Dest to Destination.
Look at the attributes of the variables in the ia.dfwlax data set.

proc contents data=ia.dfwlax;
run;

Rename the variable Dest to Destination.
proc datasets library=ia;
modify dfwlax;
rename Dest=Destination;
run;

-----Alphabetic List of Variables and Attributes-----

# Variable Type Len Pos
--------------------------------------
2 Date Char 8 19
3 Destination Char 3 27
5 Economy Num 8 8
4 FirstClass Num 8 0
1 Flight Char 3 16

Permanent Variable Attributes

Assign labels and formats in the DATA step.

libname ia 'SAS-data-library';
data ia.dfwlax;
infile 'raw-data-file';
input @1 Flight $3. @4 Date mmddyy8.
@12 Dest $3. @15 FirstClass 3.
@18 Economy 3.;
format Date mmddyy10.;
label Dest='Destination'
FirstClass='First Class Passengers'
Economy='Economy Passengers';
run;

Examine the descriptor portion of the ia.dfwlax data set.

proc contents data=ia.dfwlax;
run;

OUTPUT
-----Alphabetic List of Variables and Attributes-----

# Variable Type Len Pos Format Label
----------------------------------------------------------------
2 Date Num 8 0 MMDDYY10.
3 Dest Char 3 27 Destination
5 Economy Num 8 16 Economy Passengers
4 FirstClass Num 8 8 First Class Passengers
1 Flight Char 3 24

Override Permanent Attributes
Use a FORMAT statement in a PROC step to temporarily override the format stored in the data set descriptor.

proc print data=ia.dfwlax label;
format Date date9.;
run;

the compilation phase

At compile time, SAS creates
1. an input buffer to hold the current raw data file record that is being processed
2. a program data vector (PDV) to hold the current SAS observation
3. the descriptor portion of the output data set.

The PDV contains two automatic variables that can be used for processing but which are not written to the data set as part of an observation:
_N_ counts the number of times that the DATA step begins to execute
_ERROR_ signals the occurrence of an error that is caused by the data during
execution.
0 is the default which means there is no error.
1 there is one or more errors.

During the compilation phase, SAS also scans each statement in the DATA step, looking for syntax errors. Syntax errors include:
1. Missing or misspelled keywords
2. Invalid variable names
3. Missing or invalid punctuation
4. Invalid options

Summary of the compilation phase
1. During the compilation phase, the input buffer is created to hold a record from the external file.
2. The PDV is created to hold the current observation
3. The descriptor portion of the SAS data set is created.