Nothing Special   »   [go: up one dir, main page]

M5L2 Homwork

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 11

CHAPTER 10

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? 12
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

CODE:
data work.thirdqtr;
set orion.mnth7_2011 orion.mnth8_2011 orion.mnth9_2011;
run;

proc print data=work.thirdqtr;


run;

LOG:
10240 data work.thirdqtr;
10241 set orion.mnth7_2011 orion.mnth8_2011 orion.mnth9_2011;
10242 run;

NOTE: There were 10 observations read from the data set ORION.MNTH7_2011.
NOTE: There were 12 observations read from the data set ORION.MNTH8_2011.
NOTE: There were 10 observations read from the data set ORION.MNTH9_2011.
NOTE: The data set WORK.THIRDQTR has 32 observations and 6 variables.
NOTE: DATA statement used (Total process time):
real time 0.00 seconds
cpu time 0.00 seconds

10243
2 Error! No text of specified style in document. Error! No text of specified style in document.

10244 proc print data=work.thirdqtr;


10245 run;

NOTE: There were 32 observations read from the data set WORK.THIRDQTR.
NOTE: PROCEDURE PRINT used (Total process time):
real time 0.06 seconds
cpu time 0.01 seconds

RESULT:

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?

Copyright © 2015, SAS Institute Inc., Cary, North Carolina, USA. ALL RIGHTS RESERVED.
Error! No text of specified style in document. Error! No text of specified style in document. 3

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
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

CODE:
proc contents data=orion.sales;
run;

proc contents data=orion.nonsales;


run;

data work.allemployees;
set orion.sales orion.nonsales(rename=(First=First_Name Last=Last_Name));
keep Employee_ID First_Name Last_Name Job_Title Salary;
run;

proc print data=work.allemployees;


run;

LOG:
10282 proc contents data=orion.sales;
10283 run;

NOTE: PROCEDURE CONTENTS used (Total process time):


real time 0.05 seconds
cpu time 0.01 seconds

10284
10285 proc contents data=orion.nonsales;
10286 run;

NOTE: PROCEDURE CONTENTS used (Total process time):


real time 0.05 seconds
cpu time 0.00 seconds

10287

Copyright © 2015, SAS Institute Inc., Cary, North Carolina, USA. ALL RIGHTS RESERVED.
4 Error! No text of specified style in document. Error! No text of specified style in document.

10288 data work.allemployees;


10289 set orion.sales orion.nonsales(rename=(First=First_Name Last=Last_Name));
10290 keep Employee_ID First_Name Last_Name Job_Title Salary;
10291 run;

NOTE: There were 165 observations read from the data set ORION.SALES.
NOTE: There were 235 observations read from the data set ORION.NONSALES.
NOTE: The data set WORK.ALLEMPLOYEES has 400 observations and 5 variables.
NOTE: DATA statement used (Total process time):
real time 0.00 seconds
cpu time 0.00 seconds

10292
10293 proc print data=work.allemployees;
10294 run;

NOTE: There were 400 observations read from the data set WORK.ALLEMPLOYEES.
NOTE: PROCEDURE PRINT used (Total process time):
real time 0.17 seconds
cpu time 0.10 seconds

RESULT:

Copyright © 2015, SAS Institute Inc., Cary, North Carolina, USA. ALL RIGHTS RESERVED.
Error! No text of specified style in document. Error! No text of specified style in document. 5

Copyright © 2015, SAS Institute Inc., Cary, North Carolina, USA. ALL RIGHTS RESERVED.
6 Error! No text of specified style in document. Error! No text of specified style in document.

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

orion.charities Char 6 Char 40 Char 10

orion.us_supplier Char 6 Char 30 Char 1


s

orion.consultants Char 6 Char 30 Num 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

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
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.

Copyright © 2015, SAS Institute Inc., Cary, North Carolina, USA. ALL RIGHTS RESERVED.
Error! No text of specified style in document. Error! No text of specified style in document. 7

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

CODE:
proc contents data=orion.orders;
run;

proc contents data=orion.order_item;


run;

