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 (48)

The following SAS program is submitted:

data WORK.TEST;
drop City;
infile datalines;
input Name $ 1-14 / Address $ 1-14 / City $ 1-12 ;
if City=’New York ‘ then input @1 State $2.;
else input;
datalines;
Joe Conley
123 Main St.
Janesville
WI
Jane Ngyuen
555 Alpha Ave.
New York
NY
Jennifer Jason
666 Mt. Diablo
Eureka
CA
;
run;

What will the data set WORK.TEST contain?

A.

Obs Name Address State
1 Joe Conley 123 Main St.
2 Jane Ngyuen 555 Alpha Ave. NY
3 Jennifer Jason 666 Mt. Diablo

B.

Obs Name Address City State
1 Joe Conley 123 Main St. Janesville
2 Jane Ngyuen 555 Alpha Ave. New York NY
3 Jennifer Jason 666 Mt. Diablo Eureka

C.

Obs Name Address State
1 Jane Ngyuen 555 Alpha Ave. NY

D. No observations, there is a syntax error in the data step.

Check Answer
Answer: A

注解:INPUT statement中,$表示这是一个字符型变量;1-14表示将该行的第1-14个字符写进变量;/表示将指针移动到下一行的第一个字符,即之后的变量将从下二行的第一个字符开始读取。INPUT statement默认自动前往下一行,当INPUT后没有申明任何变量名时,其起到的作用和/类似,但两者的工作原理不同,具体请查看官方手册中INPUT statement的Without Arguments部分。题目中的程序首先逐行读取Name,Address和City,只有在City为New York时读取State的值,最后DROP statement告诉SAS不要向data set中写入City这个变量。

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。