You are on page 1of 80

SAS® SQL 1:

Essentials

Lesson Quizzes
SAS and all other SAS Institute Inc. product or service names are registered trademarks or
trademarks of SAS Institute Inc. in the USA and other countries. ® indicates USA registration. Other
brand and product names are trademarks of their respective companies.

SAS® SQL 1: Essentials – Lesson Quizzes

Copyright © 2019 SAS Institute Inc. Cary, NC, USA. All rights reserved. Printed in the United
States of America. No part of this publication may be reproduced, stored in a retrieval system, or
transmitted, in any form or by any means, electronic, mechanical, photocopying, or otherwise,
without the prior written permission of the publisher, SAS Institute Inc.

Book code E714091, course code SQ1M6LQ, prepared date 11Dec2019. SQ1M6LQ_001
Lesson 1 Lesson Quizzes
1.1 Quizzes........................................................................................................................ 1-3
Lesson 1 – Essentials.............................................................................................. 1-3
Lesson 2 – PROC SQL Fundamentals ....................................................................... 1-8
Lesson 3 – SQL Joins............................................................................................ 1-16
Lesson 4 – Subqueries .......................................................................................... 1-21
Lesson 5 – Set Operators....................................................................................... 1-26
Lesson 6 – Using and Creating Macro Variables in SQL.............................................. 1-31
Lesson 7 – Accessing DBMS Data with SAS/ACCESS ............................................... 1-36

1.2 Solutions ................................................................................................................... 1-39


Lesson 1 ............................................................................................................. 1-39
Lesson 2 ............................................................................................................. 1-45
Lesson 3 ............................................................................................................. 1-53
Lesson 4 ............................................................................................................. 1-59
Lesson 5 ............................................................................................................. 1-65
Lesson 6 ............................................................................................................. 1-70
Lesson 7 ............................................................................................................. 1-76
1-2

Copyright © 2019, SAS Institute Inc., Cary, North Carolina, USA. ALL RIGHTS RESERVED.
1-3

1.1 Quizzes
Lesson 1 – Essentials

1. You want to explore the first 10 rows of the sq.profit table. Which PROC
SQL option limits the rows from the sq.profit table that contribute to
the query?

a. OUTOBS=10
b. OBS=10
c. ROW=10
d. INOBS=10

3
C o p y r i g h t © S AS In s t i tu t e In c. Al l r i g h t s re s e r ve d .

2. When using only the SQL procedure, you can create which of the
following objects?

a. SAS tables
b. SAS views
c. DBMS tables
d. all of the above

5
C o p y r i g h t © S AS In s t i tu t e In c. Al l r i g h t s re s e r ve d .

Copyright © 2019, SAS Institute Inc., Cary, North Carolina, USA. ALL RIGHTS RESERVED.
1-4

3. You want to display the column name, column types, and other column
information from the sq.profit table. Which statement should you use?

a. content sq.profit;
b. contents table sq.profit;
c. describe sq.profit;
d. describe table sq.profit;

7
C o p y r i g h t © S AS In s t i tu t e In c. Al l r i g h t s re s e r ve d .

4. Which PROC SQL option would you use to produce a report that includes
the row number in the report?

a. NUMBER
b. OBS
c. ROWNUM
d. ROW

9
C o p y r i g h t © S AS In s t i tu t e In c. Al l r i g h t s re s e r ve d .

Copyright © 2019, SAS Institute Inc., Cary, North Carolina, USA. ALL RIGHTS RESERVED.
1-5

5. Which of the following SELECT statements would you use to produce


a report that includes every column from the sashelp.cars table?

a. SELECT ALL
b. SELECT _COLUMNS_
c. SELECT *
d. SELECT _ALL_

11
C o p y r i g h t © S AS In s t i tu t e In c. Al l r i g h t s re s e r ve d .

6. You have been programming in a specific database implementation of


SQL and want to begin using PROC SQL. All of your previous knowledge
of keywords, features, and enhancements will transfer over when using
PROC SQL.

a. true
b. false

13
C o p y r i g h t © S AS In s t i tu t e In c. Al l r i g h t s re s e r ve d .

Copyright © 2019, SAS Institute Inc., Cary, North Carolina, USA. ALL RIGHTS RESERVED.
1-6

7. You want to produce a report that selects the Make, Model, and
MPG_City columns from the sashelp.cars table. Which of the following
queries is correct?
a. select Make, Model, MPG_City
from sashelp.cars;
b.
select Make Model MPG_City
from sashelp.cars;
c.
from sashelp.cars
select Make Model MPG_City;
d. from sashelp.cars
select Make, Model, MPG_City;

15
C o p y r i g h t © S AS In s t i tu t e In c. Al l r i g h t s re s e r ve d .

8. In orion.sales, the columns appear in this order: Employee_ID,


First_Name, Last_Name, Gender, Salary, Job_Title, Country,
Birth_Date, Hire_Date. When you run the following query, in what order
do columns appear in the report?
proc sql;
select Last_Name, First_Name
from orion.sales;
quit;

a. First_Name, Last_Name
b. Last_Name, First_Name
c. Last_Name, First_Name, Job_Title

17
C o p y r i g h t © S AS In s t i tu t e In c. Al l r i g h t s re s e r ve d .

Copyright © 2019, SAS Institute Inc., Cary, North Carolina, USA. ALL RIGHTS RESERVED.
1-7

9. In your PROC SQL code, you can include SAS features that do not
conform to the ANSI standard.

a. true
b. false

19
C o p y r i g h t © S AS In s t i tu t e In c. Al l r i g h t s re s e r ve d .

10. If present, the clauses in a SELECT statement must be in which order?

a. SELECT, FROM, WHERE, GROUP BY, ORDER BY, HAVING


b. SELECT, FROM, WHERE, GROUP BY, HAVING, ORDER BY

21
C o p y r i g h t © S AS In s t i tu t e In c. Al l r i g h t s re s e r ve d .

Copyright © 2019, SAS Institute Inc., Cary, North Carolina, USA. ALL RIGHTS RESERVED.
1-8

Lesson 2 – PROC SQL Fundamentals

1. Which SELECT statement creates a report that displays only the unique
combinations of Customer_Type_ID and Country?
a. select distinct Customer_Type_ID Country
from orion.customer;

b. select distinct Customer_Type_ID, Country


from orion.customer;

c. select distinct Customer_Type_ID, distinct Country


from orion.customer;

d. select Customer_Type_ID, Country


from orion.customer
where distinct Customer_Type_ID and Country;

24
C o p y r i g h t © S AS In s t i tu t e In c. Al l r i g h t s re s e r ve d .

2. Which WHERE clause subsets rows based on NewSalary, an alias


created in the SELECT clause?
a. where 75000<=Salary calculated <=100000

b. where NewSalary>=75000 and NewSalary<=100000

c. where 75000<= calculated Salary<=100000

d. where 75000<=calculated NewSalary<=100000

26
C o p y r i g h t © S AS In s t i tu t e In c. Al l r i g h t s re s e r ve d .

Copyright © 2019, SAS Institute Inc., Cary, North Carolina, USA. ALL RIGHTS RESERVED.
1-9

3. Which of the following options uses an ANSI standard column modifier


to create a label that is displayed as the heading for a column in the
output?
a. 'Employee Number'

b. Employee Number

c. Label='Employee ID'

d. Header=Employee Reference Number

28
C o p y r i g h t © S AS In s t i tu t e In c. Al l r i g h t s re s e r ve d .

4. Which of the following expressions creates a new column in the SELECT


clause?
a. Salary *.01 as Increase

b. Bonus=Salary*.03

c. Job_Level as Scan (Job_Title,1,' ')

d. Salary*.03 Bonus

30
C o p y r i g h t © S AS In s t i tu t e In c. Al l r i g h t s re s e r ve d .

Copyright © 2019, SAS Institute Inc., Cary, North Carolina, USA. ALL RIGHTS RESERVED.
1-10

5. To guarantee the order of the rows in the output, you must use an
ORDER BY clause.

a. true
b. false

32
C o p y r i g h t © S AS In s t i tu t e In c. Al l r i g h t s re s e r ve d .

6. If the values returned by the YEAR function are . (missing), 2018, 2019,
and 2020, which value for year is displayed first in the output based on
this code?
proc sql;
select Employee_ID, Employee_Term_Date format=date9.,
year(Employee_Term_Date) as year
from orion.employee_payroll
where Employee_Gender='M' and Marital_Status='S'
order by year;
quit;

a. . (missing)
b. 2018
c. 2019
d. 2020 34
C o p y r i g h t © S AS In s t i tu t e In c. Al l r i g h t s re s e r ve d .

Copyright © 2019, SAS Institute Inc., Cary, North Carolina, USA. ALL RIGHTS RESERVED.
1-11

7. Which of the following programs positions the TITLE statement correctly


so that the title appears in the output?
a. proc sql;
select Employee_ID from orion.order_fact;
quit;
title 'Manager Report';
b. proc sql;
title 'Manager Report';
select Employee_ID from orion.order_fact;
quit;
c. proc sql;
select Employee_ID from orion.order_fact;
title 'Manager Report';
quit;

36
C o p y r i g h t © S AS In s t i tu t e In c. Al l r i g h t s re s e r ve d .

8. Which of the following CASE expressions is correct?


a. case Age
when 20 <= Age <= 29 then '20-29';

b. case
when 20 <= Age <= 29 then '20-29';

38
C o p y r i g h t © S AS In s t i tu t e In c. Al l r i g h t s re s e r ve d .

Copyright © 2019, SAS Institute Inc., Cary, North Carolina, USA. ALL RIGHTS RESERVED.
1-12

9. In this PROC SQL program, the two SELECT statements use the
orion.employee_donations table and generate the same results.

proc sql; employee_donations


select count(*) Employee_ID Qtr1 Qtr2 Qtr3 Qtr4
from orion.employee_donations;
120265 . . . 25
select count(Qtr1)
from orion.employee_donations; 120267 15 15 15 15
quit;
120269 20 20 20 20
a. true 120270 20 10 5 .
b. false

40
C o p y r i g h t © S AS In s t i tu t e In c. Al l r i g h t s re s e r ve d .

10. The product_dim table contains 481 rows—that is, 1 row for each
product that Orion Star sells. All Orion Star products are assigned to
1 of 50 product groups. Which SELECT statement creates a report
that shows the number of products in each product group?
a. select Product_Group, count(*) as Products
from orion.product_dim;

b. select Product_Group, count(*) as Products


from orion.product_dim
group by Product_Group;

c. select Product_Group, count(*) as Products


from orion.product_dim
group by Product_Group, Products;
42
C o p y r i g h t © S AS In s t i tu t e In c. Al l r i g h t s re s e r ve d .

Copyright © 2019, SAS Institute Inc., Cary, North Carolina, USA. ALL RIGHTS RESERVED.
1-13

