Module 7: Do Loops and Arrays Flashcards
What does an array do?
Provides a way to reference a group of columns for processing in the DATA setp. It groups columns together to processes repetitive calculations efficiently
What is an element in an array?
How columns are referred. Each specific column is a different element, which makes it easier to reference
What are the two steps needed to create an array?
1) Define the array.
ARRAY array-name [number-of-elements] <array-elements>;
2) Reference the array specifying the column that is desired.
Ex. of defined array: array scores[12] game01 – game12;
T/F: When you define an array in one dataset, you can also use it in another.
False: Arrays can only be accessed in the dataset they are defined. Need to redefine in a seperate data set.
How do you handle an unknown number of array elements?
Use an asterisk within the brackets when defining an array.
Ex. array name[*] var1 – varn
When you have an unknown number of variables, how do you execute a do loop properly?
Can use a DIM function to return the number of elements in the array
Ex. i = 1 to dim(array-name);
If you want to assign initial values to array elements, how would you code it?
General: array array-name [# elements] <elements> (initial values);
Ex. array var[4] (1, 2, 3, 4) => var1 assigned 1, var2 assigned 2, etc.</elements>
Write the general code for a do loop.
data new; set new;
do;
end;
run;
T/F: Every “Do” loop requires an end.
True: else it will won’t end.
What are iteravtive do loops?
Do loops that process a group of varaibles repeatedly, rather than once, based on the value of an index variable.
Write the general syntax for an interative do loop?
do indexvar = start_val to indexvar = stop_value;
statements;
end;
Ex. do 1 = 2 to 8
How do you increment by a number other than 1 in an iterative do loop?
Use a BY option.
Ex. do 1 = 2 to 8 by 2.
This increments by 2 instead of 1
Write a general accumulative summing statement.
var1+var2
For the do loop below, how many times is the loop executed?
do i = 1 to 20;
It executes 21 times. It increments to 21 and because it is over 20, it stops and processing and continues to the next DATA step statement.
What does an explicit output stament do in a do loop?
It overrides automatic output, causing SAS to add an observation to the data set only when the explicit OUTPUT statement is executed.