0% found this document useful (0 votes)
177 views6 pages

M5L2 CDS Homework

This document provides exercises on combining and merging SAS data sets. The first level focuses on concatenating like-structured and unlike-structured data sets. The second level covers concatenating data sets with variables of different lengths and types. The third level involves merging two sorted data sets in a one-to-many merge and merging a sorted data set with an unsorted data set. The final levels demonstrate using the IN= and RENAME= options when merging data sets.

Uploaded by

Richard Liu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
177 views6 pages

M5L2 CDS Homework

This document provides exercises on combining and merging SAS data sets. The first level focuses on concatenating like-structured and unlike-structured data sets. The second level covers concatenating data sets with variables of different lengths and types. The third level involves merging two sorted data sets in a one-to-many merge and merging a sorted data set with an unsorted data set. The final levels demonstrate using the IN= and RENAME= options when merging data sets.

Uploaded by

Richard Liu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Module 5 - Lesson 2 Combining Data Sets

Exercises

If you restarted your SAS session since the last exercise, open and submit the libname.sas program that
can be found in the data folder.

Level 1
1. Concatenating Like-Structured Data Sets
a. Write and submit a DATA step to concatenate orion.mnth7_2011, orion.mnth8_2011, and
orion.mnth9_2011 to create a new data set, work.thirdqtr.
How many observations in work.thirdqtr are from orion.mnth7_2011?
10
How many observations in work.thirdqtr are from orion.mnth8_2011?
10
How many observations in work.thirdqtr are from orion.mnth9_2011?
10
b. Write a PROC PRINT step to create the report below. The results should contain 32 observations. Partial
PROC PRINT Output
Order_ Order_ Delivery_
Obs Order_ID Type Employee_ID Customer_ID Date Date
1 1242691897 2 99999999 90 02JUL2011 04JUL2011
2 1242736731 1 121107 10 07JUL2011 07JUL2011
3 1242773202 3 99999999 24 11JUL2011 14JUL2011
4 1242782701 3 99999999 27 12JUL2011 17JUL2011
5 1242827683 1 121105 10 17JUL2011 17JUL2011

2. Concatenating Unlike-Structured Data Sets


Open p110e02. Submit the two PROC CONTENTS steps or explore the data sets interactively to compare the
variables in the two data sets. What are the names of the two variables that are different in the two data sets?
orion.sales orion.nonsales

First_Name First

Last_Name Last

a. Add a DATA step after the PROC CONTENTS steps to concatenate orion.sales and orion.nonsales to
create a new data set, work.allemployees.
Use a RENAME= data set option to change the names of the different variables in orion.nonsales.
The new data set should include only Employee_ID, First_Name, Last_Name, Job_Title, and
Salary.
b. Add a PROC PRINT step to create the report below. The results should contain 400 observations.
Partial PROC PRINT Output

1
Module 5 - Lesson 2 Combining Data Sets
First_
Obs Employee_ID Name Last_Name Salary Job_Title
1 120102 Tom Zhou 108255 Sales Manager
2 120103 Wilson Dawes 87975 Sales Manager
3 120121 Irenie Elvish 26600 Sales Rep. II
4 120122 Christina Ngan 27475 Sales Rep. II
5 120123 Kimiko Hotstone 26190 Sales Rep. I

Level 2
3. Concatenating Data Sets with Variables of Different Lengths and Types
a. Open p110e03. Submit the PROC CONTENTS steps or explore the data sets interactively to complete the
table below. Fill in attribute information for each variable in each data set.

Code Company ContactType

Type Length Type Length Type Length

Char 6 Char 40 Char 10


orion.charities

Char 6 Char 30 Char 1


orion.us_suppliers

orion.consultants Char 6 Char 30 Char 8

b. Write a DATA step to concatenate orion.charities and orion.us_suppliers and create a temporary data set,
contacts.
c. Submit a PROC CONTENTS step to examine work.contacts. From which input data set were the variable
attributes assigned? The first data set in the set statement, orion charities
d. Write a DATA step to concatenate orion.us_suppliers and orion.charities and create a temporary data set,
contacts2. Notice that these are the same data sets as in the previous program, but they are in reverse
order.
e. Submit a PROC CONTENTS step to examine work.contacts2. From which input data set were the
variable attributes assigned? The first data set in the set statement, orion.us_suppliers
f. Write a DATA step to concatenate orion.us_suppliers and orion.consultants and create a temporary data
set, contacts3.
Why did the DATA step fail? Variable ContactType has been defined as both character and numeric

2
Module 5 - Lesson 2 Combining Data Sets

If you restarted your SAS session since the last exercise, open and submit the libname.sas program that
can be found in the data folder.

