Ch 9: Creating and Managing Variables Flashcards
Given the following data set, which program creates the output shown below?
a.
data test2;
set cert.furnture;
if finish=’oak’;
if price<100 then delete;
TotalPrice+price;
drop price;
run;
proc print data=test2 noobs;
run;
b.
data test2;
set cert.furnture;
if finish=’oak’ and price<200;
TotalPrice+price;
drop price;
run;
proc print data=test2 noobs;
run;
c.
data test2;
set cert.furnture;
if finish=’oak’ and price<200 then delete;
TotalPrice+price;
drop price;
run;
proc print data=test2 noobs;
run;
d.
data test2;
set cert.furnture;
if finish=’oak’ and price<100 then do;
TotalPrice+price;
drop price;
end;
run;
proc print data=test2 noobs;
run;
Correct answer: c
Program c correctly deletes the observation in which the value of Finish is oak and the value of Price is less than 200. It also creates TotalPrice by summing the variable Price down observations, and then drops Price by using the DROP statement in the DATA step.
Consider the IF-THEN statement shown below. When the statement is executed, which expression is evaluated first?
if finlexam>=95
and (research=’A’ or
(project=’A’ and present=’A’))
then Grade=’A+’;
a.
finlexam>=95
b.
research=’A’
c.
project=’A’ and present=’A’
d.
research=’A’ or
(project=’A’ and present=’A’)
Correct answer: c
Logical comparisons that are enclosed in parentheses are evaluated as true or false before they are compared to other expressions. In the example, the AND comparison within the nested parentheses is evaluated before being compared to the OR comparison.
For the observation shown below, what is the result of the IF-THEN statements?
if status=’OK’ and type=3
then Count+1;
if status=’S’ or action=’E’
then Control=’Stop’;
a.
Count = 12 Control = Go
b.
Count = 13 Control =Stop
c.
Count = 12 Control =Stop
d.
Count = 13 Control = Go
Correct answer: b
You must enclose character values in quotation marks, and you must specify them in the same case in which they appear in the data set. The value OK is not identical to Ok, so the value of Count is not changed by the IF-THEN statement.
Which of the following can determine the length of a new variable?
a.
the length of the variable’s first reference in the DATA step
b.
the assignment statement
c.
the LENGTH statement
d.
all of the above
Correct answer: d
The length of a variable is determined by its first reference in the DATA step. When creating a new character variable, SAS allocates as many bytes of storage space as there are characters in the reference to that variable. The first reference to a new variable can also be made with a LENGTH statement or an assignment statement.
Which set of statements is equivalent to the code shown below?
if code=’1’ then Type=’Fixed’;
if code=’2’ then Type=’Variable’;
if code^=’1’ and code^=’2’ then Type=’Unknown’;
a.
if code=’1’ then Type=’Fixed’;
else if code=’2’ then Type=’Variable’;
else Type=’Unknown’;
b.
if code=’1’ then Type=’Fixed’;
if code=’2’ then Type=’Variable’;
else Type=’Unknown’;
c.
if code=’1’ then type=’Fixed’;
else code=’2’ and type=’Variable’;
else type=’Unknown’;
d.
if code=’1’ and type=’Fixed’;
then code=’2’ and type=’Variable’;
else type=’Unknown’;
Correct answer: a
You can write multiple ELSE statements to specify a series of mutually exclusive conditions. The ELSE statement must immediately follow the IF-THEN statement in your program. An ELSE statement executes only if the previous IF-THEN/ELSE statement is false.
What is the length of the variable Type, as created in the DATA step below?
data work.newloan;
set cert.records;
TotLoan+payment;
if code=’1’ then Type=’Fixed’;
else Type=’Variable’;
length type $ 10;
run;
a.
5
b.
8
c.
10
d.
It depends on the first value of Type.
Correct answer: a
The length of a new variable is determined by the first reference in the DATA step, not by data values. In this case, the length of Type is determined by the value Fixed. The LENGTH statement is in the wrong place; it must occur before any other reference to the variable in the DATA step. You can run PROC CONTENTS on the data set to see the length of each variable.
Which program contains an error?
a.
data stresstest(drop=timemin timesec);
set cert.tests;
TotalTime=(timemin*60)+timesec;
SumSec+totaltime;
run;
b.
proc print data=stresstest;
label totaltime=’Total Duration of Test’;
drop sumsec;
run;
c.
proc print data=stresstest(keep=totaltime timemin);
label totaltime=’Total Duration of Test’;
run;
d.
data stresstest;
set cert.tests;
TotalTime=(timemin*60)+timesec;
keep id totaltime tolerance;
run;
Correct answer: b
To select variables, you can use a DROP or KEEP statement in any DATA step. You can also use the DROP= or KEEP= data set options following a data set name in any DATA or PROC step. However, you cannot use DROP or KEEP statements in PROC steps.
If you submit the following program, which variables appear in the new data set?
data work.cardiac(drop=age group);
set cert.fitness(keep=age weight group);
if group=2 and age>40;
run;
a.
none
b.
Weight
c.
Age, Group
d.
Age, Weight, Group
Correct answer: b
The variables Age, Weight, and Group are specified using the KEEP= option in the SET statement. When Cert.Fitness is being read, Age, Weight, and Group are the variables that create Work.Cardiac. The variables Age and Group are specified in the DROP= option in the DATA statement. Age and Group are dropped from Work.Cardiac.
Which of the following programs correctly reads the data set Orders and creates the data set FastOrdr?
a.
data cert.fastordr(drop=ordrtime);
set cert.orders(keep=product units price);
if ordrtime<4;
Total=units*price;
run;
b.
data cert.orders(drop=ordrtime);
set cert.fastordr(keep=product units price);
if ordrtime<4;
Total=units*price;
run;
c.
data cert.fastordr(drop=ordrtime);
set cert.orders(keep=product units price
ordrtime);
if ordrtime<4;
Total=units*price;
run;
d.
none of the above
Correct answer: c
You specify the data set to be created in the DATA statement. The DROP= data set option prevents variables from being written to the data set. Because you use the variable OrdrTime when processing your data, you cannot drop OrdrTime in the SET statement. If you use the KEEP= option in the SET statement, then you must list OrdrTime as one of the variables to be kept.