SAS Base (32)

Consider the following data step:

data WORK.NEW;

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。

5 thoughts on “SAS Base (32)

  1. HI Harrison,

    Your explanations on sas questions are extremely clear and effective. Thank you for your share! BTW, is this your personal website? It looks cool. How did you build it?

  2. Hi Harrison,

    Thank you for this website. I got confused in the last sentence-‘ assumed the value of count variable was missing, then Count+1 would be missing. ‘
    ‘假设Count的值为missing,Sum(Count, 1)的结果为1,而Count + 1的结果则为missing,missing value加上一个值其结果还是missing。’

    I ran the codes below and found the results would not be missing value, instead 1, when the value of count is missing.

    data a;
    input cnt;

    data b; set a;
    put cnt=; run;

    Looking forward to hearing from you.

  3. “count+1” and “count=count+1” are not the same. The first one is a sum statement, which is equivalent to “retain count 0; count=sum(count, 1)” as explained above. However, “count=count+1” is an expression instead of a sum statement. The latter one can’t ignore missing value.

Leave a Reply

Your email address will not be published. Required fields are marked *