Vous êtes sur la page 1sur 5

MATH2871 DATA MANAGEMENT FOR STATISTICAL ANALYSIS

SEMESTER 2, 2016
Lab exercise Week 13: SOLUTIONS

1. Handling Same-Named Variables and Different Data Types for BY Variables


The data set orion.web_products2 contains an observation for every product
available for sale on Orion Stars wholesale Web site.
The data set orion.web_orders2 contains a list of orders made in a single day from
the Web site. Each observation contains the product ID, the quantity ordered, and the
customers name.
The two data sets are sorted by Product_ID. Product_ID is numeric in
orion.web_products2 and character with a length of 12 in orion.web_orders2.
a. Create a new data set, web_converted, from orion.web_products2 data
set. Change the type of Product_ID to character. (Use web_converted to
merge with orion.web_orders2 in the next step.)
Hint: Use the RENAME= data set option to change Product_ID.

b. Create three new data sets:

A data set named revenue contains the product code, the price, the
quantity sold, the product name, the customer name and the revenue
generated from each sale. Revenue is calculated as Price*Quantity.
The Name variable in web_converted refers to the product name and the
Name variable in web_orders2 refers to the customer name. Give each
variable an appropriate name in the revenue data set.

A data set named notsold contains the product code, price, and product
name for each product that was not sold.
A data set named invalidcode contains the product code, quantity, and
customer name for each observation in the web_orders2 data set that
does not have a corresponding product code in the web_products2 data
set.

c. Print the three data sets with appropriate titles. The data sets should contain
39, 7,and 4 observations, respectively.

Partial listing of revenue.


Revenue from Orders
Product_ID
120400305288
120400305288
120400305846
120400305846
120400305846
120400308766

Price
53.26
53.26
107.74
107.74
107.74
40.96

Product_Name
Knife
Knife
Big Guy
Big Guy
Big Guy
Big Guy

Men's
Men's
Men's
Men's

Quantity

Air Deschutz Viii Shoes


Air Deschutz Viii Shoes
Air Deschutz Viii Shoes
Packable Hiking Shorts

16
19
13
13
10
13

Customer

Revenue

Carglar Aydemir
Sanelisiwe Collier
Candy Kinsey
Cynthia Martinez
Rolf Robak
Ahmet Canko

852.16
1011.94
1400.62
1400.62
1077.40
532.48

Listing of notsold.
Revenue Not Orders
Product_ID
120400304333
120400308849
120400311211
120400317183
120400329978
120400330339
120400330967

Price
114.36
12.23
69.16
164.82
114.47
31.74
38.73

Product_Name
Smasher Super Rq Ti 350 Tennis Racket
Wood Box for 6 Balls
Tipee Summer Sleeping Bag
Smasher Rd Ti 70 Tennis Racket
Tipee Twin Blue/Orange
Small Belt Bag, Black
Duwall Pants

Listing of invalidcode.

Invalid Orders
Product_ID

Quantity

120400311465
120400312556
120400315078
120400326278

13
7
23
10

Customer
Thomas Leitmann
Robyn Klem
Tonie Asmussen
Theunis Brazier

data web_converted (drop=nProduct_ID);


length Product_ID $ 12;
set orion.web_products2 (rename=(Product_ID=nProduct_ID));
Product_ID=put(nProduct_ID,12.);
run;
data revenue
notsold(keep=Price Product_ID Product_Name)
invalidcode (keep=Product_ID Quantity Customer);
merge web_converted (in=InConv rename=(Name=Product_Name))
orion.web_orders2(in=InOrders rename=(Name=Customer));
by Product_ID;
if InConv and InOrders then do;
Revenue=Quantity*Price;
output revenue;
end;
else if InConv and not InOrders then output notsold;
else if not InConv and InOrders then output invalidcode;
run;
title 'Revenue from Orders';
proc print data=revenue noobs; run;
title 'Revenue Not Orders';
proc print data=notsold noobs; run;
title 'Invalid Orders';
proc print data=invalidcode noobs; run;

2. Coding a PROC SQL Inner Join

The table orion.order_fact contains a group of orders.


The table orion.product_dim contains descriptions of products.
Both tables have a column named Product_ID.

Write a POC SQL inner join to combine information from these tables.

Use the column Prodcut_ID to match rows.


Display columns Order_ID, Product_ID, Prodcut_Name, and Quantity
in the results.
Add an appropriate title.

Partial PROC SQL output.


Detail Information for Ordered Products and Quantities

Quantity
Order ID
Product ID Product Name
Ordered

1243960910 210200100009 Kids Sweat Round Neck,Large Logo


2
1234198497 210200100017 Sweatshirt Children's O-Neck
1
1235926178 210200200022 Sunfit Slow Swimming Trunks
2
1240886449 210200200023 Sunfit Stockton Swimming Trunks Jr.
1
1242149082 210200300006 Fleece Cuff Pant Kid'S
1
1237789102 210200300007 Hsc Dutch Player Shirt Junior
1

title 'Detail Information for Ordered Products and


Quantities';
proc sql;
select Order_ID, o.Product_ID, Product_Name, Quantity
from orion.order_fact as o, orion.product_dim as p
where o.Product_ID=p.Product_ID;
quit;
title;

3. Creating and Using a Macro Variable


a. Generate a report showing a list of all employees earning at least a
designated minimum salary.

Write a program to create a macro variable, minSal, and set its value to
represent a minimum salary of $60,000.
Use the orion.employee_payroll data set and the PRINT procedure to
generate the report below.
- Format birth_date, employee_date, and employee_term_date with
the DATE9. Format.
- Add an appropriate title and verify your results

Partial PROC PRINT output (34 total observations)


Employee Earning at Least $60000

Obs

Employee_ID

1
2
3
99

120101
120102
120103
120259

Employee_
Gender

Salary

Birth_
Date

M
M
M
M

163040
108255
87975
433800

6074
3510
-3996
1485

Employee_
Hire_Date
15887
10744
5114
10836

Employee_
Term_Date

Marital_
Status

.
.
.
.

%let minSal=60000;
title "Employee Earning at Least $&minSal";
proc print data=orion.employee_payroll;
where salary>=&minSal;
run;

S
O
M
M

Dependents
0
2
1
1

b. Modify the previous solution to display employees who earn at least


$100,000. Verify your results.
PROC PRINT output.
Employee Earning at Least $100000

Obs

Employee_ID

1
2
99
100
101
102
124
417
418

120101
120102
120259
120260
120261
120262
120659
121141
121142

Employee_
Gender

Salary

Birth_
Date

M
M
M
F
M
M
M
M
M

163040
108255
433800
207885
243190
268455
161290
194885
156065

6074
3510
1485
1797
3339
3581
-3821
-5674
3332

Employee_
Hire_Date

Employee_
Term_Date

15887
10744
10836
9071
10074
10471
5114
5114
12174

.
.
.
.
.
.
.
.
.

Marital_
Status
S
O
M
M
O
M
M
S
M

%let minSal=100000;
title "Employee Earning at Least $&minSal";
proc print data=orion.employee_payroll;
where salary>=&minSal;
run;

Dependents
0
2
1
2
1
2
3
0
2

Vous aimerez peut-être aussi