11. To produce a report that includes all Toyotas with a Price value less than
$30,000, which WHERE clause would you use?
Make Model Price MPG
Toyota RAV4 $28,750 23
Honda Accord $19,350 32
Honda Pilot $32,500 22
BMW 135i $25,200 24

a. where Make = "Toyota" and Price < $30,000

b. where Make = "Toyota" or Price < 30000

c. where Make = "Toyota" and Price < 30000


d. where Make = "Toyota" or
44
Price < $30,000
C o p y r i g h t © S AS In s t i tu t e In c. Al l r i g h t s re s e r ve d .

12. In which of the following queries can you replace the SAS summary
function MEAN with the ANSI summary function AVG and get the
same results?
a. select mean(Qtr1, Qtr2, Qtr3, Qtr4) as Mean_Annual
from orion.employee_donations;

b. select Employee_ID, mean(Qtr1, Qtr2, Qtr3, Qtr4) as Mean_Annual


from orion.employee_donations;

c. select mean(of Qtr1:) as Mean_Qtr_1


from orion.employee_donations;

d. select mean(Qtr1) as Mean_Qtr_1


from orion.employee_donations;
46
C o p y r i g h t © S AS In s t i tu t e In c. Al l r i g h t s re s e r ve d .

Copyright © 2019, SAS Institute Inc., Cary, North Carolina, USA. ALL RIGHTS RESERVED.
1-14

13. You want to count the number of employees in each department and
return only the departments with more than 30 employees. Which
query produces the correct report?
a. select Department, count(*) as TotalEmployees
from orion.employees
where calculated TotalEmployees > 30
group by Department;

select Department, count(*) as TotalEmployees


b. from orion.employees
having TotalEmployees > 30
group by Department;

select Department, count(*) as TotalEmployees


from orion.employees
c. group by Department
having TotalEmployees > 30;
48
C o p y r i g h t © S AS In s t i tu t e In c. Al l r i g h t s re s e r ve d .

14. Which of the following statements creates a new table named results
from a query?
a. create table results like
select ...

b. create table results as


select ...

c. create table results


(select ...)

d. create table results from


select ...

50
C o p y r i g h t © S AS In s t i tu t e In c. Al l r i g h t s re s e r ve d .

Copyright © 2019, SAS Institute Inc., Cary, North Carolina, USA. ALL RIGHTS RESERVED.
1-15

15. Which of the following queries produces a report of all columns from
every table in the Sashelp library?
a. select *
from dictionaries.columns
where libname="SASHELP";

b. select *
from sashelp.vcolumn
where libname="SASHELP";

c. select *
from dictionary.columns
where libname="SASHELP";

52
C o p y r i g h t © S AS In s t i tu t e In c. Al l r i g h t s re s e r ve d .

Copyright © 2019, SAS Institute Inc., Cary, North Carolina, USA. ALL RIGHTS RESERVED.
1-16

Lesson 3 – SQL Joins

1. How many rows does the following query produce in the report?

select *
from sq.employee_donations,
sq.employees;

a. 4 employee_donations employees
b. 5 Employee_ID Qtr1 Qtr2 Qtr3 Qtr4 Employee_ID Name
c. 9 120265 . . . 25 120265 Panagiotis
d. 20 120267 15 15 15 15 120267 Emma
120269 20 20 20 20 120269 Fatima
120270 20 10 5 . 120270 Sofia
120272 Owen
55
C o p y r i g h t © S AS In s t i tu t e In c. Al l r i g h t s re s e r ve d .

2. Which code produces a report of only the matches from the


employee_donations and employees tables?
The results should include each column once. employee_donations
a. select Employee_ID, Qtr1, Qtr2, Qtr2, Qtr3, Employee_ID Qtr1 Qtr2 Qtr3 Qtr4
Name
from employee_donations as d inner join 120265 . . . 25
employees as e 120267 15 15 15 15
on Employee_ID=Employee_ID;
120269 20 20 20 20
b. select e.Employee_ID, Qtr1, Qtr2, Qtr2, 120270 20 10 5 .
Qtr3, Name
from employee_donations as d inner join
employees as e
employees
on d.Employee_ID=e.Employee_ID; Employee_ID Name

c. select *
120265 Panagiotis
from employee_donations as d inner join 120267 Emma
employees as e
on d.Employee_ID=e.Employee_ID; 120269 Fatima
120270 Sofia
57 120272 Owen
C o p y r i g h t © S AS In s t i tu t e In c. Al l r i g h t s re s e r ve d .

Copyright © 2019, SAS Institute Inc., Cary, North Carolina, USA. ALL RIGHTS RESERVED.
1-17

3. Which program correctly displays a list of qualified column names


in the SAS log?
a. proc sql;
describe table acme.staff;
quit;
b. proc sql noexec;
select * from acme.staff;
quit;
c. proc sql list;
select * from acme.staff;
quit;
d. proc sql feedback;
select * from acme.staff;
quit; 59
C o p y r i g h t © S AS In s t i tu t e In c. Al l r i g h t s re s e r ve d .

4. Which result set does the following inner join produce?


products saleitems
select p.Prod_ID, Color, Price
from products as p inner join Prod_ID Color Prod_ID Price
saleitems as s 1 Blue 1 12.99
on p.Prod_ID=s.Prod_ID
2 Red 4 45.99
and Color='Blue';
3 Red
4 Yellow
5 Blue
6 Yellow

a. Result Set 1 b. Result Set 2 c. Result Set 3


Prod_ID Color Prod_ID Price Prod_ID Color Price Prod_ID Color Price
1 Blue 1 12.99 1 Blue 12.99 1 Blue 12.99
5 Blue 5 . 5 Blue .
61
C o p y r i g h t © S AS In s t i tu t e In c. Al l r i g h t s re s e r ve d .

Copyright © 2019, SAS Institute Inc., Cary, North Carolina, USA. ALL RIGHTS RESERVED.
1-18

5. Which result set does the following inner join produce?


products saleitems
select p.Prod_ID, Color, Price
from products as p inner join Prod_ID Color Prod_ID Price
saleitems as s 1 Blue 1 12.99
on p.Prod_ID=s.Prod_ID;
2 Red 4 45.99
3 Red . .99
. Green . 1.99
a. Result Set 1
. Blue
Prod_ID Color Price
1 Blue 12.99 b. Result Set 2 c. Result Set 3
. Green .99 Prod_ID Color Price Prod_ID Color Price
. Green 1.99 1 Blue 12.99 1 Blue 12.99
. Blue .99 . Green .99
. Blue 1.99 . Blue 1.99
63
C o p y r i g h t © S AS In s t i tu t e In c. Al l r i g h t s re s e r ve d .

6. Using the products and producttiers tables, complete the following


program to produce the desired results of a product and its tier.
select p.Prod_ID, Price, Tiers products producttiers
from products as p inner join Prod_ID Price Tiers Low High
productTiers as t 1 125.99 Tier 1 0 9.99
on _________________
order by p.Prod_ID; 2 90.99 Tier 2 10 99.99
3 1.99 Tier 3 100 9999.99
a. 4 11.99
p.Price <= t.High Desired Results
5 1099.99 Prod_ID Price Tiers
b. p.Price >= t.Low 6 10.99 1 125.99 Tier 3
2 90.99 Tier 2
c. p.Price >= t.Low or p.Price <= t.High 3 1.99 Tier 1
4 11.99 Tier 2
d. p.Price >= t.Low and p.Price <= t.High 5 1099.99 Tier 3
65 6 10.99 Tier 2
C o p y r i g h t © S AS In s t i tu t e In c. Al l r i g h t s re s e r ve d .

Copyright © 2019, SAS Institute Inc., Cary, North Carolina, USA. ALL RIGHTS RESERVED.
1-19

7. Which result set does the following left join produce?


products saleitems
select p.Prod_ID, Color, Price
from products as p left join Prod_ID Color Prod_ID Price
saleitems as s 1 Blue 1 12.99
on p.Prod_ID=s.Prod_ID;
2 Red 4 45.99
3 Red 6 99.99
a. Result Set 1 b. Result Set 2
4 Green
Prod_ID Color Price Prod_ID Color Price
5 Blue
1 Blue 12.99 1 Blue 12.99
2 Red . 2 Red .
3 Red . 3 Red .
4 Green 45.99 4 Green 45.99
5 Blue . 5 Blue .
6 99.99
67
C o p y r i g h t © S AS In s t i tu t e In c. Al l r i g h t s re s e r ve d .

8. Which query joins the tables group1 and group2 and overlays the Tag
columns to produce the result set shown below? group1 group2
Tag FName Tag LName
select g1.Tag
a. from group1 as g1 a Fred b Zhou
group2 as g2 d Sek c Amos
where g1.Tag=g2.Tag
e Lars

b. select coalesce(Tag) as Tag


Results
from group1 as g1 full join group2 as g2
on g1.Tag=g2.Tag;
Tag
a
c. select coalesce(g1.Tag, g2.Tag) as Tag
from group1 as g1 full join group2 as g2 b
on g1.Tag=g2.Tag;
c
d
e
69
C o p y r i g h t © S AS In s t i tu t e In c. Al l r i g h t s re s e r ve d .

Copyright © 2019, SAS Institute Inc., Cary, North Carolina, USA. ALL RIGHTS RESERVED.
1-20

widgets locations
9. The widgets table stores information about the
ID Type ID Bin
most popular items sold in a hardware store.
89 FL 1 B98
The locations table stores information about the
46 UI 46 B63
location of some of the items in the store. Which
47 TA 74 B02
query produces the result set that is shown below?
92 QY 85 B98
a. select coalesce(w.ID, I.ID) as ID, Type, Bin 99 B02
from widgets as w left join locations as I
on w.ID = I.ID Results
order by w.ID;
ID Type Bin

b. select coalesce(w.ID, I.ID) as ID, Type, Bin 46 UI B63


from widgets as w right join locations as I 47 TA
on w.ID = I.ID
order by w.ID; 89 FL
92 QY

71
C o p y r i g h t © S AS In s t i tu t e In c. Al l r i g h t s re s e r ve d .

10. Which syntax is appropriate for a self-join on the employees table?

a. select e1.ID, e1.Name as EmpName, e2.Name as ManagerName


from employees inner join employees
on ID = ID;

b. select e1.ID, e1.Name as EmpName, e2.Name as ManagerName


from employees as e1 and employees as e2
on e1.ID = e2.ID;

c. select e1.ID, e1.Name as EmpName, e2.Name as ManagerName


from employees as e1 inner join employees as e2
on e1.ID = e2.ID;

73
C o p y r i g h t © S AS In s t i tu t e In c. Al l r i g h t s re s e r ve d .

Copyright © 2019, SAS Institute Inc., Cary, North Carolina, USA. ALL RIGHTS RESERVED.
1-21

Lesson 4 – Subqueries

1. Which of the following statements identifies the syntax error in this


