SAS Base (54)

Consider the following data step:

data WORK.TEST;
set SASHELP.CLASS(obs=5);
retain City ‘Beverly Hills’;
State=’California’;
run;

The computed variables City and State have their values assigned using two different methods, a RETAIN statement and an Assignment statement. Which statement regarding this program is true?

A. The RETAIN statement is fine, but the value of City will be truncated to 8 bytes as the LENGTH statement has been omitted.
B. Both the RETAIN and assignment statement are being used to initialize new variables and are equally efficient. Method used is a matter of programmer preference.
C. The assignment statement is fine, but the value of City will be truncated to 8 bytes as the LENGTH statement has been omitted.
D. City’s value will be assigned one time, State’s value 5 times.

Check Answer
Answer: D

注解:Both RETAIN statement and Assignment (=) statement can be used to assign values, but there are a few differences.

1. As RETAIN statement assigns the value at compile time, the code will be executed once and only once. Assignment statement, however, respecifies the value in every iteration.
2. SAS automatically sets variables that are assigned values by an assignment statement to missing before each iteration of the DATA step. On the contrary, RETAIN statement retains the value from one iteration to the next.

AS this program reads the first 5 observations (5 iterations) in CLASS data set,  the value of State is specified 5 times, and the variable is reset to missing when SAS begins to read the next observation. City is initialized as ‘Beverly Hills’ before DATA step and won’t be reset to missing.

SAS Base (53)

The following SAS program is submitted:

data WORK.TOTAL_SALARY;
retain Total;
set WORK.SALARY;
by Department;
if First.Department
then Total=0;
Total=sum(Total, Wagerate);
if Last.Total;
run;

What is the initial value of the variable Total?

A. 0
B. Missing
C. The value of the first observations Wagerate
D. Cannot be determined from the information given

Check Answer
Answer: B

注解:The second line of codes, retain Total, initializes the variable Total.  As it doesn’t specify an initial value, a missing value is assigned to the variable. Please also check SAS Base (32) for the details of RETAIN statement.

SAS Base (32)

Consider the following data step:

data WORK.NEW;
set WORK.OLD;
Count+1;
run;

The variable Count is created using a sum statement. Which statement regarding this variable is true?
A. It is assigned a value 0 when the data step begins execution.
B. It is assigned a value of missing when the data step begins execution.
C. It is assigned a value 0 at compile time.
D. It is assigned a value of missing at compile time.

Check Answer
Answer: C

注解:Count + 1为一个Sum Statement,等价于:
RETAIN Count 0;
Count = SUM(Count, 1);

RETAIN的作用是初始化一个变量,并且在每次进入新的DATA step时,保留该变量的值。值得注意的是,RETAIN statement在编译代码时执行,即在执行DATA step(读取数据)之前,所以RETAIN statement只执行一次。SUM function的作用是求合,并且忽略missing value。上面程序第一句的意思为初始化一个叫Count的变量,并且其初始值为0。第二行的作用是将Count的值增加1,与Count = Count + 1类似,但两者有一个重要的区别,SUM function会忽略missing value,而后一种表达式则不会。假设Count的值为missing,Sum(Count, 1)的结果为1,而Count + 1的结果则为missing,missing value加上一个值其结果还是missing。