Level 1
4. Merging Two Sorted Data Sets in a One-to-Many Merge
a. Retrieve the starter program p110e04.
b. Submit the two PROC CONTENTS steps or explore the data sets interactively to determine the
common variable among the two data sets.
c. Add a DATA step after the two PROC CONTENTS steps to merge orion.orders and orion.order_item by
the common variable to create a new data set, work.allorders. A sort is not required because the data
sets are already sorted by the common variable.
d. Submit the program and confirm that work.allorders was created with 732 observations and 12
variables.
e. Add a statement to subset the variables. The new data set should contain six variables: Order_ID,
Order_Item_Num, Order_Type, Order_Date, Quantity, and Total_Retail_Price.
f. Write a PROC PRINT step to create the report below. Include only those observations with a value for
Order_Date in the fourth quarter of 2011. The results should contain 35 observations.
Order_ Order_ Order_ Total_Retail_
Order_ID Type Date Item_Num Quantity Price
1243515588 1 01OCT2011 1 1 $251.80
1243515588 1 01OCT2011 2 1 $114.20
1243568955 1 07OCT2011 1 1 $172.50
1243643970 1 16OCT2011 1 1 $101.50
1243644877 3 16OCT2011 1 1 $14.60

Level 2
5. Merging a Sorted Data Set and an Unsorted Data Set in a One-to-Many Merge
a. Sort orion.product_list by Product_Level to create a new data set, work.product_list.
b. Merge orion.product_level with the sorted data set. Create a new data set, work.listlevel, which includes
only Product_ID, Product_Name, Product_Level, and Product_Level_Name.
c. Create the report below. Include only those observations with Product Level equal to 3. The results
should contain 13 observations.
Partial PROC PRINT Output

3
Module 5 - Lesson 2 Combining Data Sets
Product_ Product_Level_
Level Name Product_ID Product_Name
3 Product Category 210100000000 Children Outdoors
3 Product Category 210200000000 Children Sports
3 Product Category 220100000000 Clothes
3 Product Category 220200000000 Shoes
3 Product Category 230100000000 Outdoors

Challenge
6. Using the MERGENOBY Option
a. Use the SAS Help facility or online documentation to explore the MERGENOBY system option.
What is the purpose of this option and why is it used?

b. Complete the following table to include the values that this option can assume:

Value Description Default (Y/N)

If you restarted your SAS session since the last exercise, open and submit the libname.sas program that
can be found in the data folder.

Level 1
7. Merging Using the IN= Option
a. Retrieve the starter program p110e07. Add a DATA step after the PROC SORT step to merge
work.product and orion.supplier by Supplier_ID to create a new data set, work.prodsup.
b. Submit the program and confirm that work.prodsup was created with 556 observations and 10 variables.
c. Modify the DATA step to output only those observations that are in work.product but not orion.supplier.
A subsetting IF statement that references IN= variables listed in the MERGE statement must be added.
d. Submit the program and confirm that work.prodsup was created with 75 observations and 10 variables.
e. Submit the PROC PRINT step to create the report below. The results should contain 75 observations.

 Supplier information is missing in the output.


Supplier_ Obs Product_ID Product_Name Supplier_ID Name
1 210000000000 Children .
2 210100000000 Children Outdoors .
3 210100100000 Outdoor things, Kids .

4
Module 5 - Lesson 2 Combining Data Sets
4 210200000000 Children Sports . 5 210200100000 A-Team, Kids .

Level 2
8. Merging Using the IN= and RENAME= Options
a. Write a PROC SORT step to sort orion.customer by Country to create a new data set,
work.customer.
b. Write a DATA step to merge the resulting data set with orion.lookup_country by Country to create
a new data set, work.allcustomer.
In the orion.lookup_country data set, rename Start to Country and rename Label to Country_Name.
Include only four variables: Customer_ID, Country, Customer_Name, and Country_Name.
c. Create the report below. The results should contain 308 observations.
Partial PROC PRINT Output
Obs Customer_ID Country Customer_Name Country_Name
1 . AD Andorra
2 . AE United Arab Emirates
...
306 3959 ZA Rita Lotz South Africa
307 . ZM Zambia
308 . ZW Zimbabwe

d. Modify the DATA step to store only those observations that contain both customer information
and country information. A subsetting IF statement that references the IN= variables in the MERGE
statement must be added.
e. Submit the program to create the report below. The results should contain 77 observations. Partial
PROC PRINT Output
Country_ Obs Customer_ID Country Customer_Name
Name
1 29 AU Candy Kinsey Australia
2 41 AU Wendell Summersby Australia
3 53 AU Dericka Pockran Australia
4 111 AU Karolina Dokter Australia
5 171 AU Robert Bowerman Australia

Challenge
9. Merging and Creating Output in Multiple Data Sets
a. Write a PROC SORT step to sort orion.orders by Employee_ID to create a new data set, work.orders.
b. Write a DATA step to merge orion.staff and work.orders by Employee_ID and create two new data sets:
work.allorders and work.noorders.
• The work.allorders data set should include all observations from work.orders, regardless of matches
or nonmatches from the orion.staff data set.
• The work.noorders data set should include only those observations from orion.staff that do not have a
match in work.orders.

5
Module 5 - Lesson 2 Combining Data Sets
• Both new data sets should include only Employee_ID, Job_Title, Gender, Order_ID, Order_Type,
and Order_Date.
c. Submit the program and confirm that work.allorders was created with 490 observations and six variables
and work.noorders was created with 324 observations and six variables.
d. Create a detailed listing report for each new data set with an appropriate title.

You might also like