2.6 Flashcards

1
Q

Which of the following correctly specifies a DO LOOP?
a) data forecast;
set sashelp.shoes (rename=(Sales=ProjectedSales));
do Year = 1 to 3;
ProjectedSales=ProjectedSales1.05;
output;
end;
run;
b) data forecast;
set sashelp.shoes (rename=(Sales=ProjectedSales));
do Year = 1 to 3;
ProjectedSales=ProjectedSales
1.05;
output;
run;
c)data forecast;
set sashelp.shoes (rename=(Sales=ProjectedSales));
do Year = 1 to 3;
ProjectedSales=ProjectedSales*1.05;
output;
end;
run;

d)data forecast;
set sashelp.shoes (rename=(Sales=ProjectedSales));
do Year = 1 : 3;
ProjectedSales=ProjectedSales*1.05;
output;
end;
run;

A

a) data forecast;
set sashelp.shoes (rename=(Sales=ProjectedSales));
do Year = 1 to 3;
ProjectedSales=ProjectedSales*1.05;
output;
end;
run;

The correct syntax for a iterative do loop is:

DO index-column = start TO stop < BY increment >;
…..repetitive code……
RUN;

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

What will be the value of Year on the 3rd iteration of the DO LOOP in the following code?
data forecast;
set sashelp.shoes(rename=(Sales=ProjectedSales));
do Year = 1 to 3;
ProjectedSales=ProjectedSales*1.05;
output;
end;
run;

a) 3
b) 2
c) 4
d) .

A

c) 4

At the end of the third iteration of the DO loop, Year is incremented to 4. SAS determines that 4 is outside of the range of the index variable, so it exists the loop and jumps to the RUN statement. Although the value of Year is 4, the loop executed three times.

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

How many rows are written to the data set based on the code below?

data forecast;
set sashelp.shoes(rename=(Sales=ProjectedSales));
do Year = 1 to 3;
ProjectedSales=ProjectedSales*1.05;
output;
end;
run;

a) 1
b) 2
c) 3
d) 4

A

c) 3

The explicit OUTPUT statement is within the DO loop, so three rows are written for each one row read from sashelp.shoes.

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

True/False - An OUTPUT statement between the DO and END statements writes one row to the data set after the final iteration.

A

False - An OUTPUT statement between the DO and END statements writes a row for each iteration of the DO loop.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q
Which of the following is correct syntax for a DO UNTIL loop?
a) do until Savings>3000;
        Month+1;
        Savings+Amount;
        Savings+(Savings*0.02/12);
   end;
b) do until (Savings > 3000);
        Month+1;
        Savings+Amount;
        Savings+(Savings*0.02/12);
    end;
c) do until;
        Savings>3000;
        Month+1;
        Savings+Amount;
        Savings+(Savings*0.02/12);
    end;
A
b) do until (Savings > 3000);
        Month+1;
        Savings+Amount;
        Savings+(Savings*0.02/12);
    end;

There are two variations of the conditional DO loop - DO UNTIL and DO WHILE. DO UNTIL executes until a condition is true. DO WHILE executes while a condition is true. For both methods, the expression must be enclosed in parentheses.

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

True/False - The two conditional loops below will give the same result.

do until (Savings>3000);
   Month+1;
   Savings+Amount;
   Savings+(Savings*0.02/12);
end;
do while (Savings<=3000);
   Month+1;
   Savings+Amount;
   Savings+(Savings*0.02/12);
end;
A

True

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

In a DO UNTIL loop, when is the condition checked?

a) At the top of the DO loop.
b) At the bottom of the DO loop.

A

b) At the bottom of the DO loop

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

In a DO WHILE loop, when is the condition checked?

a) At the top of the DO loop.
b) At the bottom of the DO loop.

A

a) At the top of the DO loop.

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

True/False - DO WHILE loops always execute at least one time.

A

False - DO UNTIL loops always execute at least one time while DO WHILE loops do not execute even once if the condition is false.

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

Which of the following is NOT true regarding the combined conditional and iterative DO loop below?

do Month=1 to 12 until (Savings > 5000);
Savings+Amount;
Savings+(Savings*0.02/12);
end;

a) The condition is checked before the index-column is incremented at the bottom of the loop.
b) If the value of Savings is greater than 5000 the Month column is incremented one more time but the do loop is complete.
c) If the value of Savings is less than 5000, Month is incremented and the value of Month is checked against the stop value.
d) If the value of Savings is greater than 5000, the DO loop processing stops and Month is not incremented.

A

