**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:

1
2
3
4
5
| data WORK.TOTAL;
set WORK.INPUT;
<insert code here>
Total=Total+Cost;
run; |

data WORK.TOTAL;
set WORK.INPUT;
<insert code here>
Total=Total+Cost;
run;

Which statement correctly completes the program?

**Options:**

A. keep Total;
B. retain Total 0;
C. Total = 0;
D. If _N_= 1 then Total = 0;

**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

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

** Data Set:**

WORK.TOTAL

** 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.)

I didn’t think retain was needed with a sum statement like Total=Total+Cost;

Am I wrong?

I believe if it was total+cost it works, but it is total=total+cost

I think both options b and c works.Please response my quarry.Thank you.

It won’t work because it will set the total to 0 after every observation. Retain will hold the previous amount but initialize it to 0 when it is first created.

To Ashley: The retain function is redundant if you assign a value to a variabele in a sum statement (‘total+0’). Thus, you could have skipped the retain function using the following program:

data WORK.TOTAL;

set WORK.INPUT;

Total+0;

Total=Total+Cost;

run;

Or even easier.

data WORK.TOTAL;

set WORK.INPUT;

Total+Cost;

run;

data TOTAL;

set INPUT;

Total+Cost;

run;

total + cost

initially total=0(default it taken has zero);

cost=1;

first observation will be 1

and next total=1 and

cost=2;

second oberservation will be 3;

and why your using retain total 0;

Total+0 is inefficient as it will be executed once for every observation – Andy Perloy’s solution or the answer in the question are the best ways to achieve the outcome required