query?

select Lastname, Firstname


from charity.donors
where Donation > 1000 and
Lastname not in select Lastname
from charity.current;

a. The subquery requires its own semicolon.


b. Parentheses are required around the subquery.
c. The subquery must reside inside a HAVING clause.
d. The subquery must reference the same table as the outer query.
76
C o p y r i g h t © S AS In s t i tu t e In c. Al l r i g h t s re s e r ve d .

2. Which query finds the ID, Job_Title, and partial staff


Salary values of all employees who make ID Job_Title Salary
more than the average of all employees? 001 Director $163,040

046 Sales Manager $108,255


a. select ID, Job_Title, Salary 074 Office Assistant $36,256
from staff
where Salary > (mean(Salary)); 085 Service Assistant $29,147

b. select ID, Job_Title, Salary


from staff where Salary > (select mean(Salary));

c. select ID, Job_Title, Salary


from staff
where Salary > (select mean(Salary) from staff);
78
C o p y r i g h t © S AS In s t i tu t e In c. Al l r i g h t s re s e r ve d .

Copyright © 2019, SAS Institute Inc., Cary, North Carolina, USA. ALL RIGHTS RESERVED.
1-22

3. Which statement is true regarding a noncorrelated subquery?

a. The subquery is known as a virtual table.


b. The subquery can run as a stand-alone query.
c. The outer query passes a value to the inner query before it can
execute.
d. The subquery must be enclosed in brackets.

80
C o p y r i g h t © S AS In s t i tu t e In c. Al l r i g h t s re s e r ve d .

4. Which query finds the ID, partial staff


Job_Title, and Salary values ID Job_Title Salary Department

of all employees in the 001 Director $163,040 Sales Management


Engineering Department? 074 Office Assistant $36,256 Administration

085 Service Assistant $29,147 Engineering


a.
select ID, Job_Title, Salary
from staff
where ID in (select ID from staff where Department ="Engineering");

b.
select ID, Job_Title, Salary
from staff
where ID = (select ID from staff where Department ="Engineering");
82
C o p y r i g h t © S AS In s t i tu t e In c. Al l r i g h t s re s e r ve d .

Copyright © 2019, SAS Institute Inc., Cary, North Carolina, USA. ALL RIGHTS RESERVED.
1-23

5. An in-line view can return multiple rows and columns.

a. true
b. false

84
C o p y r i g h t © S AS In s t i tu t e In c. Al l r i g h t s re s e r ve d .

6. Suppose you need to use the most up-to-date data from a complex
query and want multiple users to be able access the information. Which
of the following solutions would be best?

a. Create a table with the complex query and allow users access to
the table.
b. Create a view with the complex query and allow users to access the
view.
c. Send users the complex query to copy and paste in their queries as
an in-line view.

86
C o p y r i g h t © S AS In s t i tu t e In c. Al l r i g h t s re s e r ve d .

Copyright © 2019, SAS Institute Inc., Cary, North Carolina, USA. ALL RIGHTS RESERVED.
1-24

7. Which of the following statements creates a new view named


CustomerView from a query?
a. create view CustomerView
select...

b. create view as CustomerView


select...

c. create view CustomerView as


select...

d. create view CustomerView;


select...

88
C o p y r i g h t © S AS In s t i tu t e In c. Al l r i g h t s re s e r ve d .

8. Which method enables a view to be portable—that is, to function


correctly if the underlying tables in the view are stored in different
locations than the view?

a. using a one-level name for the source table in the FROM clause in
the view definition, and storing the view in a different location as
the source table
b. using a two-level name for the source table in the FROM clause in
the view definition, and storing the view in the different location as
the source table
c. embedding the LIBNAME statement with a USING clause when
creating the view
90
C o p y r i g h t © S AS In s t i tu t e In c. Al l r i g h t s re s e r ve d .

Copyright © 2019, SAS Institute Inc., Cary, North Carolina, USA. ALL RIGHTS RESERVED.
1-25

9. Before you run this query, which of the following changes can you make
to prevent SAS from remerging, and still produce a report?

select Order_Type,
min(Quantity) label="Min Qty"
from orion.order_fact;

a. Add an ORDER BY clause to sort the output rows by the values


of Order_Type.
b. Add a GROUP BY clause to group by Order_Type.
c. Add an additional column to the SELECT list.
d. Specify the PROC SQL NOREMERGE option.

92
C o p y r i g h t © S AS In s t i tu t e In c. Al l r i g h t s re s e r ve d .

inventory Results
10. Which SELECT clause calculates the Product Quantity Product Pct
total percentage of inventory for Shirt 5 Shirt 50%
each product? Shoes 3 Shoes 30%
Pants 2 Pants 20%

a. select Product, Quantity, sum(Quantity) as Total format=percent5.2


from inventory;

b. select Product, (select sum(Quantity) from inventory) as Pct format=percent5.2


from inventory;

c. select Product, Quantity/(select sum(Quantity)) as Pct format=percent5.2


from inventory;

d. select Product, Quantity/sum(Quantity) as Pct format=percent5.2


from inventory;
94
C o p y r i g h t © S AS In s t i tu t e In c. Al l r i g h t s re s e r ve d .

Copyright © 2019, SAS Institute Inc., Cary, North Carolina, USA. ALL RIGHTS RESERVED.
1-26

Lesson 5 – Set Operators

1. How many rows does this query produce?

proc sql;
a b
select * ID Pet ID Pet
from a 1 Cat 1 Cat
intersect
select * 1 Dog 2 Cow
from b; 1 Pig 3 Dog
quit;

a. 1
b. 2
c. 5
d. 6
97
C o p y r i g h t © S AS In s t i tu t e In c. Al l r i g h t s re s e r ve d .

2. By default, the EXCEPT, INTERSECT, and UNION set operators remove


duplicate rows from the query results.

a. true
b. false

99
C o p y r i g h t © S AS In s t i tu t e In c. Al l r i g h t s re s e r ve d .

Copyright © 2019, SAS Institute Inc., Cary, North Carolina, USA. ALL RIGHTS RESERVED.
1-27

3. How many rows does this query produce?


a b
proc sql;
select * ID Pet ID Pet
from a 1 Cat 1 Cat
except
select * 1 Dog 2 Cow
from b; 1 Pig 3 Dog
quit;

a. 1
b. 2
c. 5
d. 6
101
C o p y r i g h t © S AS In s t i tu t e In c. Al l r i g h t s re s e r ve d .

4. Adding the CORR and ALL keywords to the EXCEPT operator would
change the number of rows that this query produces.
a b
proc sql;
select * ID Pet ID Pet
from a 1 Cat 1 Cat
except
select * 1 Dog 2 Cow
from b; 1 Pig 3 Dog
quit;

a. true
b. false

103
C o p y r i g h t © S AS In s t i tu t e In c. Al l r i g h t s re s e r ve d .

Copyright © 2019, SAS Institute Inc., Cary, North Carolina, USA. ALL RIGHTS RESERVED.
1-28

5. How many rows does this query produce?


a b
proc sql;
select * ID Pet ID Pet
from a 1 Cat 1 Cat
outer union
select * 1 Dog 2 Cow
from b; 1 Pig 3 Dog
quit;

a. 1
b. 2
c. 5
d. 6
105
C o p y r i g h t © S AS In s t i tu t e In c. Al l r i g h t s re s e r ve d .

6. How many rows does this query produce?


a b
proc sql;
select * ID Pet ID Pet
from a 1 Cat 1 Cat
union
select * 1 Dog 2 Cow
from b; 1 Pig 3 Dog
quit;

a. 2
b. 3
c. 4
d. 5
107
C o p y r i g h t © S AS In s t i tu t e In c. Al l r i g h t s re s e r ve d .

Copyright © 2019, SAS Institute Inc., Cary, North Carolina, USA. ALL RIGHTS RESERVED.
1-29

7. How many rows does this query produce?


a b
proc sql;
select * ID Pet ID Pet
from a 1 Cat 1 Cat
union all
select * 1 Dog 2 Cow
from b; 1 Pig 3 Dog
quit;

a. 2
b. 4
c. 6
d. 8
109
C o p y r i g h t © S AS In s t i tu t e In c. Al l r i g h t s re s e r ve d .

8. Which statement is true about using the keyword CORR in a set


operation?

a. When used with the EXCEPT operator, the CORR keyword instructs
PROC SQL to position by same-name columns and eliminate
columns that do not have the same name in both tables from the
final results.
b. When used with the INTERSECT operator, the CORR keyword
instructs PROC SQL to overlay columns by position in the final
results.
c. When used with the OUTER UNION operator, the CORR keyword
instructs PROC SQL to eliminate columns that do not have the same
name in both tables from the final results.
111
C o p y r i g h t © S AS In s t i tu t e In c. Al l r i g h t s re s e r ve d .

Copyright © 2019, SAS Institute Inc., Cary, North Carolina, USA. ALL RIGHTS RESERVED.
1-30

9. Which statement is true about using the keyword ALL in a set


operation?

a. Generally, using the keyword ALL is less efficient because it requires


an extra pass through the data.
b. Generally, using the keyword ALL is more efficient because it avoids
an extra pass through the data.
c. PROC SQL does not allow duplicate rows to remain eligible for
processing.
d. The keyword ALL is not an implied part of the OUTER UNION set
operator and is not necessary.

113
C o p y r i g h t © S AS In s t i tu t e In c. Al l r i g h t s re s e r ve d .

10. PROC SQL with set operators handles columns and rows, depending on
the specific set operator and keywords used in the set operation.

a. true
b. false

115
C o p y r i g h t © S AS In s t i tu t e In c. Al l r i g h t s re s e r ve d .

Copyright © 2019, SAS Institute Inc., Cary, North Carolina, USA. ALL RIGHTS RESERVED.
1-31

Lesson 6 – Using and Creating Macro Variables in SQL

1. Which of the following can be stored in a macro variable?

a. 9.2
b. 10:32
c. 1352050
d. Soccerballs
e. all of the above

118
C o p y r i g h t © S AS In s t i tu t e In c. Al l r i g h t s re s e r ve d .

2. Which of the following lines of code correctly references a macro


variable?

a. title "The Sales of the &Country Offices";

b. where salary > 'avgSal';

c. title 'The Sales of the &Country Offices';

120
C o p y r i g h t © S AS In s t i tu t e In c. Al l r i g h t s re s e r ve d .

Copyright © 2019, SAS Institute Inc., Cary, North Carolina, USA. ALL RIGHTS RESERVED.
1-32

3. Which of the following options enables you to view the values of the
macro variables without using them in a report?

a. the SYMBOLLOG system option


b. the SYMBOLGEN system option
c. the NOFEEDBACK PROC SQL option
d. the LABEL= option

122
C o p y r i g h t © S AS In s t i tu t e In c. Al l r i g h t s re s e r ve d .

4. Which statement correctly creates the user-defined path macro


