SAS Base Certification – Question 7

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; 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.

Correct Answer Incorrect Answer Click to see Answer Explanation below  Ashley Hill Oct 14, 2015

I didn’t think retain was needed with a sum statement like Total=Total+Cost;
Am I wrong? mcypert16 Feb 29, 2016

I believe if it was total+cost it works, but it is total=total+cost Kesu Kaltol Dec 05, 2015

I think both options b and c works.Please response my quarry.Thank you. Rachael Nov 28, 2016

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. Andy Perloy Dec 10, 2016

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; Andy Perloy Dec 10, 2016

Or even easier.

data WORK.TOTAL;
set WORK.INPUT;
Total+Cost;
run; saireddy May 14, 2017

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; ishmo65 Jun 16, 2017