Answer Key

advertisement
Statistics 6250
Spring 2013
Prof. Fan
Name:__________________
(print: first
last )
NetID #:________________
Midterm Two
Instructions: This is an in-class and open book midterm. No internet access (except our
class webpage) is allowed. You must print your answers on the given spaces. All data sets are
on the class webpage: www.sci.csueastbay.edu/~sfan, then click teaching and STAT 6250.
1. A character array that contains three variables (name1, name2, and name3) with the values
of Smith, Jones, and Westinghouse is requested.
Which ARRAY statement will create the desired array?
a. array name{3} $ ('Smith','Jones','Westinghouse');
b. array name1-name3 $ ('Smith' 'Jones' 'Westinghouse');
c. array name{3} name1-name3 ('Smith' 'Jones' 'Westinghouse');
d. array name(3) $ 12 ('Smith','Jones','Westinghouse');
Answer: d
2. Which assignment statement will produce a value for FULLNAME with a comma between the
LASTNAME and FIRSTNAME?
a. FULLNAME = CATS(',', LASTNAME, FIRSTNAME);
b. FULLNAME = CATX(',', LASTNAME, FIRSTNAME);
c. FULLNAME = CATS(LASTNAME, FIRSTNAME, ',');
d. FULLNAME = CATX(LASTNAME, FIRSTNAME, ',');
Answer: b
3. The following SAS program is submitted:
proc print data=account;
<insert statement here>
run;
The following list report containing the character variable name and the numeric variables
date and amount is generated:
Obs
name
date
amount
1
2
JACK
SUE
16MAY2007
17JUL2006
$123,830.23
$89,654.05
Which SAS statement created the report?
a. format date date9. amount dollar11.2;
1
b. format date ddmmyy9. amount dollar11.2;
c. format date ddmmmyyyy9. amount comma11.2;
d. format date date9. amount dollarcomma11.2;
Answer: a
4. Which of the following is true regarding the SCAN function?
a.
The SCAN function only uses two default delimiters (the blank and the comma) if a
delimiter is not specified.
b.
The SCAN function returns a missing value if the number of the word scanned is greater
than the number of words in the character string.
c.
The SCAN function has an optional forth argument, which is the direction (forward or
backward) to read the string.
d.
If the SCAN function returns a value to a variable that was not yet assigned a length, the
variable length is determined by the length of the first argument.
Answer: b
5. Below is the input data set work.employees:
Below is the input data set work.salaries:
Below is the output data set work.empsal:
Add ONE appropriate SAS statement to the following program to create the output data set
based on the input data sets:
data empsal;
2
merge employees(in=emp rename=(id=idnum))
salaries(in=sal);
by idnum;
if emp=1 and sal=1;
run;
6. The following is the input data set, Weekly:
The following is the desired output data set, WeeklyRotate:
Complete the following program to create the desired output data set:
data WeeklyRotate;
set Weekly;
array run{4} week1 - week4;
do week = 1 to 4;
miles = run{week};
output;
end;
drop week1 - week4;
run;
7. The dataset phoneall contains four variables: name, phone number and height of
customers.
*Data set phoneall;
data phoneall;
input Name $20.
Phone & $14.
3
Height & $10.;
datalines;
Roger
Cody
(908)782-1234 5ft. 10in.
Thomas Jefferson
(315) 848-8484 6ft. 1in.
Marco Polo
(800)123-4567 5Ft. 6in.
;
run;
Use “SET” to create a new SAS dataset “newphone” containing Height and the following new
variables:




FirstName:
LastName:
AreaCode:
LocalCode:
the first name of a customer (eg. Roger)
the last name of a customer (eg. Cody)
the variable of the 3-digit area code (eg. 908)
the 7 digits of the local phone number (eg. 7821234)
Drop name and phone. Write down your SAS code.
data newphone;
set phoneall;
firstname=scan(name,1);
lastname=scan(name,-1);
phone=compress(phone,,'kd');
areacode=substr(phone,1,3);
localcode=substr(phone,4,10);
drop name phone;
run;
(b). Use “SET” to replace the variable Height (of character type) in dataset “newphone” by
height in inches (of numeric type). For example, since 5ft. 10 in. = 5*12+10=70 in., the new
Height would be value 70. Hint: extract the number of ft. and the number of in. first.
Write down your SAS code.
data newphone2;
set newphone(rename=(height=height_char));
feet=input(compress(scan(height_char,1),,'kd'),8.);
inches=input(compress(scan(height_char,2),,'kd'),8.);
if not missing(inches) then height=12*feet+inches;
else height=12*feet;
drop height_char feet inches;
run;
4
8. Answer the questions based on the following program:
data newprice;
infile 'raw-data-file';
input mfg $ type $ price;
length saletype $ 18;
if mfg='Crew' then do;
pct=0.75;
saleprice = price * pct;
saletype = '25% off';
end;
else if mfg='Hi-fly' then do;
pct=0.70;
saleprice = price * pct;
saletype = '30% off';
end;
else do;
pct=0.90;
saleprice = price * pct;
saletype = '10% Storewide Sale';
end;
format price saleprice dollar8.2;
run;
a. Draw a flow chart of this program.
Answer: will show in class
b. What would be the byte size of saletype if the LENGTH statement were not part of
the program? What problem will we encounter without the LENGTH statement?
Answer: 8 bytes. Without the length statement the ‘10% storewide sale’ will be too long and
so be truncated.
5
Download