variable with the value of s:/workshop/data?

a. %user path=s:/workshop/data;

b. %macro path=s:/workshop/data;

c. %path=s:/workshop/data;

d. %let path=s:/workshop/data;

124
C o p y r i g h t © S AS In s t i tu t e In c. Al l r i g h t s re s e r ve d .

Copyright © 2019, SAS Institute Inc., Cary, North Carolina, USA. ALL RIGHTS RESERVED.
1-33

5. Which of these queries places one value from the first row returned by
the SQL query into a macro variable?

a. select avg(Salary)
into :MeanSalary
from store.employee;

b. select avg(Salary)
into %MeanSalary
from store.employee;

c. select avg(Salary)
into &MeanSalary
from store.employee;
126
C o p y r i g h t © S AS In s t i tu t e In c. Al l r i g h t s re s e r ve d .

6. You created a macro variable named MeanSalary and want to display


the macro variable value in the log. Which statement produces the
value in the log?

a. put &=MeanSalary;

b. put MeanSalary;

c. %put &=MeanSalary;

d. %put MeanSalary;

128
C o p y r i g h t © S AS In s t i tu t e In c. Al l r i g h t s re s e r ve d .

Copyright © 2019, SAS Institute Inc., Cary, North Carolina, USA. ALL RIGHTS RESERVED.
1-34

7. You want to store the descriptive statistics of the Price column from the
inventory table into multiple macro variables. Which query successfully
stores mean, median, min, and max?
a. select mean(Price), median(Price), min(msrp), max(Price)
into: MeanPrice, MedianPrice, MinPrice, MaxPrice
from inventory;

b. select mean(Price), median(Price), min(msrp), max(Price)


into MeanPrice, MedianPrice, MinPrice, MaxPrice separated by ","
from inventory;

c. select mean(Price), median(Price), min(msrp), max(Price)


into :MeanPrice, :MedianPrice, :MinPrice, :MaxPrice
from inventory;

130
C o p y r i g h t © S AS In s t i tu t e In c. Al l r i g h t s re s e r ve d .

8. Which SELECT clause stores a list of products in a macro variable named


InventoryList from the inventory table? Each product should be
delimited by a comma.
inventory
a. select Product Product Quantity
into :InventoryList ","
from inventory; Shirt 5
Shoes 3
b. select Product Pants 2
into :InventoryList delimited by ","
from inventory;

c. select Product
into :InventoryList separated by ","
from inventory;

132
C o p y r i g h t © S AS In s t i tu t e In c. Al l r i g h t s re s e r ve d .

Copyright © 2019, SAS Institute Inc., Cary, North Carolina, USA. ALL RIGHTS RESERVED.
1-35

9. Given the table revenue below, you want to store the sum of Revenue
in a macro variable named TotalRevenue. Does the following query
store the exact value of 900000000 in TotalRevenue?
revenue
proc sql; Product Revenue
select sum(Revenue) Shirt $200,000,000
into :TotalRevenue
from revenue; Shoes $300,000,000
quit; Pants $400,000,000

a. yes
b. no

134
C o p y r i g h t © S AS In s t i tu t e In c. Al l r i g h t s re s e r ve d .

10. Given the table revenue below, you want to store the sum of Revenue
in a macro variable named TotalRevenue. Which of the following
queries stores the exact value of $900,000,000 in the macro variable
TotalRevenue?
revenue
a. select sum(Revenue) format=dollar16. Product Revenue
into :TotalRevenue
from revenue; Shirt $200,000,000
Shoes $300,000,000
b. select sum(Revenue) Pants $400,000,000
into :TotalRevenue format=dollar16.
from revenue;

c. select sum(Revenue)
into :TotalRevenue
from revenue; 136
C o p y r i g h t © S AS In s t i tu t e In c. Al l r i g h t s re s e r ve d .

Copyright © 2019, SAS Institute Inc., Cary, North Carolina, USA. ALL RIGHTS RESERVED.
1-36

Lesson 7 – Accessing DBMS Data with SAS/ACCESS

1. SAS/ACCESS technology enables you to read data that is in a third-party


DBMS from within SAS.

a. true
b. false

139
C o p y r i g h t © S AS In s t i tu t e In c. Al l r i g h t s re s e r ve d .

2. You're familiar with the Oracle SQL implementation. You begin to use
PROC SQL but don't want to learn any of the SAS enhancements and
functions. You would like to continue to use the Oracle SQL
implementation inside SAS. Which method would you use to interact
with the Oracle database?

a. SQL pass-through (explicit pass through)


b. LIBNAME statement (implicit pass through)

141
C o p y r i g h t © S AS In s t i tu t e In c. Al l r i g h t s re s e r ve d .

Copyright © 2019, SAS Institute Inc., Cary, North Carolina, USA. ALL RIGHTS RESERVED.
1-37

3. You're familiar with using PROC SQL and are connecting to an SQL
Server database. You are not familiar with the database implementation
of SQL and want to continue using PROC SQL. Which method would you
use to interact with the SQL Server database?

a. SQL Pass-Through (Explicit Pass Through)


b. LIBNAME Statement (Implicit Pass Through)

143
C o p y r i g h t © S AS In s t i tu t e In c. Al l r i g h t s re s e r ve d .

4. Which query executes correctly in PROC FEDSQL?

a. proc fedsql;
select EmployeeID, Name, Salary, Department
from orion.employee
where = "Management";
quit;

b. proc fedsql;
select EmployeeID, Name, Salary, Department
from orion.employee
where = 'Management';
quit;

145
C o p y r i g h t © S AS In s t i tu t e In c. Al l r i g h t s re s e r ve d .

Copyright © 2019, SAS Institute Inc., Cary, North Carolina, USA. ALL RIGHTS RESERVED.
1-38

5. You are working with an Oracle database and want to push as much of
the processing as possible into the database. Which PROC would be
better to use?

a. PROC SQL
b. PROC FEDSQL

147
C o p y r i g h t © S AS In s t i tu t e In c. Al l r i g h t s re s e r ve d .

Copyright © 2019, SAS Institute Inc., Cary, North Carolina, USA. ALL RIGHTS RESERVED.
1-39

1.2 Solutions
Lesson 1

1. You want to explore the first 10 rows of the sq.profit table. Which PROC
SQL option limits the rows from the sq.profit table that contribute to
the query?

a. OUTOBS=10
b. OBS=10
c. ROW=10
d. INOBS=10

4
C o p y r i g h t © S AS In s t i tu t e In c. Al l r i g h t s re s e r ve d .

The INOBS= option restricts the number of rows that PROC SQL reads from all tables specified in
the FROM clause. The OUTOBS= option restricts the number of rows that a query outputs. The
OBS= option is a SAS data set option that is used in the FROM clause to l imit the number of rows
from a table.

Copyright © 2019, SAS Institute Inc., Cary, North Carolina, USA. ALL RIGHTS RESERVED.
1-40

2. When using only the SQL procedure, you can create which of the
following objects?

a. SAS tables
b. SAS views
c. DBMS tables
d. all of the above

6
C o p y r i g h t © S AS In s t i tu t e In c. Al l r i g h t s re s e r ve d .

As output, a PROC SQL query creates a report by default. However, a PROC SQL query can also
create SAS tables, SAS or DBMS views, and DBMS tables.

3. You want to display the column name, column types, and other column
information from the sq.profit table. Which statement should you use?

a. content sq.profit;
b. contents table sq.profit;
c. describe sq.profit;
d. describe table sq.profit;

8
C o p y r i g h t © S AS In s t i tu t e In c. Al l r i g h t s re s e r ve d .

You specify the DESCRIBE TABLE statement and then the name of the t able to describe.
DESCRIBE TABLE generates a description of the table in the log. Results display the column
names, column types, and associated labels or formats, if they exist, for specific columns.

Copyright © 2019, SAS Institute Inc., Cary, North Carolina, USA. ALL RIGHTS RESERVED.
1-41

4. Which PROC SQL option would you use to produce a report that includes
the row number in the report?

a. NUMBER
b. OBS
c. ROWNUM
d. ROW

10
C o p y r i g h t © S AS In s t i tu t e In c. Al l r i g h t s re s e r ve d .

The NUMBER option specifies whether the SELECT statement includes the row number of the data
as the rows are retrieved.

5. Which of the following SELECT statements would you use to produce


a report that includes every column from the sashelp.cars table?

a. SELECT ALL
b. SELECT _COLUMNS_
c. SELECT *
d. SELECT _ALL_

12
C o p y r i g h t © S AS In s t i tu t e In c. Al l r i g h t s re s e r ve d .

The asterisk (*) represents all columns of the table or tables listed in the FROM clause.

Copyright © 2019, SAS Institute Inc., Cary, North Carolina, USA. ALL RIGHTS RESERVED.
1-42

6. You have been programming in a specific database implementation of


SQL and want to begin using PROC SQL. All of your previous knowledge
of keywords, features, and enhancements will transfer over when using
PROC SQL.

a. true
b. false

14
C o p y r i g h t © S AS In s t i tu t e In c. Al l r i g h t s re s e r ve d .

Although SQL has a set of standards, it is important to understand that database management
systems offer different implementations of the standard SQL. Database management systems like
Oracle, Teradata, SQL Server, Postgres, and a variety of others all follow ANSI standard SQL.
However, some might have keywords, features, and enhancements that are slightly different
between systems.

7. You want to produce a report that selects the Make, Model, and
MPG_City columns from the sashelp.cars table. Which of the following
queries is correct?
a. select Make, Model, MPG_City
from sashelp.cars;
b.
select Make Model MPG_City
from sashelp.cars;
c.
from sashelp.cars
select Make Model MPG_City;
d. from sashelp.cars
select Make, Model, MPG_City;

16
C o p y r i g h t © S AS In s t i tu t e In c. Al l r i g h t s re s e r ve d .

You specify the names of the columns, separated by commas, in the SELECT clause, and specify
the input table in the FROM clause.

Copyright © 2019, SAS Institute Inc., Cary, North Carolina, USA. ALL RIGHTS RESERVED.
1-43

8. In orion.sales, the columns appear in this order: Employee_ID,


First_Name, Last_Name, Gender, Salary, Job_Title, Country,
Birth_Date, Hire_Date. When you run the following query, in what order
do columns appear in the report?
proc sql;
select Last_Name, First_Name
from orion.sales;
quit;

a. First_Name, Last_Name
b. Last_Name, First_Name
c. Last_Name, First_Name, Job_Title

18
C o p y r i g h t © S AS In s t i tu t e In c. Al l r i g h t s re s e r ve d .

In a query’s output, the columns appear in the order in which they are listed in the SELECT clause.
Columns that are listed in other clauses, but not the SELECT clause, do not appear in output.

9. In your PROC SQL code, you can include SAS features that do not
conform to the ANSI standard.

a. true
b. false

