Module 7: Do Loops and Arrays Flashcards

1
Q

What does an array do?

A

Provides a way to reference a group of columns for processing in the DATA setp. It groups columns together to processes repetitive calculations efficiently

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is an element in an array?

A

How columns are referred. Each specific column is a different element, which makes it easier to reference

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What are the two steps needed to create an array?

A

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;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

T/F: When you define an array in one dataset, you can also use it in another.

A

False: Arrays can only be accessed in the dataset they are defined. Need to redefine in a seperate data set.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How do you handle an unknown number of array elements?

A

Use an asterisk within the brackets when defining an array.

Ex. array name[*] var1 – varn

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

When you have an unknown number of variables, how do you execute a do loop properly?

A

Can use a DIM function to return the number of elements in the array

Ex. i = 1 to dim(array-name);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

If you want to assign initial values to array elements, how would you code it?

A

General: array array-name [# elements] <elements> (initial values);
Ex. array var[4] (1, 2, 3, 4) => var1 assigned 1, var2 assigned 2, etc.</elements>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Write the general code for a do loop.

A

data new; set new;
do;
end;
run;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

T/F: Every “Do” loop requires an end.

A

True: else it will won’t end.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What are iteravtive do loops?

A

Do loops that process a group of varaibles repeatedly, rather than once, based on the value of an index variable.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Write the general syntax for an interative do loop?

A

do indexvar = start_val to indexvar = stop_value;
statements;
end;

Ex. do 1 = 2 to 8

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How do you increment by a number other than 1 in an iterative do loop?

A

Use a BY option.
Ex. do 1 = 2 to 8 by 2.

This increments by 2 instead of 1

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Write a general accumulative summing statement.

A

var1+var2

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

For the do loop below, how many times is the loop executed?

do i = 1 to 20;

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What does an explicit output stament do in a do loop?

A

It overrides automatic output, causing SAS to add an observation to the data set only when the explicit OUTPUT statement is executed.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is a nested do loop?

A

A do loop inside of a do loop.

17
Q

How many observations will this do loop output?

data earn;
do year = 1 to 20;
do month = 1 to 12;
output;
end;
end;
run;

A

12*20 = 240; displays EOM reports

18
Q

How many observations will this do loop output?

data new;
do year = 1 to 20;
do month = 1 to 12;
end;
output;
end;
run;

A

20 times; only displays EOY reports

19
Q

What does DO UNTIL do?

A

Executes a DO loop until the expression becomes true

20
Q

What does DO WHILE do?

A

Executes a DO loop until the expression becomes false

21
Q

What is the major difference between DO UNTIL and DO WHILE?

A

It is how SAS evaluates the loop. UNTIL evaluates at the end and WHILE evaluates at the beginning