1
Paper SA-09-2015
Importing multiple spreadsheets in an Excel Workbook: An introduction to Macros
Nathan Becker, Pearson Vue, Bloomington, MN
William Muntean, Pearson Vue, Chicago, IL
Abstract
The SAS
®
import procedure makes importing Microsoft Excel
®
files quite simple. However, this procedure can only
import a single spreadsheet. Importing more than one sheet from a larger Excel workbook requires using the
procedure multiple times and knowing the name of each spreadsheet. Furthermore, handling Excel files that vary in
spreadsheet naming conventions necessitates changing SAS code every time encountering a new naming scheme.
This is undesirable, especially for repetitive reports. The following paper provides a solution to importing multiple
Excel spreadsheets without knowing their names. The concept of macros is introduced and demonstrates the power
of generalizable code by being reusable.
INTRODUCTION
Although there are many ways to get Excel files into SAS, the steps below allow getting Excel files into SAS with
limited knowledge of the Excel sheets or columns. A search of the internet will return several results on using the
SAS Import function. Inspecting Excel sheet names using PROC CONTENTS is one useful method of obtaining
sheet names (see Babcock, 2010). The method presented here inspects the sheet names and puts them into a data
set so that a user can iterative through the sheets and perform functions on them. To accomplish this, we introduce
the concept of SAS macros, which are tantamount to reusable functions. This removes the need to modify the SAS
code when importing different Excel files. The paper first covers the steps required to import sheets of an Excel
spreadsheet into SAS and then concludes by wrapping up the SAS code within a macro statement.
Step 1 – Create Library Name
The first step is to create a macro variable and library name. When a variable is used several times throughout a SAS
program, a macro variable makes it convenient to change the value without modifying a substantial amount of code.
When importing different Excel files, the path to the file will change. Therefore, we set a macro variable to the path at
the beginning of our program:
%Let File = ‘C:\ExcelFile.xlsx';
The %LET statement creates the macro variable, which is called by adding preceding the variable name with a “&”
(see below). A LIBNAME statement is a convenient method of referring to a storage location, which can take on many
forms. In this example, we set a library name for an Excel file using the following code.
LIBNAME XDATA &File;
Step 2 – Get Excel Sheet Names
After setting up a library name, the PROC CONTENTS inspects the Excel file and returns the names of the sheets.
To accomplish this, we use the _ALL_ keyword on our library, which allows PROC CONTENTS to inspect the entire
library. For an Excel library, this argument returns information on every spreadsheet contained therein. PROC
CONTENTS provides a thorough set of information about a SAS data set. However, we are only interested in
retaining the sheet names. The OUT statement in conjunction with the KEEP statement accomplishes this; they
create a data set called Sheets that contains the sheet names of the Excel file. Once in a data set, we can loop
through the sheet names and import each one.
PROC CONTENTS DATA=XDATA._ALL_ NOPRINT OUT=Sheets (KEEP=Memname);
RUN;
Step 3 – Cleaning up the data set
This step cleans up the Sheets data set created in the previous section. Because PROC CONTENTS returns a
record for every column in a spreadsheet, there is potential for duplicate sheet names in the data set. The SORT