20
C o p y r i g h t © S AS In s t i tu t e In c. Al l r i g h t s re s e r ve d .

In PROC SQL, you can include SAS enhancements that go beyond the ANSI standard.

Copyright © 2019, SAS Institute Inc., Cary, North Carolina, USA. ALL RIGHTS RESERVED.
1-44

10. If present, the clauses in a SELECT statement must be in which order?

a. SELECT, FROM, WHERE, GROUP BY, ORDER BY, HAVING


b. SELECT, FROM, WHERE, GROUP BY, HAVING, ORDER BY

22
C o p y r i g h t © S AS In s t i tu t e In c. Al l r i g h t s re s e r ve d .

If present, the clauses must be in this order: SELECT, FROM, WHERE, GROUP BY, HAVING, and
ORDER BY, but not all clauses must be present. For example, you can use the WHERE clause and
ORDER BY clause and not use the GROUP BY or HAVING clause.

Copyright © 2019, SAS Institute Inc., Cary, North Carolina, USA. ALL RIGHTS RESERVED.
1-45

Lesson 2

1. Which SELECT statement creates a report that displays only the unique
combinations of Customer_Type_ID and Country?
a. select distinct Customer_Type_ID Country
from orion.customer;

b. select distinct Customer_Type_ID, Country


from orion.customer;

c. select distinct Customer_Type_ID, distinct Country


from orion.customer;

d. select Customer_Type_ID, Country


from orion.customer
where distinct Customer_Type_ID and Country;

25
C o p y r i g h t © S AS In s t i tu t e In c. Al l r i g h t s re s e r ve d .

To select unique combinations of the values of multiple columns, you place the DISTINCT keyword
once in the SELECT clause before the entire column list. Columns must be separated by commas.

2. Which WHERE clause subsets rows based on NewSalary, an alias


created in the SELECT clause?
a. where 75000<=Salary calculated <=100000

b. where NewSalary>=75000 and NewSalary<=100000

c. where 75000<= calculated Salary<=100000

d. where 75000<=calculated NewSalary<=100000

27
C o p y r i g h t © S AS In s t i tu t e In c. Al l r i g h t s re s e r ve d .

The SAS keyword CALCULATED enables you to use the results of an expression in the same
SELECT clause or in the WHERE clause.

Copyright © 2019, SAS Institute Inc., Cary, North Carolina, USA. ALL RIGHTS RESERVED.
1-46

3. Which of the following options uses an ANSI standard column modifier


to create a label that is displayed as the heading for a column in the
output?
a. 'Employee Number'

b. Employee Number

c. Label='Employee ID'

d. Header=Employee Reference Number

29
C o p y r i g h t © S AS In s t i tu t e In c. Al l r i g h t s re s e r ve d .

The LABEL= column modifier is not ANSI standard. It is a SAS enhancement to specify labels. You
can use the ANSI column modifier, the LABEL= column modifier, or both within the same SELECT
clause.

4. Which of the following expressions creates a new column in the SELECT


clause?
a. Salary *.01 as Increase

b. Bonus=Salary*.03

c. Job_Level as Scan (Job_Title,1,' ')

d. Salary*.03 Bonus

31
C o p y r i g h t © S AS In s t i tu t e In c. Al l r i g h t s re s e r ve d .

An equal sign is not allowed in the expression in the SELECT clause when creating a new column.
To assign a name that is referred to as an alias in PROC SQL, use the keyword AS followed by a
valid SAS name, not the expression.

Copyright © 2019, SAS Institute Inc., Cary, North Carolina, USA. ALL RIGHTS RESERVED.
1-47

5. To guarantee the order of the rows in the output, you must use an
ORDER BY clause.

a. true
b. false

33
C o p y r i g h t © S AS In s t i tu t e In c. Al l r i g h t s re s e r ve d .

To guarantee the order of the rows in the output, you must use an ORDER BY clause.

6. If the values returned by the YEAR function are . (missing), 2018, 2019,
and 2020, which value for year is displayed first in the output based on
this code?
proc sql;
select Employee_ID, Employee_Term_Date format=date9.,
year(Employee_Term_Date) as year
from orion.employee_payroll
where Employee_Gender='M' and Marital_Status='S'
order by year;
quit;

a. . (missing)
b. 2018
c. 2019
d. 2020 35
C o p y r i g h t © S AS In s t i tu t e In c. Al l r i g h t s re s e r ve d .

The default of the ORDER BY clause specifies to display values from the smallest to the largest
numeric or character value. PROC SQL treats null or missing values as the smallest possible value.

Copyright © 2019, SAS Institute Inc., Cary, North Carolina, USA. ALL RIGHTS RESERVED.
1-48

7. Which of the following programs positions the TITLE statement correctly


so that the title appears in the output?
a. proc sql;
select Employee_ID from orion.order_fact;
quit;
title 'Manager Report';
b. proc sql;
title 'Manager Report';
select Employee_ID from orion.order_fact;
quit;
c. proc sql;
select Employee_ID from orion.order_fact;
title 'Manager Report';
quit;

37
C o p y r i g h t © S AS In s t i tu t e In c. Al l r i g h t s re s e r ve d .

You must place TITLE and FOOTNOTE statements before the SELECT clause, because in the SQL
procedure the SELECT clause runs immediately. Alternatively, you can place the statements before
the PROC SQL statement.

8. Which of the following CASE expressions is correct?


a. case Age
when 20 <= Age <= 29 then '20-29';

b. case
when 20 <= Age <= 29 then '20-29';

39
C o p y r i g h t © S AS In s t i tu t e In c. Al l r i g h t s re s e r ve d .

When you need to use a comparison operator other than equal, you use a simple case expression.

Copyright © 2019, SAS Institute Inc., Cary, North Carolina, USA. ALL RIGHTS RESERVED.
1-49

9. In this PROC SQL program, the two SELECT statements use the
orion.employee_donations table and generate the same results.

proc sql; employee_donations


select count(*) Employee_ID Qtr1 Qtr2 Qtr3 Qtr4
from orion.employee_donations;
120265 . . . 25
select count(Qtr1)
from orion.employee_donations; 120267 15 15 15 15
quit;
120269 20 20 20 20
a. true 120270 20 10 5 .
b. false

41
C o p y r i g h t © S AS In s t i tu t e In c. Al l r i g h t s re s e r ve d .

COUNT(*) counts the total number of rows in a table or in a subset. However, when you specify a
column as an argument for COUNT, the function counts the number of rows in the table or in a
subset that have a nonmissing value for that column.

10. The product_dim table contains 481 rows—that is, 1 row for each
product that Orion Star sells. All Orion Star products are assigned to
1 of 50 product groups. Which SELECT statement creates a report
that shows the number of products in each product group?
a. select Product_Group, count(*) as Products
from orion.product_dim;

b. select Product_Group, count(*) as Products


from orion.product_dim
group by Product_Group;

c. select Product_Group, count(*) as Products


from orion.product_dim
group by Product_Group, Products;
43
C o p y r i g h t © S AS In s t i tu t e In c. Al l r i g h t s re s e r ve d .

To aggregate summary statistics by the values of the non-summarized column, Product_Group, the
query must use a GROUP BY clause that specifies the Product_Group column. This report is
grouped by only one column. Also, the GROUP BY clause cannot specify a summarized column, like
Products.

Copyright © 2019, SAS Institute Inc., Cary, North Carolina, USA. ALL RIGHTS RESERVED.
1-50

11. To produce a report that includes all Toyotas with a Price value less than
$30,000, which WHERE clause would you use?
Make Model Price MPG
Toyota RAV4 $28,750 23
Honda Accord $19,350 32
Honda Pilot $32,500 22
BMW 135i $25,200 24

a. where Make = "Toyota" and Price < $30,000

b. where Make = "Toyota" or Price < 30000

c. where Make = "Toyota" and Price < 30000


d. where Make = "Toyota" or
45
Price < $30,000
C o p y r i g h t © S AS In s t i tu t e In c. Al l r i g h t s re s e r ve d .

To select all rows where Make equals Toyota and Price is less than 30000, you must use the AND
operator. Toyota must be in quotation marks and is case sensitive, and the numeric Price value must
be in standard numeric form.

12. In which of the following queries can you replace the SAS summary
function MEAN with the ANSI summary function AVG and get the
same results?
a. select mean(Qtr1, Qtr2, Qtr3, Qtr4) as Mean_Annual
from orion.employee_donations;

b. select Employee_ID, mean(Qtr1, Qtr2, Qtr3, Qtr4) as Mean_Annual


from orion.employee_donations;

c. select mean(of Qtr1:) as Mean_Qtr_1


from orion.employee_donations;

d. select mean(Qtr1) as Mean_Qtr_1


from orion.employee_donations;
47
C o p y r i g h t © S AS In s t i tu t e In c. Al l r i g h t s re s e r ve d .

Unlike most SAS summary functions, ANSI summary functions can take only a single argument.
Also, because PROC SQL is ANSI standard, shortcuts to reference a range of variables in an
argument list within a function are not acceptable in PROC SQL as they are in other SAS
procedures.

Copyright © 2019, SAS Institute Inc., Cary, North Carolina, USA. ALL RIGHTS RESERVED.
1-51

13. You want to count the number of employees in each department and
return only the departments with more than 30 employees. Which
query produces the correct report?
a. select Department, count(*) as TotalEmployees
from orion.employees
where calculated TotalEmployees > 30
group by Department;

select Department, count(*) as TotalEmployees


b. from orion.employees
having TotalEmployees > 30
group by Department;

select Department, count(*) as TotalEmployees


from orion.employees
c. group by Department
having TotalEmployees > 30;
49
C o p y r i g h t © S AS In s t i tu t e In c. Al l r i g h t s re s e r ve d .

The WHERE clause is evaluated before a row is available for processing and determines which
individual rows are available for grouping. You cannot use a WHERE clause to subset grouped rows
by referring to the calculated summary column.
You must use the HAVING clause with a GROUP BY clause to filter summarized rows. The HAVING
clause affects groups in a way that is similar to how a WHERE clause affects indiv idual rows. When
you use a HAVING clause, PROC SQL displays only the groups that satisfy the HAVING expression.
PROC SQL applies the HAVING condition after grouping the data and applying aggregate functions.
Think of the HAVING clause as post-summarization filtering.

Copyright © 2019, SAS Institute Inc., Cary, North Carolina, USA. ALL RIGHTS RESERVED.
1-52

14. Which of the following statements creates a new table named results
from a query?
a. create table results like
select ...

b. create table results as


select ...

c. create table results


(select ...)

d. create table results from


select ...

51
C o p y r i g h t © S AS In s t i tu t e In c. Al l r i g h t s re s e r ve d .

To create a table from a query result, you use a CREATE TABLE statement with the new table name
and the AS keyword followed by the query. Adding CREATE TABLE at the beginning of the query
creates a table instead of results from the query.

