Reading an Excel file in SAS allows you to import spreadsheet data for analysis, reporting, or further processing within the SAS environment. This is essential when working with data initially collected or stored in Microsoft Excel format, enabling seamless integration into SAS workflows without manual re-entry.
What You'll Need
- SAS software installed (Base SAS or SAS Studio)
- An Excel file (.xls or .xlsx) containing the data you want to import
- Appropriate permissions to access the file location
- For .xlsx files, SAS 9.4M3 or later is required for native support via the XLSX engine
Step-by-Step
- Identify the full file path to your Excel file (e.g., C:\Data\sales.xlsx or /home/user/data/sales.xlsx). Ensure the path is accessible from where SAS is running.
- Determine the Excel file format: use
XLSengine for .xls files (Excel 97-2003) orXLSXengine for .xlsx files (Excel 2007 and later). - In your SAS program, use a LIBNAME statement to assign a library reference to the Excel file using the appropriate engine. For example:
LIBNAME myxl XLSX 'C:\Data\sales.xlsx';
For older .xls files, replace XLSX with XLS:LIBNAME myxl XLS 'C:\Data\sales.xls';
- After assigning the libref, SAS treats each worksheet in the Excel file as a SAS dataset. To view the contents of a specific worksheet (e.g., named 'Sheet1'), use PROC PRINT or PROC CONTENTS:
PROC PRINT DATA=myxl.'Sheet1$'N;
Note: The worksheet name must be followed by a dollar sign ($) and enclosed in quotes with the 'N' suffix to indicate a literal name, especially if it contains spaces or special characters. - To permanently import the data into a SAS dataset for reuse, use a DATA step:
DATA work.sales_data;
SET myxl.'Sheet1$'N;
RUN;
This creates a SAS dataset named sales_data in the WORK library that you can use in subsequent analyses. - When finished, clear the library assignment to free resources:
LIBNAME myxl CLEAR;
Common Mistakes
- Forgetting the dollar sign ($) and 'N' suffix when referencing worksheet names, leading to errors like 'File does not exist'.
- Using the wrong engine (e.g., XLSX for an .xls file or vice versa), which causes import failures.
- Specifying an incorrect or inaccessible file path, especially when running SAS on a server where the local client path is not available.
- Assuming worksheet names are case-insensitive; while Excel is not case-sensitive, SAS may treat them literally, so match the exact spelling.
- Not clearing the libref after use, which can cause file locking issues if the Excel file needs to be edited or moved.
Tips
- Use PROC CONTENTS on the Excel libref to list all available worksheets before importing:
PROC CONTENTS DATA=myxl._ALL_;
- If your Excel file has multiple worksheets and you want to import all of them, consider using a macro to loop through the worksheet names obtained from the dictionary tables.
- For large Excel files, consider saving the data as CSV first and importing via
PROC IMPORTorDATAstep withINFILEfor better performance. - Ensure the first row of your Excel worksheet contains column headers; SAS uses this to assign variable names by default. If not, use
GETNAMES=NOin the LIBNAME statement and manually assign names later. - When working in SAS Studio or SAS Enterprise Guide, use the 'Import Data' task as a graphical alternative, which generates the underlying code for you.
Troubleshooting
- Error: 'Physical file does not exist': Verify the file path is correct and accessible from the SAS server. Use a FILENAME statement to test access:
FILENAME test 'C:\Data\sales.xlsx';
then check the log. - Error: 'Unsupported file format': Confirm you are using the correct engine (XLS vs XLSX) and that your SAS version supports the XLSX engine (requires 9.4M3+).
- Worksheet not found: Double-check the worksheet name spelling, including spaces. Use PROC CONTENTS to list exact names.
- Data appears truncated or misaligned: Ensure there are no merged cells in the Excel range being read, as SAS cannot interpret them correctly. Unmerge cells before importing.
- Special characters or numbers imported incorrectly: Check data types in Excel; SAS may guess variable types from the first few rows. Use
MIXED=NOorSCANTEXT=YESin the LIBNAME statement to improve guessing, or import as character and convert later.
Frequently Asked Questions
Can I import a specific range of cells from an Excel worksheet?
Yes, use the RANGE= option in the LIBNAME statement. For example:
LIBNAME myxl XLSX 'C:\Data\sales.xlsx' RANGE='Sheet1$B2:D10';This imports only cells B2 to D10 from Sheet1.
What if my Excel file has a password?
SAS cannot directly open password-protected Excel files. You must remove the password or save an unprotected version before importing.
Why are my numeric values being read as character?
This often happens if the Excel column contains mixed data types (e.g., numbers and text) or if the first few rows contain text. SAS assigns the variable type based on early rows. To fix, ensure consistent data types in Excel or use GUESSINGROWS=MAX to scan more rows for type detection.
Can I read Excel files in SAS University Edition?
Yes, SAS University Edition supports the XLSX engine for .xlsx files, provided the file is placed in the shared folder that is mapped to the virtual environment.
Is it better to use LIBNAME with XLSX engine or PROC IMPORT for Excel files?
LIBNAME with XLSX engine is preferred for repeated access or when working with multiple worksheets, as it avoids re-reading the file. PROC IMPORT is simpler for one-time imports but less efficient for large or frequently accessed files.