0% found this document useful (0 votes)
6 views18 pages

stat 440 lab exercises 14

The document provides detailed instructions for completing Lab Exercises 14 in Statistics 440, focusing on importing and exporting data between SAS and Microsoft Access. It includes steps for using the Import Wizard to read Excel files into SAS, as well as the Export Wizard to transfer SAS data sets to Access. Additionally, it covers creating relationships between tables in Access and executing queries using both the Query Wizard and SQL statements.

Uploaded by

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

stat 440 lab exercises 14

The document provides detailed instructions for completing Lab Exercises 14 in Statistics 440, focusing on importing and exporting data between SAS and Microsoft Access. It includes steps for using the Import Wizard to read Excel files into SAS, as well as the Export Wizard to transfer SAS data sets to Access. Additionally, it covers creating relationships between tables in Access and executing queries using both the Query Wizard and SQL statements.

Uploaded by

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

Statistics 440: Lab Exercises 14

Before you begin:


Login to Illinois Compass (compass.uiuc.edu) and download the SAS and Excel data files from
the Data\Lab 14 folder. Save them in ‘D:\netid’. Create the libref ‘st’ which points to ‘D:\netid’
(or folder of your choice). When you reference the SAS data sets, use the library ‘st’.
Part I: Reading External Data Files with the Import Wizard
Use the Import Wizard to import the file inventory.xls into SAS. This is an Excel file that
contains information on products in stock. Name the resulting data set work.inventory.
1. Select File  Import Data….

2. From the list box, select Microsoft Excel 97/2000/2002/2003 Workbook.


3. Select Next >. The Connect to MS Excel window opens.

1
Statistics 440: Lab Exercises 14

Select Browse… to specify a workbook file to import from the Open window. After you
select the pathname, select Open to complete your selections and return to the Connect to MS
Excel window.
4. Select OK. The Import Wizard – Select Table window opens:

2
Statistics 440: Lab Exercises 14

5. Select Sheet_1, the name of the worksheet (or named range) to be imported.
 You can select Options… to change default import settings through the SAS Import:
Spreadsheet Options window.

6. Select Next > to open the Import Wizard – Select library and member window, where you
specify the storage location for the imported file.

7. In the Library box on the left, leave the library as WORK. In the Member box on the right, type
inventory.
You can also select the down arrow in the Library box and select a different library. You can
select the down arrow in the Member box to select an existing data set. If you select an
existing data set, you will be asked later to verify that you want to replace it.

3
Statistics 440: Lab Exercises 14

8. Select Next > to move to the next window or Finish to create the SAS data set from the
Excel spreadsheet.
If you select Finish and you select the name of an existing SAS data set for the name of your
new SAS data set (in the Import Wizard – Select library and member window), you are
prompted to determine whether or not you want to replace the existing data set. Select OK or
Cancel.
If you select Next >, you are taken to the Import Wizard – Create SAS Statements window.

9. Select Browse… to specify a location from the Save As window. After you select the
pathname, select Save to complete your selections and return to the Import Wizard – Create
SAS Statements window.
If the file already exists, you are prompted to replace the existing file, append to the existing
file, or cancel the save.
10. Select Finish.
11. Check the log to see that the SAS data set is successfully created.
NOTE: WORK.INVENTORY was successfully created.

12. Go to the Program Editor window and write SAS code to print the data set.
proc print data=work.inventory;
run;

4
Statistics 440: Lab Exercises 14

13. Go to the Program Editor window and open the SAS code created by the Import Wizard.
PROC IMPORT OUT=WORK. inventory
DATAFILE="D:\netid\inventory.xls"
DBMS=EXCEL REPLACE;
SHEET="Sheet_1";
GETNAMES=YES;
MIXED=NO;
SCANTEXT=YES;
USEDATE=YES;
SCANTIME=YES;
RUN;
The default is MIXED=NO, which means that numeric data will be imported as missing values in
a character column. The MIXED= option is available only when reading Excel data.
When using the Import Wizard, the MIXED=YES option can be set by selecting Convert
numeric values to characters in a mixed types column in the SAS Import: Spreadsheet Options
window.

Other commonly used PROC IMPORT data source statements include


GETNAMES=NO;
Do not attempt to generate SAS variable names from the column names in the input file's
first row of data.
SHEET="spreadsheet-name";
Read a particular spreadsheet from a file that contains multiple spreadsheets.

5
Statistics 440: Lab Exercises 14

The IMPORT Procedure


General form of the IMPORT procedure:

PROC
PROCIMPORT
IMPORTOUT=SAS-data-set
OUT=SAS-data-set
DATAFILE='external-file-name‘
DATAFILE='external-file-name‘
<< DBMS=file-type
DBMS=file-type >> <REPLACE>;
<REPLACE>;
RUN;
RUN;

REPLACE
overwrites an existing SAS data set.

106

Available DBMS Specifications


Identifier Input Data Source Extension

ACCESS Microsoft Access table .MDB

CSV delimited file (comma-separated values) .CSV

DBF dBASE 5.0, IV, III+, and II files .DBF

DLM delimited file (default delimiter is a blank) .*

EXCEL Microsoft Excel spreadsheet .XLS

JMP JMP table .JMP

TAB delimited file (tab-delimited values) .TXT


WK1 Lotus 1-2-3 Release 2 spreadsheet .WK1

WK3 Lotus 1-2-3 Release 3 spreadsheet .WK3

WK4 Lotus 1-2-3 Release 4 or 5 spreadsheet .WK4