15. Which of the following queries produces a report of all columns from
every table in the Sashelp library?
a. select *
from dictionaries.columns
where libname="SASHELP";

b. select *
from sashelp.vcolumn
where libname="SASHELP";

c. select *
from dictionary.columns
where libname="SASHELP";

53
C o p y r i g h t © S AS In s t i tu t e In c. Al l r i g h t s re s e r ve d .

The columns DICTIONARY table shows all columns in every table. The WHERE clause limits the
report to all tables in the Sashelp library. All library values are stored in uppercase. The
SASHELP.VCOLUMN table produces the same report when using the PRINT procedure.
SASHELP.VCOLUMN does not work in PROC SQL.

Copyright © 2019, SAS Institute Inc., Cary, North Carolina, USA. ALL RIGHTS RESERVED.
1-53

Lesson 3

1. How many rows does the following query produce in the report?

select *
from sq.employee_donations,
sq.employees;

a. 4 employee_donations employees
b. 5 Employee_ID Qtr1 Qtr2 Qtr3 Qtr4 Employee_ID Name
c. 9 120265 . . . 25 120265 Panagiotis
d. 20 120267 15 15 15 15 120267 Emma
120269 20 20 20 20 120269 Fatima
120270 20 10 5 . 120270 Sofia
120272 Owen
56
C o p y r i g h t © S AS In s t i tu t e In c. Al l r i g h t s re s e r ve d .

The default join combines every row in each table. This is called the Cartesian product. Typically, the
Cartesian product is not the desired result. In this example, all 4 rows from the
employee_donations table are joined with every row in the employees table. The new report
contains 20 rows.

Copyright © 2019, SAS Institute Inc., Cary, North Carolina, USA. ALL RIGHTS RESERVED.
1-54

2. Which code produces a report of only the matches from the


employee_donations and employees tables?
The results should include each column once. employee_donations
a. select Employee_ID, Qtr1, Qtr2, Qtr2, Qtr3, Employee_ID Qtr1 Qtr2 Qtr3 Qtr4
Name
from employee_donations as d inner join 120265 . . . 25
employees as e 120267 15 15 15 15
on Employee_ID=Employee_ID;
120269 20 20 20 20
b. select e.Employee_ID, Qtr1, Qtr2, Qtr2, 120270 20 10 5 .
Qtr3, Name
from employee_donations as d inner join
employees as e
employees
on d.Employee_ID=e.Employee_ID; Employee_ID Name

c. select *
120265 Panagiotis
from employee_donations as d inner join 120267 Emma
employees as e
on d.Employee_ID=e.Employee_ID; 120269 Fatima
120270 Sofia
58 120272 Owen
C o p y r i g h t © S AS In s t i tu t e In c. Al l r i g h t s re s e r ve d .

In the FROM clause, you specify the first table and the keywords INNER JOIN, followed by the
second table. Following the table names and join type, the syntax requires an ON clause to describe
the join criteria for matching rows in the tables. When you refer to tables that have a same-named
column, you must qualify the column names to specify the location of each column.

3. Which program correctly displays a list of qualified column names


in the SAS log?
a. proc sql;
describe table acme.staff;
quit;
b. proc sql noexec;
select * from acme.staff;
quit;
c. proc sql list;
select * from acme.staff;
quit;
d. proc sql feedback;
select * from acme.staff;
quit; 60
C o p y r i g h t © S AS In s t i tu t e In c. Al l r i g h t s re s e r ve d .

To display an expanded query, which contains a list of qualified column names in the SAS log, you
add the FEEDBACK option to the PROC SQL statement above the SELECT statement.

Copyright © 2019, SAS Institute Inc., Cary, North Carolina, USA. ALL RIGHTS RESERVED.
1-55

4. Which result set does the following inner join produce?


products saleitems
select p.Prod_ID, Color, Price
from products as p inner join Prod_ID Color Prod_ID Price
saleitems as s 1 Blue 1 12.99
on p.Prod_ID=s.Prod_ID
2 Red 4 45.99
and Color='Blue';
3 Red
4 Yellow
5 Blue
6 Yellow

a. Result Set 1 b. Result Set 2 c. Result Set 3


Prod_ID Color Prod_ID Price Prod_ID Color Price Prod_ID Color Price
1 Blue 1 12.99 1 Blue 12.99 1 Blue 12.99
5 Blue 5 . 5 Blue .
62
C o p y r i g h t © S AS In s t i tu t e In c. Al l r i g h t s re s e r ve d .

The SELECT clause qualifies the Prod_ID column name, so the result set has only one Prod_ID
column. This is an inner join, so the ON clause joins only the rows that have the matching values of
Prod_ID and the Color Blue.

5. Which result set does the following inner join produce?


products saleitems
select p.Prod_ID, Color, Price
from products as p inner join Prod_ID Color Prod_ID Price
saleitems as s 1 Blue 1 12.99
on p.Prod_ID=s.Prod_ID;
2 Red 4 45.99
3 Red . .99
. Green . 1.99
a. Result Set 1
. Blue
Prod_ID Color Price
1 Blue 12.99 b. Result Set 2 c. Result Set 3
. Green .99 Prod_ID Color Price Prod_ID Color Price
. Green 1.99 1 Blue 12.99 1 Blue 12.99
. Blue .99 . Green .99
. Blue 1.99 . Blue 1.99
64
C o p y r i g h t © S AS In s t i tu t e In c. Al l r i g h t s re s e r ve d .

Most database products treat missing values as the absence of a value (nulls), and because they
don't contain any value, they're excluded from any conditional evaluation. PROC SQL treats missing
values as valid values and matches for joins. Any missing value matches with any other missing
value of the same type (character or numeric) in a join. This could return unexpected results.

Copyright © 2019, SAS Institute Inc., Cary, North Carolina, USA. ALL RIGHTS RESERVED.
1-56

6. Using the products and producttiers tables, complete the following


program to produce the desired results of a product and its tier.
select p.Prod_ID, Price, Tiers products producttiers
from products as p inner join Prod_ID Price Tiers Low High
productTiers as t 1 125.99 Tier 1 0 9.99
on _________________
order by p.Prod_ID; 2 90.99 Tier 2 10 99.99
3 1.99 Tier 3 100 9999.99
a. 4 11.99
p.Price <= t.High Desired Results
5 1099.99 Prod_ID Price Tiers
b. p.Price >= t.Low 6 10.99 1 125.99 Tier 3
2 90.99 Tier 2
c. p.Price >= t.Low or p.Price <= t.High 3 1.99 Tier 1
4 11.99 Tier 2
d. p.Price >= t.Low and p.Price <= t.High 5 1099.99 Tier 3
66 6 10.99 Tier 2
C o p y r i g h t © S AS In s t i tu t e In c. Al l r i g h t s re s e r ve d .

In the ON clause, you can use comparison operators instead of equality for the JOIN criteria. The
p.Price column must be greater than or equal to the t.Low value, and less than or equal to the
t.High value for the row to join.

7. Which result set does the following left join produce?


products saleitems
select p.Prod_ID, Color, Price
from products as p left join Prod_ID Color Prod_ID Price
saleitems as s 1 Blue 1 12.99
on p.Prod_ID=s.Prod_ID;
2 Red 4 45.99
3 Red 6 99.99
a. Result Set 1 b. Result Set 2
4 Green
Prod_ID Color Price Prod_ID Color Price
5 Blue
1 Blue 12.99 1 Blue 12.99
2 Red . 2 Red .
3 Red . 3 Red .
4 Green 45.99 4 Green 45.99
5 Blue . 5 Blue .
6 99.99
68
C o p y r i g h t © S AS In s t i tu t e In c. Al l r i g h t s re s e r ve d .

A left join lists every item in the left table (products) with or without a match in the right table
(saleitems).

Copyright © 2019, SAS Institute Inc., Cary, North Carolina, USA. ALL RIGHTS RESERVED.
1-57

8. Which query joins the tables group1 and group2 and overlays the Tag
columns to produce the result set shown below? group1 group2
Tag FName Tag LName
select g1.Tag
a. from group1 as g1 a Fred b Zhou
group2 as g2 d Sek c Amos
where g1.Tag=g2.Tag
e Lars

b. select coalesce(Tag) as Tag


Results
from group1 as g1 full join group2 as g2
on g1.Tag=g2.Tag;
Tag
a
c. select coalesce(g1.Tag, g2.Tag) as Tag
from group1 as g1 full join group2 as g2 b
on g1.Tag=g2.Tag;
c
d
e
70
C o p y r i g h t © S AS In s t i tu t e In c. Al l r i g h t s re s e r ve d .

The full join with the COALESCE function creates this result set. To overlay two columns, you must
specify the two columns as arguments in the COALESCE function.

widgets locations
9. The widgets table stores information about the
ID Type ID Bin
most popular items sold in a hardware store.
89 FL 1 B98
The locations table stores information about the
46 UI 46 B63
location of some of the items in the store. Which
47 TA 74 B02
query produces the result set that is shown below?
92 QY 85 B98
a. select coalesce(w.ID, I.ID) as ID, Type, Bin 99 B02
from widgets as w left join locations as I
on w.ID = I.ID Results
order by w.ID;
ID Type Bin

b. select coalesce(w.ID, I.ID) as ID, Type, Bin 46 UI B63


from widgets as w right join locations as I 47 TA
on w.ID = I.ID
order by w.ID; 89 FL
92 QY

72
C o p y r i g h t © S AS In s t i tu t e In c. Al l r i g h t s re s e r ve d .

This result set contains all the rows from the widgets table and only the matching row from the
locations table. A left join that specifies the widgets table as the left, or first, table can create these
results. A right join can create this result set only if the widgets table is specified as the right, or
second, table.

Copyright © 2019, SAS Institute Inc., Cary, North Carolina, USA. ALL RIGHTS RESERVED.
1-58

10. Which syntax is appropriate for a self-join on the employees table?

a. select e1.ID, e1.Name as EmpName, e2.Name as ManagerName


from employees inner join employees
on ID = ID;

b. select e1.ID, e1.Name as EmpName, e2.Name as ManagerName


from employees as e1 and employees as e2
on e1.ID = e2.ID;

c. select e1.ID, e1.Name as EmpName, e2.Name as ManagerName


from employees as e1 inner join employees as e2
on e1.ID = e2.ID;

74
C o p y r i g h t © S AS In s t i tu t e In c. Al l r i g h t s re s e r ve d .

To write a self-join, select from the same table listed twice with different aliases . Set up the join type
and then the matching criteria referencing the table alias.

Copyright © 2019, SAS Institute Inc., Cary, North Carolina, USA. ALL RIGHTS RESERVED.
1-59

Lesson 4

1. Which of the following statements identifies the syntax error in this


query?

select Lastname, Firstname


from charity.donors
where Donation > 1000 and
Lastname not in select Lastname
from charity.current;

a. The subquery requires its own semicolon.