b) If the value of Savings is greater than 5000 the Month column is incremented one more time but the do loop is complete.

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

Which output table does the following step produce?

data Earnings (keep=Qtr Earned);
    Amount=1000; Rate=.075/4;
   do Qtr=1 to 4;
         Earned+(Amount+Earned)*Rate;
   end;
run;

a) Qtr Earned
4 77.135

b) Qtr Earned
5 77.135

c) Qtr Earned
    1        18.75
    2       37.85
    3       57.311
    4       77.135
d) Qtr Earned
     1          18.75
     2         37.851
     3         57.311
     4         77.135
     5         77.135
A

b) The implicit OUTPUT occurs after the DO loop. The value of QTR is 5, which is one increment beyond the stop value of 4.

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

Which statement is true regarding the iterative DO loop?

DO index-column = start TO stop ;

a) The start and stop values can be character or numeric values.
b) If an increment value is not specified, the default increment is 0.
c) The index column is incremented at the bottom of each DO loop.
d) The index column is not in the final table unless specifically kept.

A

c) The index column is incremented at the bottom of each DO loop.

The index column is incremented at the bottom of each DO loop. The start and stop values must be numeric when used with the keyword TO. The default increment is 1. The index column is in the final table unless specifically dropped.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q
How many rows are in the savings output table given the following input table and code?
Name  Amount
James     250
Linda       350
Mary        275
Robert     350
data work.savings;
      set pg2.savings;
      Savings=0;
      do Year = 1 to 5;
          do qtr=1 to 4;
                Savings+Amount;
                Savings+(Savings*0.02/12);
           end;
      end;
run;

a) 1
b) 4
c) 5
d) 20

A

b) 4

Four rows are in the output table, one row per each row in the input table. The implied OUTPUT is after the nested DO loops.

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

Which of the following statements contains valid syntax?

a) do 1 to 10 by 2;
b) do while (Year > 2025);
c) do until Earnings <= 100000;
d) do date=’01JAN2019’ to ‘31JAN2019’;

A

b) do while (Year > 2025);

When WHILE or UNTIL is used in the DO statement, the expression must be in a set of parentheses. In answer choice a, the index column is missing. In answer choice c, the parentheses are missing around the expression. In answer choice d, the DATE values are character instead of numeric (‘01JAN2019’d).

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

How many rows are in the bikeinfo2 output table given the following input table and code?

name bike
Marco 12
Angela 10

data bikeinfo2;
     set bikeinfo;
     do month=1 to 3;
         do week=1 to 4;
              bike=bike+2;
         end;
     output;
     end;
run;

a) 2
b) 3
c) 6
d) 12
e) 24

A

c) 6

For each row read in, 3 rows are created (1 for each of 3 months). So, 2 rows read * 3 months = 6 rows.

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

What is the value of x at the completion of the DATA step?

data test;
   x=15;
   do until (x>12);
         x+1;
   end;
run;

a) . (missing)
b) 13
c) 15
d) 16

A

d) 16

A DO UNTIL is evaluated at the bottom. The initial value of x is 15. The DO loop occurs one time, even though 15 is greater than 12, because the condition is not checked until the bottom of the loop. Therefore, 15 becomes 16 before the condition is checked.

17
Q

Which statement is false?

a) The DO UNTIL loop executes until a condition is true.
b) The DO WHILE loop always executes at least one time.
c) The DO WHILE loop checks the condition at the top of the loop.
d) The DO UNTIL loop checks the condition at the bottom of the loop.

A

b) The DO WHILE loop always executes at least one time.

18
Q

Which of the following statements contains valid syntax?

a) do Age=10 to 14 and while (Weight<150);
b) do week=1 to 52 do until (Mileage ge 2750);
c) do Increase=5 to 10 while (temperature lt 102);
d) do Year=2018 to 2028 or until (Earnings<=100000);

A

c) do Increase=5 to 10 while (temperature lt 102);

When combining an iterative with a conditional, you cannot use extra words such as AND, DO, or OR. The conditional immediately follows the iterative.

19
Q

Which output table does the following step produce?

data test;
    bike=10;
    do day=1 to 7 while (bike lt 13);
         bike=bike+2;
    end;
run;
a) Bike    Day
    14        2
b) Bike    Day
    14         3
c) Bike    Day
     24       7 
d) Bike    Day
    24       8
A

b) Bike Day
14 3

On the second iteration of the DO loop, Day=2 and Bike=14. At the bottom of the loop, Day becomes 3. At the top of the loop, the condition is checked. The value 14 exceeds 13, so the DO loop is over with Day=3 and Bike=14.