data work.allorders;
merge orion.orders orion.order_item;
by Order_ID;
keep Order_ID Order_Item_Num Order_Type Order_Date Quantity
Total_Retail_Price;
run;
proc print data=work.allorders noobs;
where qtr(Order_Date)=4 and year(Order_Date)=2011;

run;

LOG:
10435 proc contents data=orion.orders;
10436 run;

NOTE: PROCEDURE CONTENTS used (Total process time):


real time 0.06 seconds
cpu time 0.01 seconds

10437
10438 proc contents data=orion.order_item;
10439 run;

NOTE: PROCEDURE CONTENTS used (Total process time):


real time 0.05 seconds
cpu time 0.01 seconds

10440
10441 data work.allorders;
10442 merge orion.orders orion.order_item;
10443 by Order_ID;
10444 keep Order_ID Order_Item_Num Order_Type Order_Date Quantity Total_Retail_Price;
10445 run;

Copyright © 2015, SAS Institute Inc., Cary, North Carolina, USA. ALL RIGHTS RESERVED.
8 Error! No text of specified style in document. Error! No text of specified style in document.

NOTE: There were 490 observations read from the data set ORION.ORDERS.
NOTE: There were 732 observations read from the data set ORION.ORDER_ITEM.
NOTE: The data set WORK.ALLORDERS has 732 observations and 6 variables.
NOTE: DATA statement used (Total process time):
real time 0.00 seconds
cpu time 0.01 seconds

10446 proc print data=work.allorders noobs;


10447 where qtr(Order_Date)=4 and year(Order_Date)=2011;
10448 run;

NOTE: There were 35 observations read from the data set WORK.ALLORDERS.
WHERE (QTR(Order_Date)=4) and (YEAR(Order_Date)=2011);
NOTE: PROCEDURE PRINT used (Total process time):
real time 0.07 seconds
cpu time 0.03 seconds

Copyright © 2015, SAS Institute Inc., Cary, North Carolina, USA. ALL RIGHTS RESERVED.
Error! No text of specified style in document. Error! No text of specified style in document. 9

RESULTS

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.

Copyright © 2015, SAS Institute Inc., Cary, North Carolina, USA. ALL RIGHTS RESERVED.
10 Error! No text of specified style in document. Error! No text of specified style in document.

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
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

CODE:
proc sort data=orion.product_list
out=work.product_list;
by Product_Level;
run;

data work.listlevel;
merge orion.product_level work.product_list;
by Product_Level;
keep Product_ID Product_Name Product_Level Product_Level_Name;
run;

proc print data=work.listlevel;


where Product_Level=3;
run;

LOG:
10449 proc sort data=orion.product_list
10450 out=work.product_list;
10451 by Product_Level;
10452 run;

NOTE: There were 556 observations read from the data set ORION.PRODUCT_LIST.
NOTE: The data set WORK.PRODUCT_LIST has 556 observations and 5 variables.
NOTE: PROCEDURE SORT used (Total process time):
real time 0.00 seconds
cpu time 0.01 seconds

10453
10454 data work.listlevel;
10455 merge orion.product_level work.product_list;
10456 by Product_Level;
10457 keep Product_ID Product_Name Product_Level Product_Level_Name;
10458 run;

NOTE: There were 4 observations read from the data set ORION.PRODUCT_LEVEL.
NOTE: There were 556 observations read from the data set WORK.PRODUCT_LIST.
NOTE: The data set WORK.LISTLEVEL has 556 observations and 4 variables.
NOTE: DATA statement used (Total process time):
real time 0.05 seconds
cpu time 0.03 seconds

10459
10460 proc print data=work.listlevel;
10461 where Product_Level=3;
10462 run;

Copyright © 2015, SAS Institute Inc., Cary, North Carolina, USA. ALL RIGHTS RESERVED.
Error! No text of specified style in document. Error! No text of specified style in document. 11

NOTE: There were 13 observations read from the data set WORK.LISTLEVEL.
WHERE Product_Level=3;
NOTE: PROCEDURE PRINT used (Total process time):
real time 0.07 seconds
cpu time 0.01 seconds

RESULT:

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

You might also like