If external-file-name contains a valid extension so that PROC IMPORT can recognize the type of
data, you may omit the DBMS= option.

6
Statistics 440: Lab Exercises 14

Part II: Writing Data to an External File with the Export Wizard
Use the Export Wizard to import the SAS data set customers into MS ACESS.
1. From the Start menu, open Microsoft Office Access. Create a new MS Access database called
“officesupply.accdb” and save it in ‘D:\netid’. Close the database. You cannot export tables
into an Access database from SAS while it is open.
2. Go back to your SAS session. Select File  Export Data….
3. In the Select library and member window, specify the library st and member (SAS data set)
customers. Select Next >.

4. From the list box, select Microsoft Access 200x Database. Select Next >.

7
Statistics 440: Lab Exercises 14

5. The Connect to MS Acess window opens.

Select Browse… to specify a database file to export to from the Open window. Select the
pathname ‘D:\netid\officesupply.mdb’.
6. Select OK. The Export Wizard – Select Table window opens:

8
Statistics 440: Lab Exercises 14

7. Type customers, the name of the table to export to.


8. Select Next > to move to the next window or Finish to create the Access database table from
the SAS data set.
If you select Next >, you are taken to the Export Wizard – Create SAS Statements window.

9
Statistics 440: Lab Exercises 14

9. Select Browse… to specify a location from the Save As window (use D:\netid\export.sas).
After you select the pathname, select Save to complete your selections and return to the
Export Wizard – Create SAS Statements window.
If the file already exists, you are prompted to replace the existing file, append to the existing
file, or cancel the save.
10. Select Finish.
11. Check the log to see that the SAS data set is successfully created.
NOTE: ’customers’ was successfully created.

12. Go to the Program Editor window and open the SAS code created by the Export Wizard.
PROC EXPORT DATA= ST.CUSTOMERS
OUTTABLE= "customers"
DBMS=ACCESS REPLACE;
DATABASE="D:\netid\officesupply.mdb";
RUN;

13. Edit the SAS code created by the Export Wizard to export the SAS data sets inventory,
invoice, manufacturers, and products.

10
Statistics 440: Lab Exercises 14

Part III: Working with MS Access Databases


1. Go back to your MS Access session and open the database ‘officesupply’. The five tables
you exported should now be in your database. From the Microsoft Office Menu, save the
database as a 2007 database.
2. You can browse the contents of a table by double-clicking on it or by right-clicking and
selecting Open. To browse or edit its properties, select Design View.

3. Creating Relationships
a. To open the Relationships window, choose Database Tools tab and click the Relationship
button on toolbar. Since no relationships have been defined, the Show Table
window opens.

11
Statistics 440: Lab Exercises 14

b. Select all five tables then click Add. The Relationships window appears.

12
Statistics 440: Lab Exercises 14

c. To relate the customers table to the invoice table by CUSTNUM, drag the CUSTNUM
field from customers to the CUSTNUM field in the invoice table. The Edit
Relationship window opens.

d. Click Create. A relationship line should now be drawn between the two tables.
e. Create additional relationships between
• invoice and products on PRODNUM
• products and inventory on PRODNUM
• products and manufacturers on MANUNUM

13
Statistics 440: Lab Exercises 14

4. Using the Simple Query Wizard

a. Select the Create tab, click Queries, then click the Query Wizard icon from the
toolbar. Select Simple Query Wizard from the New Query window.

b. In the Simple Query window, select


• CUSTNUM and CUSTNAME from customers
• INVNUM, INVQTY, INVPRICE and PRODNUM from invoice
• PRODNAME and PRODTYPE from products

c. Click Next.

14
Statistics 440: Lab Exercises 14

d. Make sure Detail is selected then click Next.

e. Change the query’s title to “Bought products”. Click Finish.

15
Statistics 440: Lab Exercises 14

f. Switch to Design view (make sure the Home tab is selected) by clicking the View icon,
then select Design View.

g. Change the Sort order of CUSTNUM to Ascending.

h. Save the query by clicking the Save button on the toolbar.

i. Run the query by clicking the Run button on the toolbar.

16
Statistics 440: Lab Exercises 14

5. Working with SQL statements


a. To view the SQL statement generated by the Query wizard, select ViewSQL View.

b. In the editor window that appears, modify the SQL statement so that all the rows in the
invoice table are returned by the query.

6. Using the Design View to Build a Make-Table Query

a. From the create tab, click the Query Design icon .


b. In the Show Table window, add the products and manufacturers tables.
c. In the Select Query window, select the fields PRODNUM, PRODNAME, PRODCOST,
PRODTYPE from products, and MANUNAME from manufacturers. Uncheck
the Show box under PRODTYPE and add the criteria =’Software’. This will select
the software products only.

d.

17
Statistics 440: Lab Exercises 14

e. Select the Datasheet view to preview the rows that will be returned by the query.

f. Return to Design view .

g. From the toolbar, select the Make Table Query icon . In the Make Table window,
name the new table software_prod then click OK.

h. Save the query by clicking the Save button on the toolbar. Name the query “make
software_prod”.

i. Run the query by clicking the Run button on the toolbar.

-The End-

Portions Copyright  2007 by SAS Institute Inc., Cary, NC, USA. All rights reserved.
Reproduced with permission of SAS Institute Inc., Cary, NC, USA. SAS Institute Inc. makes no
warranties with respect to these materials and disclaims all liabilities thereof.

18

You might also like