SAS Base (49)

The following SAS program is submitted:

data WORK.TOTALSALES(keep=MonthSales{12});
set WORK.MONTHLYSALES(keep=Year Product Sales);
array MonthSales{12};
do i=1 to 12;
MonthSales{i}=Sales;
end;
drop i;
run;

The program fails execution due to syntax errors.
What is the cause of the syntax error?
A. An array cannot be referenced on a keep= data set option.
B. The keep= data set option should be (keep=MonthSales*).
C. The keep= data set option should be the statement KEEP MonthSales{12}.
D. The variable MonthSales does not exist.

Check Answer
Answer:  A

注解:KEEP和DROP的对象不能是数组,只能是变量。如果希望去掉数组中的变量,可以直接引用该变量的名字。例如题目中,希望仅保留数组中第12个元素,即MonthSales{12},可以使用以下KEEP option: KEEP = MonthSales12,如果想保留整个数组:KEEP = MonthSales1 – MonthSales12。

SAS Base (6)

After a SAS program is submitted, the following is written to the SAS log:

101 data WORK.JANUARY;
102 set WORK.ALLYEAR(keep=product month num_Sold Cost);
103 if Month=’Jan’ then output WORK.JANUARY;
104 Sales=Cost * Num_Sold;
105 keep=Product Sales;
-----
22
ERROR 22-322: Syntax error, expecting one of the following: !,!!, &, *, **, +, -, , <=, <>, =, >, >=, AND, EQ, GE, GT, IN, LE, LT, MAX, MIN, NE, NG, NL,NOTIN, OR, ^=, |, ||, ~=.
106 run;

What changes should be made to the KEEP statement to correct the errors in the LOG?
A. keep=(Product Sales);
B. keep Product, Sales;
C. keep=Product, Sales;
D. keep Product Sales;

Check Answer
Answer: D

注解:KEEP用于指定需要包括在data set中的变量。与之相反的是DROP,用于指定那些不需要的变量。KEEP和DROP既可作为option跟在DATA statement或者SET statement后面,也可以作为独立的statement出现。当作为option时,需要写在括号内,并跟上一个等号,比如:(keep = variable1 variable2)。当作为statement时,不需要括号也不需要等号,比如:keep variable1 variable2。