SAS Base – Sample Question 7

This item will ask you to provide a line of missing code;

The SAS data set WORK.INPUT contains 10 observations, and includes the numeric variable Cost.

The following SAS program is submitted to accumulate the total value of Cost for the 10 observations:

data WORK.TOTAL;
  set WORK.INPUT;

  Total=Total+Cost;
run;

Which statement correctly completes the program?

  1. keep Total;
  2. retain Total 0;
  3. Total = 0;
  4. If _N_= 1 then Total = 0;
[expand title=”See Answer and Commentary” tag=”h4″]


correct_answer = “B”

Commentary:
You can see that the retain statement serves to increment.  For each observation, it adds the current value of Cost to the running total for Total.  As the objective of the program states, this is what we are trying to do and this is a very useful feature of the retain statement.  (Its other general purpose is to reorder variables in a data set.)

Log:

41   data WORK.TOTAL;
42     set WORK.INPUT;
43     retain Total 0;
44     Total=Total+Cost;
45   run;

NOTE: There were 10 observations read from the data set WORK.INPUT.
NOTE: The data set WORK.TOTAL has 10 observations and 2 variables.
NOTE: DATA statement used (Total process time):
      real time           0.00 seconds
      cpu time            0.00 seconds
Data Set WORK.INPUT:
[table class=”table table-bordered”] [attr style=”background-color: #c1ccc6″],Cost[attr style=”background-color: #c1ccc6″] 1[attr style=”background-color: #c1ccc6″],1
2[attr style=”background-color: #c1ccc6″],2
3[attr style=”background-color: #c1ccc6″],3
4[attr style=”background-color: #c1ccc6″],4
5[attr style=”background-color: #c1ccc6″],5
6[attr style=”background-color: #c1ccc6″],6
7[attr style=”background-color: #c1ccc6″],7
8[attr style=”background-color: #c1ccc6″],8
9[attr style=”background-color: #c1ccc6″],9
10[attr style=”background-color: #c1ccc6″],10
[/table]
Data Set WORK.TOTAL:
[table class=”table table-bordered”] [attr style=”background-color: #c1ccc6″],Cost[attr style=”background-color: #c1ccc6″],Total[attr style=”background-color: #c1ccc6″] 1[attr style=”background-color: #c1ccc6″],1,1
2[attr style=”background-color: #c1ccc6″],2,3
3[attr style=”background-color: #c1ccc6″],3,6
4[attr style=”background-color: #c1ccc6″],4,10
5[attr style=”background-color: #c1ccc6″],5,15
6[attr style=”background-color: #c1ccc6″],6,21
7[attr style=”background-color: #c1ccc6″],7,28
8[attr style=”background-color: #c1ccc6″],8,36
9[attr style=”background-color: #c1ccc6″],9,45
10[attr style=”background-color: #c1ccc6″],10,55
[/table]
[/expand]

*The SAS certification sample problem and answer above are sourced from the official SAS sample questions page. All answer logs, data sets, output, and commentary are provided by this blog.

Click to see Answer Explanation below

thanks for your support

Comments are closed.