2.6 Flashcards
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=ProjectedSales1.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) 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;
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) .
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 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
c) 3
The explicit OUTPUT statement is within the DO loop, so three rows are written for each one row read from sashelp.shoes.
True/False - An OUTPUT statement between the DO and END statements writes one row to the data set after the final iteration.
False - An OUTPUT statement between the DO and END statements writes a row for each iteration of the DO loop.
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;
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.
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;
True
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.
b) At the bottom of the DO loop
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) At the top of the DO loop.
True/False - DO WHILE loops always execute at least one time.
False - DO UNTIL loops always execute at least one time while DO WHILE loops do not execute even once if the condition is false.
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.
b) If the value of Savings is greater than 5000 the Month column is incremented one more time but the do loop is complete.
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
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.
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.
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 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
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.
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’;
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 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
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.