Ch 11: Processing Data with DO Loops Flashcards

1
Q
1.
Which statement is false regarding the use of DO loops?
a.
They can contain conditional clauses.
b.
They can generate multiple observations.
c.
They can be used to combine DATA and PROC steps.
d.
They can be used to read data.
A

Correct answer: c

DO loops are DATA step statements and cannot be used in conjunction with PROC steps.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q
During each execution of the following DO loop, the value of Earned is calculated and is added to its previous value. How many times does this DO loop execute?
data work.earnings;
Amount=1000;
Rate=.075/12;
do month=1 to 12;
Earned+(amount+earned)*rate;
end;
run;
a.
0
b.
1
c.
12
d.
13
A

Correct answer: c

The number of iterations is determined by the DO statement’s stop value, which in this case is 12.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q
On January 1 of each year, $5,000 is invested in an account. Complete the DATA step below to determine the value of the account after 15 years if a constant interest rate of 10% is expected.
data work.invest;
...
Capital+5000;
capital+(capital*.10);
end;
run;
a.
do count=1 to 15;
b.
do count=1 to 15 by 10%;
c.
do count=1 to capital;
d.
do count=capital to (capital*.10);
A

Correct answer: a

Use a DO loop to perform repetitive calculations starting at 1 and looping 15 times.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q
In the data set Work.Invest, what would be the stored value for Year?
data work.invest;
do year=1990 to 2004;
Capital+5000;
capital+(capital*.10);
end;
run;
a.
missing
b.
1990
c.
2004
d.
2005
A

Correct answer: d
At the end of the 15th iteration of the DO loop, the value for Year is incremented to 2005. Because this value exceeds the stop value, the DO loop ends. At the bottom of the DATA step, the current values are written to the data set.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q
Which of the following statements is false regarding the program shown below?
data work.invest;
do year=1990 to 2004;
Capital+5000;
capital+(capital*.10);
output;
end;
run;
a.
The OUTPUT statement writes current values to the data set immediately.
b.
The last value for Year in the new data set is 2005.
c.
The OUTPUT statement overrides the automatic output at the end of the DATA step.
d.
The DO loop performs 15 iterations.
A

Correct answer: b
The OUTPUT statement overrides the automatic output at the end of the DATA step. On the last iteration of the DO loop, the value of Year, 2004, is written to the data set.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q
How many observations will the data set Work.Earn contain?
data work.earn;
Value=2000;
do year=1 to 20;
Interest=value*.075;
value+interest;
output;
end;
run;
a.
0
b.
1
c.
19
d.
20
A

Correct answer: d
The number of observations is based on the number of times the OUTPUT statement executes. The new data set has 20 observations, one for each iteration of the DO loop.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q
Which of the following would you use to compare the result of investing $4,000 a year for five years in three different banks that compound interest monthly? Assume a fixed rate for the five-year period.
a.
DO WHILE statement
b.
nested DO loops
c.
DO UNTIL statement
d.
a DO group
A

Correct answer: b
Place the monthly calculation in a DO loop within a DO loop that iterates once for each year. The DO WHILE and DO UNTIL statements are not used here because the number of required iterations is fixed. A non-iterative DO group would not be useful.

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

Which statement is false regarding DO UNTIL statements?
a.
The condition is evaluated at the top of the loop, before the enclosed statements are executed.
b.
The enclosed statements are always executed at least once.
c.
SAS statements in the DO loop are executed until the specified condition is true.
d.
The DO loop must have a closing END statement.

A

Correct answer: a
The DO UNTIL condition is evaluated at the bottom of the loop, so the enclosed statements are always executed at least once.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q
Select the DO WHILE statement that would generate the same result as the program below.
data work.invest;
capital=100000;
do until(Capital gt 500000);
Year+1;
capital+(capital*.10);
end;
run;
a.
do while(Capital ge 500000);
b.
do while(Capital=500000);
c.
do while(Capital le 500000);
d.
do while(Capital>500000);
A

Correct answer: c
Because the DO WHILE loop is evaluated at the top of the loop, you specify the condition that must exist in order to execute the enclosed statements.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q
In the following program, complete the statement so that the program stops generating observations when Distance reaches 250 miles or when 10 gallons of fuel have been used.
data work.go250;
set cert.cars;
do gallons=1 to 10 ... ;
Distance=gallons*mpg;
output;
run;
a.
while(Distance<=250)
b.
when(Distance>250)
c.
over(Distance le 250)
d.
until(Distance=250)
A

Correct answer: a
The WHILE expression causes the DO loop to stop executing when the value of Distance becomes equal to or greater than 250.

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