SAS Base (59)

Given the contents of the raw data file TYPECOLOR.DAT:

—-+—-10—+—-20—+—-30
daisyyellow

The following SAS program is submitted:

data FLOWERS;
infile ‘TYPECOLOR.DAT’ truncover;
length
Type $ 5
Color $ 11;
input
Type $
Color $;
run;

What are the values of the variables Type and Color?

A. Type=daisy, Color=yellow
B. Type=daisy, Color=w
C. Type=daisy, Color=daisyyellow
D. Type=daisy, Color=

Check Answer
Answer: D

注解:The code neither specified where to read the value of each variable nor the delimiter. SAS will start to read value from left to right, using the default delimiter, space(‘ ‘). SAS reads the first value ‘daisyyellow’ and assigns it to variable ‘Type’. As the length of ‘Type’ is 5 which means it can only hold up to 5 characters, the value is truncated to ‘daisy’. Then SAS tries to read the second value for ‘Color’, but there is nothing left in the file. SAS simply sets the value to missing.

One of the proper way to read the file would be telling SAS the position of each variable:

data FLOWERS;
infile ‘TYPECOLOR.DAT’;
length
Type $ 5
Color $ 11;
input
Type $ 1-5
Color $ 6-11;
run;

Obs Type Color
1 daisy yellow

If you were not sure the length of the second variable, TRUNCOVER option would be handy. It enables you to read variable-length records when some records are shorter than the INPUT statement expects.

The following program will produce the identical result, except that the value of ‘Color’ could be any length from 0 to 15.

data FLOWERS;
infile ‘TYPECOLOR.DAT’ truncover;
length
Type $ 5
Color $ 11;
input
Type $ 1-5
Color $ 6-20;
run;

SAS Base (58)

The following program is submitted:

proc format;
value salfmt. 0 -< 50000 = ‘Less than 50K’ 50000 – high = ’50K or Greater’;

options fmterr nodate pageno=1;
title ‘Employee Report’;

proc print data=work.employees noobs;
var fullname salary hiredate;
format salary salfmt. hiredate date9.;
label fullname=’Name of Employee’ salary=’Annual Salary’ hiredate=’Date of Hire’;
run;

Why does the program fail?

A. The PAGENO option is invalid in the OPTIONS statement.
B. The RUN statement is missing after the FORMAT procedure.
C. The format name contains a period in the VALUE statement.
D. The LABEL option is missing from the PROC PRINT statement.

Check Answer
Answer: C

注解:When defining custom formats, the name of formats cannot end in a number or a period. The name, ‘salfmt.’, in VALUE statement is invalid. However, when you refer to the custom format, the ending period is necessary.

SAS Base (55)

The following SAS program is submitted:

data WORK.DATE_INFO;
X=”01Jan1960″D ;
run;

Variable X contains what value?

A. the numeric value 0
B. the character value “01Jan1960”
C. the date value 01011960
D. the code contains a syntax error and does not execute.

Check Answer
Answer: A

注解:Letter D is used to convert a normal date in DDMMMYY or DDMMMYYYY format to SAS date value. Check SAS Base (16) for details.

This question has another version. A space is placed between the closing quotation mark and letter D. This will cause a compile error and the answer would be D.

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

Given the SAS data set WORK.EMP_NAME:

Name EmpID
Jill 1864
Jack 2121
Joan 4698
John 5463

Given the SAS data set WORK.EMP_DEPT:

EmpID Department
2121 Accounti
3567 Finance
4698 Marketin
5463 Accounti

The following program is submitted:

data WORK.ALL;
merge WORK.EMP_NAME(in=Emp_N)
WORK.EMP_DEPT(in=Emp_D);
by Empid;
if (Emp_N and not Emp_D) or (Emp_D and not Emp_N);
run;

How many observations are in data set WORK.ALL after submitting the program?

A. 1
B. 2
C. 3
D. 5

Check Answer
Answer: B

注解:The new data set WORK.ALL only contains observation which exits in EMP_Name but not in EMP_DEPT, or the other way around. WORK.ALL is:

Obs Name EmpID Department
1 Jill 1864
2 3567 Finance

For the details of MERGE statement and IN option, please check SAS Base (28).

SAS Base (51)

The following program is submitted:

proc contents data=_all_;
run;

Which statement best describes the output from the submitted program?

A. The output contains only a list of the SAS data sets that are contained in the WORK library.
B. The output displays only the contents of the SAS data sets that are contained in the WORK library.
C. The output displays only the variables in the SAS data sets that are contained in the WORK library.
D. The output contains a list of the SAS data sets that are contained in the WORK library and displays the contents of those data sets.

Check Answer
Answer: D

注解:The CONTENTS procedure shows the contents of a SAS data set and prints the directory of the SAS library. As the libref is omitted, SAS uses the default library WORK, and  _ALL_ represents all data sets in that library. SAS lists the data sets in WORK library followed by the contents of each data set.

SAS Base (50)

Given the SAS data set WORK.ONE:

Id Char1
111 A
158 B
329 C
644 D

and the SAS data set WORK.TWO:

Id Char2
111 E
538 F
644 G

The following program is submitted:

data WORK.BOTH;
set WORK.ONE WORK.TWO;
by Id;
run;

What is the first observation in SAS data set WORK.BOTH?

A.

Id Char1 Char2
111 A

B.

Id Char1 Char2
111 E

C.

Id Char1 Char2
111 A  E

D.

Id Char1 Char2
644 D G
Check Answer
Answer: A

注解:SET statement的作用是将多个data set合并,但不会对观测值进行合并。题目中,新data set BOTH中将包含ONE和TWO中所有的变量,BY statement作用是使新data set根据ID升序排列,所以ONE中的第一条数据将成为BOTH中的第一条数据,由于该观测值中没有Char2变量,所以输出结果中显示为missing。完整的BOTH为:

Obs Id Char1 Char2
1 111 A
2 111 E
3 158 B
4 329 C
5 538 F
6 644 D
7 644 G

如需对观测值合并,应当使用MERGE,具体请参考SAS Base (28)

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这个变量。