b. Parentheses are required around the subquery.
c. The subquery must reside inside a HAVING clause.
d. The subquery must reference the same table as the outer query.
77
C o p y r i g h t © S AS In s t i tu t e In c. Al l r i g h t s re s e r ve d .

A subquery has to be enclosed in parentheses.

2. Which query finds the ID, Job_Title, and partial staff


Salary values of all employees who make ID Job_Title Salary
more than the average of all employees? 001 Director $163,040

046 Sales Manager $108,255


a. select ID, Job_Title, Salary 074 Office Assistant $36,256
from staff
where Salary > (mean(Salary)); 085 Service Assistant $29,147

b. select ID, Job_Title, Salary


from staff where Salary > (select mean(Salary));

c. select ID, Job_Title, Salary


from staff
where Salary > (select mean(Salary) from staff);
79
C o p y r i g h t © S AS In s t i tu t e In c. Al l r i g h t s re s e r ve d .

A noncorrelated subquery must be a valid query inside parentheses in the WHERE or HAVING
clause, and return a single column with one or more rows.

Copyright © 2019, SAS Institute Inc., Cary, North Carolina, USA. ALL RIGHTS RESERVED.
1-60

3. Which statement is true regarding a noncorrelated subquery?

a. The subquery is known as a virtual table.


b. The subquery can run as a stand-alone query.
c. The outer query passes a value to the inner query before it can
execute.
d. The subquery must be enclosed in brackets.

81
C o p y r i g h t © S AS In s t i tu t e In c. Al l r i g h t s re s e r ve d .

A noncorrelated subquery can be extracted and run independently of the outer query.

4. Which query finds the ID, partial staff


Job_Title, and Salary values ID Job_Title Salary Department

of all employees in the 001 Director $163,040 Sales Management


Engineering Department? 074 Office Assistant $36,256 Administration

085 Service Assistant $29,147 Engineering


a.
select ID, Job_Title, Salary
from staff
where ID in (select ID from staff where Department ="Engineering");

b.
select ID, Job_Title, Salary
from staff
where ID = (select ID from staff where Department ="Engineering");
83
C o p y r i g h t © S AS In s t i tu t e In c. Al l r i g h t s re s e r ve d .

The IN operator is required in the WHERE clause because it can accept multiple values from the
subquery.

Copyright © 2019, SAS Institute Inc., Cary, North Carolina, USA. ALL RIGHTS RESERVED.
1-61

5. An in-line view can return multiple rows and columns.

a. true
b. false

85
C o p y r i g h t © S AS In s t i tu t e In c. Al l r i g h t s re s e r ve d .

An in-line view acts as a virtual table and can return values from one column or multiple columns.

6. Suppose you need to use the most up-to-date data from a complex
query and want multiple users to be able access the information. Which
of the following solutions would be best?

a. Create a table with the complex query and allow users access to
the table.
b. Create a view with the complex query and allow users to access the
view.
c. Send users the complex query to copy and paste in their queries as
an in-line view.

87
C o p y r i g h t © S AS In s t i tu t e In c. Al l r i g h t s re s e r ve d .

A view builds the data when the view is executed and retrieves the most up-to-date information
every time that the view is run.

Copyright © 2019, SAS Institute Inc., Cary, North Carolina, USA. ALL RIGHTS RESERVED.
1-62

7. Which of the following statements creates a new view named


CustomerView from a query?
a. create view CustomerView
select...

b. create view as CustomerView


select...

c. create view CustomerView as


select...

d. create view CustomerView;


select...

89
C o p y r i g h t © S AS In s t i tu t e In c. Al l r i g h t s re s e r ve d .

To create a view from a query result, you use a CREATE VIEW statement with the new view name
and the AS keyword, followed by the query. Adding CREATE VIEW at the beginning of the query
creates a view instead of results from the query.

8. Which method enables a view to be portable—that is, to function


correctly if the underlying tables in the view are stored in different
locations than the view?

a. using a one-level name for the source table in the FROM clause in
the view definition, and storing the view in a different location as
the source table
b. using a two-level name for the source table in the FROM clause in
the view definition, and storing the view in the different location as
the source table
c. embedding the LIBNAME statement with a USING clause when
creating the view
91
C o p y r i g h t © S AS In s t i tu t e In c. Al l r i g h t s re s e r ve d .

Use the embedded LIBNAME statement in the USING clause to make the view definition portable. If
you use a one-level name for the source table in a view definition, the view and the source table
must reside in the same location.

Copyright © 2019, SAS Institute Inc., Cary, North Carolina, USA. ALL RIGHTS RESERVED.
1-63

9. Before you run this query, which of the following changes can you make
to prevent SAS from remerging, and still produce a report?

select Order_Type,
min(Quantity) label="Min Qty"
from orion.order_fact;

a. Add an ORDER BY clause to sort the output rows by the values


of Order_Type.
b. Add a GROUP BY clause to group by Order_Type.
c. Add an additional column to the SELECT list.
d. Specify the PROC SQL NOREMERGE option.

93
C o p y r i g h t © S AS In s t i tu t e In c. Al l r i g h t s re s e r ve d .

The SELECT list in this query specifies both a non-summarized column and a summarized column.
By default, SAS remerges unless you either group by Order_Type or remove the non-summarized
column. Specifying the NOREMERGE option prevents SAS from remerging, but then PROC SQL
does not produce a report.

Copyright © 2019, SAS Institute Inc., Cary, North Carolina, USA. ALL RIGHTS RESERVED.
1-64

inventory Results
10. Which SELECT clause calculates the Product Quantity Product Pct
total percentage of inventory for Shirt 5 Shirt 50%
each product? Shoes 3 Shoes 30%
Pants 2 Pants 20%

a. select Product, Quantity, sum(Quantity) as Total format=percent5.2


from inventory;

b. select Product, (select sum(Quantity) from inventory) as Pct format=percent5.2


from inventory;

c. select Product, Quantity/(select sum(Quantity)) as Pct format=percent5.2


from inventory;

d. select Product, Quantity/sum(Quantity) as Pct format=percent5.2


from inventory;
95
C o p y r i g h t © S AS In s t i tu t e In c. Al l r i g h t s re s e r ve d .

To calculate the percentage for the Pct column, you divide each product's quantity by the sum of all
the quantities. The SELECT clause contains the non-summarized columns Product and Quantity
from the inventory table, and the summarized value of Quantity.
In the f irst pass, PROC SQL calculates the total value of Quantity. In the second pass, PROC SQL
retrieves the Product and Quantity values f or each row, and divides each value of Quantity by the
sum of all quantities f rom the f irst pass.

Copyright © 2019, SAS Institute Inc., Cary, North Carolina, USA. ALL RIGHTS RESERVED.
1-65

Lesson 5

1. How many rows does this query produce?

proc sql;
a b
select * ID Pet ID Pet
from a 1 Cat 1 Cat
intersect
select * 1 Dog 2 Cow
from b; 1 Pig 3 Dog
quit;

a. 1
b. 2
c. 5
d. 6
98
C o p y r i g h t © S AS In s t i tu t e In c. Al l r i g h t s re s e r ve d .

The INTERSECT operator returns the unique rows that occur in both queries.

2. By default, the EXCEPT, INTERSECT, and UNION set operators remove


duplicate rows from the query results.

a. true
b. false

100
C o p y r i g h t © S AS In s t i tu t e In c. Al l r i g h t s re s e r ve d .

Although they do not remove the duplicate rows at the same point, the EXCEPT, INTERSECT, and
UNION set operators remove duplicate rows.

Copyright © 2019, SAS Institute Inc., Cary, North Carolina, USA. ALL RIGHTS RESERVED.
1-66

3. How many rows does this query produce?


a b
proc sql;
select * ID Pet ID Pet
from a 1 Cat 1 Cat
except
select * 1 Dog 2 Cow
from b; 1 Pig 3 Dog
quit;

a. 1
b. 2
c. 5
d. 6
102
C o p y r i g h t © S AS In s t i tu t e In c. Al l r i g h t s re s e r ve d .

The EXCEPT set operator selects unique rows from the first query that are not found in the second
query.

4. Adding the CORR and ALL keywords to the EXCEPT operator would
change the number of rows that this query produces.
a b
proc sql;
select * ID Pet ID Pet
from a 1 Cat 1 Cat
except
select * 1 Dog 2 Cow
from b; 1 Pig 3 Dog
quit;

a. true
b. false

104
C o p y r i g h t © S AS In s t i tu t e In c. Al l r i g h t s re s e r ve d .

Because the tables have no duplicate rows and the columns are named the same, the keywords
cause no changes.

Copyright © 2019, SAS Institute Inc., Cary, North Carolina, USA. ALL RIGHTS RESERVED.
1-67

5. How many rows does this query produce?


a b
proc sql;
select * ID Pet ID Pet
from a 1 Cat 1 Cat
outer union
select * 1 Dog 2 Cow
from b; 1 Pig 3 Dog
quit;

a. 1
b. 2
c. 5
d. 6
106
C o p y r i g h t © S AS In s t i tu t e In c. Al l r i g h t s re s e r ve d .

The OUTER UNION set operator selects all rows from both query results.

6. How many rows does this query produce?


a b
proc sql;
select * ID Pet ID Pet
from a 1 Cat 1 Cat
union
select * 1 Dog 2 Cow
from b; 1 Pig 3 Dog
quit;

a. 2
b. 3
c. 4
d. 5
108
C o p y r i g h t © S AS In s t i tu t e In c. Al l r i g h t s re s e r ve d .

The UNION set operator returns unique rows from both queries.

Copyright © 2019, SAS Institute Inc., Cary, North Carolina, USA. ALL RIGHTS RESERVED.
1-68

7. How many rows does this query produce?


a b
proc sql;
select * ID Pet ID Pet
from a 1 Cat 1 Cat
union all
select * 1 Dog 2 Cow
from b; 1 Pig 3 Dog
quit;

a. 2
b. 4
c. 6
d. 8
110
C o p y r i g h t © S AS In s t i tu t e In c. Al l r i g h t s re s e r ve d .

Because the columns in both tables correspond, no columns are removed. The ALL keyword
instructs PROC SQL to retain all duplicate rows in the results.

8. Which statement is true about using the keyword CORR in a set


operation?

a. When used with the EXCEPT operator, the CORR keyword instructs
PROC SQL to position by same-name columns and eliminate
columns that do not have the same name in both tables from the
final results.
b. When used with the INTERSECT operator, the CORR keyword
instructs PROC SQL to overlay columns by position in the final
results.
c. When used with the OUTER UNION operator, the CORR keyword
instructs PROC SQL to eliminate columns that do not have the same
name in both tables from the final results.
112
C o p y r i g h t © S AS In s t i tu t e In c. Al l r i g h t s re s e r ve d .

