Reading Microsoft Excel Data with the XLSX Engine

Running SAS with Microsoft Excel

The examples in this section are based on SAS 9.4 64-bit running with Microsoft Office 2016 64-bit on Microsoft Windows 64-bit.
This configuration does not require the SAS/ACCESS PC Files Server. If SAS runs in a UNIX environment and needs to access Excel files on Microsoft Windows, you must license the SAS/ACCESS PC Files Server.

Steps for Reading Excel Data

To read the Excel workbook file, SAS must receive the following information in the DATA step:
  • a libref to reference the Excel workbook to be read
  • the name of the Excel worksheet that is to be read
The table below outlines the basic statements that are used in a program that reads Excel data and creates a SAS data set from an Excel worksheet. The PROC CONTENTS and PROC PRINT statements are not requirements for reading Excel data and creating a SAS data set. However, these statements are useful for confirming that your Excel data has successfully been read into SAS.
Table 4.1 Basic Steps for Reading Excel Data into a SAS Data Set
Task
Statement
Example
Reference an Excel workbook file
SAS/ACCESS LIBNAME statement
LIBNAME cert
libname cert xlsx
'C:UsersStudent1certexercise.xlsx';
Write out the contents of the SAS Library
PROC CONTENTS
proc contents data=cert._all_;
Execute the PROC CONTENTS statement
RUN statement
run;
Name and create a new SAS data set
DATA statement
data work.stress;
Read in an Excel worksheet (as the input data for the new SAS data set)
SET statement
set cert.ActLevel;
Execute the DATA step
RUN statement
run;
View the contents of a particular data set
PROC PRINT
proc print data=stress;
Execute the PROC PRINT statement
RUN statement
run;
Here is the syntax for assigning a libref to an Excel workbook.

The LIBNAME Statement

To assign a libref to a database, use the LIBNAME statement. The SAS/ACCESS LIBNAME statement associates a SAS libref with a database, schema, server, or a group of tables and views.
Syntax, SAS/ACCESS LIBNAME statement:
LIBNAME <libref>XLSX <'physical-path-and-filename.xlsx'><options>;
  • libref is a name that you associate with an Excel workbook.
  • XLSX is the SAS LIBNAME engine name for an XLSX file format. The SAS/ACCESS LIBNAME statement associates a libref with an XLSX engine that supports the connections to Microsoft Excel 2007, 2010, and later files.
    Important: The engine name XLSX is required.
    When reading XLSX data, the XLSX engine reads mixed data (columns containing numeric and character values) and converts it to character data values.
    The XLSX engine allows sequential reading of data only. It does not support random access. Therefore, it does not support certain tasks that require random access such as the RANK procedure, which requires the reading of rows in a random order.
  • 'physical-path-and-filename.xlsx' is the physical location of the Excel workbook.
Example:
libname results XLSX 'C:UsersStudent1certexercise.xlsx';
Note: The XLSX engine requires quotation marks for physical-path-and-filename.xlsx.

Referencing an Excel Workbook

Overview

This example uses data similar to the scenario used for the raw data in the previous section. The data shows the readings from exercise stress tests that have been performed on patients at a health clinic.
The stress test data is located in an Excel workbook named exercise.xlsx (shown below), which is stored in the location C:UsersStudent1cert.
Figure 4.3 Excel Workbook
Excel Workbook
In the sample worksheet above, the date column is defined in Excel as dates. If you right-click the cells and select Format Cells, the cells have a category of Date. SAS reads this data just as it is stored in Excel. If the date had been stored as text in Excel, then SAS would have read it as a character string.
To read in this workbook, create a libref to point to the workbook's location:
libname certxl XLSX 'C:UsersStudent1certexercise.xlsx';
The SAS/ACCESS LIBNAME statement creates the libref Certxl, which points to the Excel workbook exercise.xlsx. The workbook contains two worksheets, Tests and Adv, which are now available in the new SAS library (Results) as data sets.

Referencing an Excel Workbook in a DATA Step

SET Statement

Use the SET statement to indicate which worksheet in the Excel file you want to read.
data work.stress;
   set certxl.ActivityLevels;
run;
In this example, the DATA statement tells SAS to name the new data set, Stress, and store it in the temporary library Work. The SET statement specifies the libref (the reference to the Excel file) and the worksheet name as the input data.
You can use several statements in the DATA step to subset your data as needed. Here, the WHERE statement is used with a variable to include only those participants whose activity level is HIGH.
data work.stress;
   set certxl.ActivityLevels;
   where ActLevel='HIGH';
run;
The figure below shows the output for this DATA step in table format.
Figure 4.4 DATA Step Output
DATA Step Output

Name Literals

The SAS/ACCESS LIBNAME statement created a permanent library, Certxl, which is the libref for the workbook file and its location. The new library contains two SAS data sets, which access the data from the Excel worksheets.
Name literals are required with the XLSX engine only when the worksheet name contains a special character or spaces. By default, SAS does not allow special characters in SAS data set names. A SAS name literal is a name token that is expressed as a string within quotation marks, followed by the uppercase or lowercase letter n. The name literal tells SAS to allow the special character ($) in the data set name.
The following example illustrates reading an Excel worksheet using a name literal. Specify the name of the worksheet in quotation marks with an n following the name. This syntax tells SAS that there are special characters or spaces in the data set name.
libname certxl xlsx 'C:UsersStudent1certstock.xlsx';
data work.bstock;
  set certxl.'boots stock'n;
run;

Printing an Excel Worksheet as a SAS Data Set

After using the DATA step to read in the Excel data and create a SAS data set, you can use PROC PRINT to produce a report that displays the data set values. In the following example, the PROC PRINT statement displays all the data values for the new data set, Work.Bstock.
libname certxl xlsx 'C:UsersStudent1certstock.xlsx';
data work.bstock;
  set certxl.'boots stock'n;
run;
proc print data=work.bstock;
run;
Output 4.10 PROC PRINT Output of Work.Bstock
PROC PRINT Output of Work.Bstock
In the following example, the PROC PRINT statement refers to the worksheet Boot Sales and prints the contents of the Excel worksheet that was referenced by the SAS/ACCESS LIBNAME statement.
libname certxl xlsx 'C:UsersStudent1certstock.xlsx';
proc print data=cerxl.'boots stock'n;
run;
Output 4.11 PROC PRINT Output Using Name Literals
PROC PRINT Output Using Name Literals
Last updated: August 23, 2018
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.125.51