M5L2 Homwork
M5L2 Homwork
M5L2 Homwork
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
CODE:
data work.thirdqtr;
set orion.mnth7_2011 orion.mnth8_2011 orion.mnth9_2011;
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.
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:
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
CODE:
proc contents data=orion.sales;
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;
LOG:
10282 proc contents data=orion.sales;
10283 run;
10284
10285 proc contents data=orion.nonsales;
10286 run;
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.
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.
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
CODE:
proc contents data=orion.orders;
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;
10437
10438 proc contents data=orion.order_item;
10439 run;
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
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
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;
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.