When the CORR keyword is used with EXCEPT and INTERSECT, it instructs PROC SQL to align
columns that the query results have in common. The OUTER UNION CORR aligns by like column
names in both tables and includes columns that do not have the same name in both tables.

Copyright © 2019, SAS Institute Inc., Cary, North Carolina, USA. ALL RIGHTS RESERVED.
1-69

9. Which statement is true about using the keyword ALL in a set


operation?

a. Generally, using the keyword ALL is less efficient because it requires


an extra pass through the data.
b. Generally, using the keyword ALL is more efficient because it avoids
an extra pass through the data.
c. PROC SQL does not allow duplicate rows to remain eligible for
processing.
d. The keyword ALL is not an implied part of the OUTER UNION set
operator and is not necessary.

114
C o p y r i g h t © S AS In s t i tu t e In c. Al l r i g h t s re s e r ve d .

Not using the keyword ALL is generally less efficient because it requires PROC SQL to make an
extra pass through the data to eliminate duplicate rows.

10. PROC SQL with set operators handles columns and rows, depending on
the specific set operator and keywords used in the set operation.

a. true
b. false

116
C o p y r i g h t © S AS In s t i tu t e In c. Al l r i g h t s re s e r ve d .

The INTERSECT, EXCEPT, and UNION set operators align columns by position and remove
duplicate rows by default. The OUTER UNION set operator includes all columns and rows by
default.

Copyright © 2019, SAS Institute Inc., Cary, North Carolina, USA. ALL RIGHTS RESERVED.
1-70

Lesson 6

1. Which of the following can be stored in a macro variable?

a. 9.2
b. 10:32
c. 1352050
d. Soccerballs
e. all of the above

119
C o p y r i g h t © S AS In s t i tu t e In c. Al l r i g h t s re s e r ve d .

All of these can be stored as a text string in a macro variable.

Copyright © 2019, SAS Institute Inc., Cary, North Carolina, USA. ALL RIGHTS RESERVED.
1-71

2. Which of the following lines of code correctly references a macro


variable?

a. title "The Sales of the &Country Offices";

b. where salary > 'avgSal';

c. title 'The Sales of the &Country Offices';

121
C o p y r i g h t © S AS In s t i tu t e In c. Al l r i g h t s re s e r ve d .

When you reference a macro variable, you must precede the macro variable name with an
ampersand. You can reference macro variables anywhere in SAS code. When you reference a
macro variable within a text string, you must enclose the text string within doubl e quotation marks.
The macro processor does not resolve macro variable references that appear within single quotation
marks.

3. Which of the following options enables you to view the values of the
macro variables without using them in a report?

a. the SYMBOLLOG system option


b. the SYMBOLGEN system option
c. the NOFEEDBACK PROC SQL option
d. the LABEL= option

123
C o p y r i g h t © S AS In s t i tu t e In c. Al l r i g h t s re s e r ve d .

The SYMBOLGEN system option shows you what the macro variable resolves to in the log.

Copyright © 2019, SAS Institute Inc., Cary, North Carolina, USA. ALL RIGHTS RESERVED.
1-72

4. Which statement correctly creates the user-defined path macro


variable with the value of s:/workshop/data?

a. %user path=s:/workshop/data;

b. %macro path=s:/workshop/data;

c. %path=s:/workshop/data;

d. %let path=s:/workshop/data;

125
C o p y r i g h t © S AS In s t i tu t e In c. Al l r i g h t s re s e r ve d .

To create a user-defined macro variable, you use the %LET statement and specify the name of the
macro variable, followed by an equal sign, and then the text string that you want to store.

5. Which of these queries places one value from the first row returned by
the SQL query into a macro variable?

a. select avg(Salary)
into :MeanSalary
from store.employee;

b. select avg(Salary)
into %MeanSalary
from store.employee;

c. select avg(Salary)
into &MeanSalary
from store.employee;
127
C o p y r i g h t © S AS In s t i tu t e In c. Al l r i g h t s re s e r ve d .

The INTO clause creates one macro variable and assigns the value from the first column of the first
row of the query result. The name in the INTO clause is preceded by a colon.

Copyright © 2019, SAS Institute Inc., Cary, North Carolina, USA. ALL RIGHTS RESERVED.
1-73

6. You created a macro variable named MeanSalary and want to display


the macro variable value in the log. Which statement produces the
value in the log?

a. put &=MeanSalary;

b. put MeanSalary;

c. %put &=MeanSalary;

d. %put MeanSalary;

129
C o p y r i g h t © S AS In s t i tu t e In c. Al l r i g h t s re s e r ve d .

When you want to view the resolved value of a macro variable without having to include it in a
report, you can use the %PUT statement to display the resolved macro variable value along with
descriptive text in the SAS log.

7. You want to store the descriptive statistics of the Price column from the
inventory table into multiple macro variables. Which query successfully
stores mean, median, min, and max?
a. select mean(Price), median(Price), min(msrp), max(Price)
into: MeanPrice, MedianPrice, MinPrice, MaxPrice
from inventory;

b. select mean(Price), median(Price), min(msrp), max(Price)


into MeanPrice, MedianPrice, MinPrice, MaxPrice separated by ","
from inventory;

c. select mean(Price), median(Price), min(msrp), max(Price)


into :MeanPrice, :MedianPrice, :MinPrice, :MaxPrice
from inventory;

131
C o p y r i g h t © S AS In s t i tu t e In c. Al l r i g h t s re s e r ve d .

The INTO clause creates multiple macro variables by specifying each macro variable name with a
colon. Each macro variable is assigned the summarized value from the SELECT clause.

Copyright © 2019, SAS Institute Inc., Cary, North Carolina, USA. ALL RIGHTS RESERVED.
1-74

8. Which SELECT clause stores a list of products in a macro variable named


InventoryList from the inventory table? Each product should be
delimited by a comma.
inventory
a. select Product Product Quantity
into :InventoryList ","
from inventory; Shirt 5
Shoes 3
b. select Product Pants 2
into :InventoryList delimited by ","
from inventory;

c. select Product
into :InventoryList separated by ","
from inventory;

133
C o p y r i g h t © S AS In s t i tu t e In c. Al l r i g h t s re s e r ve d .

Use the SEPARATED BY keywords to specify a character to delimit the values in the macro variable.

9. Given the table revenue below, you want to store the sum of Revenue
in a macro variable named TotalRevenue. Does the following query
store the exact value of 900000000 in TotalRevenue?
revenue
proc sql; Product Revenue
select sum(Revenue) Shirt $200,000,000
into :TotalRevenue
from revenue; Shoes $300,000,000
quit; Pants $400,000,000

a. yes
b. no

135
C o p y r i g h t © S AS In s t i tu t e In c. Al l r i g h t s re s e r ve d .

The query stores the value 9E8. When storing a number, SAS uses the BEST8. format. If the
number is larger than eight digits, scientific notation is often used.

Copyright © 2019, SAS Institute Inc., Cary, North Carolina, USA. ALL RIGHTS RESERVED.
1-75

10. Given the table revenue below, you want to store the sum of Revenue
in a macro variable named TotalRevenue. Which of the following
queries stores the exact value of $900,000,000 in the macro variable
TotalRevenue?
revenue
a. select sum(Revenue) format=dollar16. Product Revenue
into :TotalRevenue
from revenue; Shirt $200,000,000
Shoes $300,000,000
b. select sum(Revenue) Pants $400,000,000
into :TotalRevenue format=dollar16.
from revenue;

c. select sum(Revenue)
into :TotalRevenue
from revenue; 137
C o p y r i g h t © S AS In s t i tu t e In c. Al l r i g h t s re s e r ve d .

Use the FORMAT= column modifier in the SELECT clause to format the summarized value.

Copyright © 2019, SAS Institute Inc., Cary, North Carolina, USA. ALL RIGHTS RESERVED.
1-76

Lesson 7

1. SAS/ACCESS technology enables you to read data that is in a third-party


DBMS from within SAS.

a. true
b. false

140
C o p y r i g h t © S AS In s t i tu t e In c. Al l r i g h t s re s e r ve d .

SAS/ACCESS technology is a family of interfaces, each licensed separately, that enables you to
read data in a third-party DBMS from within SAS. SAS/ACCESS technology for relational databases
provides an interface engine between SAS software and data in other vendors' database
management systems.

Copyright © 2019, SAS Institute Inc., Cary, North Carolina, USA. ALL RIGHTS RESERVED.
1-77

2. You're familiar with the Oracle SQL implementation. You begin to use
PROC SQL but don't want to learn any of the SAS enhancements and
functions. You would like to continue to use the Oracle SQL
implementation inside SAS. Which method would you use to interact
with the Oracle database?

a. SQL pass-through (explicit pass through)


b. LIBNAME statement (implicit pass through)

142
C o p y r i g h t © S AS In s t i tu t e In c. Al l r i g h t s re s e r ve d .

To accomplish your task, the best option is to use the SQL pass-through facility, which enables you
to send DBMS-specific SQL statements directly to a DBMS for execution. The syntax executes as if
you are simply coding inside the database, and processing occurs inside the database.

3. You're familiar with using PROC SQL and are connecting to an SQL
Server database. You are not familiar with the database implementation
of SQL and want to continue using PROC SQL. Which method would you
use to interact with the SQL Server database?

a. SQL Pass-Through (Explicit Pass Through)


b. LIBNAME Statement (Implicit Pass Through)

144
C o p y r i g h t © S AS In s t i tu t e In c. Al l r i g h t s re s e r ve d .

The SAS/ACCESS LIBNAME Engine translates your PROC SQL syntax to native DBMS SQL when
possible. This is important because SAS SQL and native database SQL can differ. So if you are
working with multiple databases, you can learn SAS SQL instead of learning native database SQL
implementations and let the engine do the work for you.

Copyright © 2019, SAS Institute Inc., Cary, North Carolina, USA. ALL RIGHTS RESERVED.
1-78

4. Which query executes correctly in PROC FEDSQL?

a. proc fedsql;
select EmployeeID, Name, Salary, Department
from orion.employee
where = "Management";
quit;

b. proc fedsql;
select EmployeeID, Name, Salary, Department
from orion.employee
where = 'Management';
quit;

146
C o p y r i g h t © S AS In s t i tu t e In c. Al l r i g h t s re s e r ve d .

When using FEDSQL, you must use single quotation marks around a text string in the WHERE
clause. Double quotation marks are a SAS enhancement and do not run in FEDSQL.

5. You are working with an Oracle database and want to push as much of
the processing as possible into the database. Which PROC would be
better to use?

a. PROC SQL
b. PROC FEDSQL

148
C o p y r i g h t © S AS In s t i tu t e In c. Al l r i g h t s re s e r ve d .

PROC FEDSQL is a vendor-neutral ANSI SQL that pushes as much of the processing as possible
into the database.

Copyright © 2019, SAS Institute Inc., Cary, North Carolina, USA. ALL RIGHTS RESERVED.

You might also like