If you have been using SAS for long, you have probably noticed that there is generally more than one way to do anything. (For an example, see my co-author Lora Delwiche’s blog about PROC SQL.) The Little SAS Book has long covered reading and writing Microsoft Excel files with the IMPORT and EXPORT procedures, but for the Sixth Edition, we decided it was time to add two more ways: The ODS EXCEL destination makes it easy to convert procedure results into Excel files, while the XLSX LIBNAME engine allows you to access Excel files as if they were SAS data sets.
With the XLSX LIBNAME engine, you can convert an Excel file to a SAS data set (or vice versa) if you want to, but you can also access an Excel file directly without the need for a SAS data set. This engine works for files created using any version of Microsoft Excel 2007 or later in the Windows or UNIX operating environments. You must have SAS 9.4M2 or higher and SAS/ACCESS Interface to PC Files software. A nice thing about this engine is that it works with any combination of 32-bit and 64-bit systems.
The XLSX LIBNAME engine uses the first line in your file for the variable names, scans each full column to determine the variable type (character or numeric), assigns lengths to character variables, and recognizes dates, and numeric values containing commas or dollar signs. While the XLSX LIBNAME engine does not offer many options, because you are using an Excel file like a SAS data set, you can use many standard data set options. For example, you can use the RENAME= data set option to change the names of variables, and FIRSTOBS= and OBS= to select a subset of rows.
Reading an Excel file as is
Suppose you have the following Excel file containing data about magnolia trees:
With the XLSX LIBNAME engine, SAS can read the file, without first converting it to a SAS data set. Here is a PROC PRINT that prints the data directly from the Excel file.
* Read an Excel spreadsheet using XLSX LIBNAME; LIBNAME exfiles XLSX 'c:\MyExcel\Trees.xlsx'; PROC PRINT DATA = exfiles.sheet1; TITLE 'PROC PRINT of Excel File'; RUN;
Here are the results of the PROC PRINT. Notice that the variable names were taken from the first row in the file.
PROC PRINT of Excel File
Converting an Excel file to a SAS data set
If you want to convert an Excel file to a SAS data set, you can do that too. Here is a DATA step that reads the Excel file. The RENAME= data set option changes the variable name MaxHeight to MaxHeightFeet. Then a new variable is computed which is equal to the height in meters.
* Import Excel into a SAS data set and compute height in meters; DATA magnolia; SET exfiles.sheet1 (RENAME = (MaxHeight = MaxHeightFeet)); MaxHeightMeters = ROUND(MaxHeightFeet * 0.3048); RUN;
Here is the SAS data set with the renamed and new variables:
It is just as easy to write to an Excel file as it is to read from it.
* Write a new sheet to the Excel file; DATA exfiles.trees; SET magnolia; RUN; LIBNAME exfiles CLEAR;
Here is what the Excel file looks like with the new sheet. Notice that the new tab is labeled with the name of the SAS data set TREES.
The XLSX LIBNAME engine is so flexible and easy to use that we think it’s a great addition to any SAS programmer’s skill set.