To import an Excel sheet into SAS with a defined header row, use PROC IMPORT with the sheet name in the DATAFILE path and let the GETNAMES=YES option tell SAS to read the first row as column names. If your headers sit below blank or title rows, add a RANGE= reference such as Sheet1$A3:F100 that starts at the real header line, or skip the first rows first in the RANGE. That one option is the difference between a clean dataset with named variables and a table where SAS calls everything F1, F2, F3.
What You'll Need
- A licensed SAS installation (SAS 9.4 on a server or SAS Studio on SAS Viya) with the SAS/ACCESS Interface to PC Files engine, which is what makes
PROC IMPORTread.xlsxfiles. - The Excel workbook saved locally, or on a path the SAS server can see. If you use SAS Studio or a remote server, the file must sit on the server, not only on your laptop.
- The exact sheet name inside the workbook. Open Excel and read the tab label; spelling and spaces matter.
- Excel closed on your machine if the server mounts the same drive, so the file is not locked while SAS reads it.
Step-by-Step
- Confirm the header row. In Excel, decide which row holds the column names. It is usually row 1, but reports exported from other systems often have a title in row 1, a blank in row 2, and headers in row 3 or 4. Write down that row number.
- Clean the header row. Delete merged cells in the header, remove blank columns between headers, and make every header unique. SAS truncates long names and will refuse duplicate variable names, appending numbers such as
Name0,Name1and warning that a duplicate was found. - Get the sheet name right. If the tab is called
Sales 2024, the range must quote it exactly. InPROC IMPORT, writeDATAFILE="C:\data\sales.xlsx"andSHEET="Sales 2024", or combine them inRANGE="'Sales 2024$'". Use double quotes around the whole path and single quotes inside for sheet names containing spaces or special characters. - Run the basic import with GETNAMES. The minimal working code for a header on row 1 is:
proc import datafile="C:\data\sales.xlsx" out=work.sales dbms=xlsx replace; sheet="Sales 2024"; getnames=yes; run;DBMS=XLSXhandles modern workbooks; useDBMS=XLSonly for legacy.xlsfiles.REPLACEoverwrites the output table if it already exists, andOUT=is the SAS library and table name you are creating. - Define the header row when it is not row 1. Add
RANGE=with an absolute cell range whose first row is the header row. For headers in row 4 and data through row 500 in columns A to F, writerange="Sheet1$A4:F500"(substitute your sheet name). SAS begins reading at A4, uses that row for names becauseGETNAMES=YES, and reads the rows beneath as observations. - Leave out the upper bound if you are unsure of the last row. A form such as
range="Sheet1$A4:F"lets SAS read down to the last populated cell, which avoids cutting data off when someone adds rows. - Check the log first, then the data. Read the log for notes about truncated variable names, variables converted to character because of mixed types, and the number of rows read. Then run
proc contents data=work.sales; run;to confirm the variable list and types, andproc print data=work.sales(obs=10); run;to see the first rows. - Make the names permanent and safe. If SAS generated names you dislike, use
options validvarname=v7;before the import, then rename variables in aDATAstep withRENAME=.PROC IMPORThas no option to supply your own variable names directly, so renaming afterwards is the reliable route. LABEL statements can carry the original Excel text if it is too long for a SAS name. - Save the code once it works.
PROC IMPORTguesses column types and lengths from the data it reads. For repeatable work, run the import once, then copy theDATAstep that SAS writes to the log (generated by the import engine) into your program with your own lengths and informats. This stops a column of mostly numbers with one stray text value from being read as character next month.
Common Mistakes
- Forgetting GETNAMES=YES. Without it, the header row becomes the first observation and columns are named
F1,F2and so on. If you see your header text in the data, this is why. - Pointing RANGE at the title row. Setting the range to
A1:F500when headers are on row 4 makes SAS treat the title as a column name and the real headers as data. - Forgetting the
$in the range. The sheet reference needs a dollar sign before the cell coordinates:Sheet1$A1:F100, notSheet1A1:F100. - Leaving a space in a sheet name unquoted. Sheet names with spaces must be wrapped in quotes inside
RANGE, for examplerange="'Sales Data$A1:F100'". - Assuming the server can see your local path. A
C:\path that works in Excel on your PC is meaningless to a remote SAS server. Use a network or server path, or upload the file through SAS Studio first. - Trusting guessed types. Excel cells are not typed like database columns. A phone number column may lose leading zeros, and dates may arrive as numbers if the Excel cells were formatted as General.
- Duplicate or blank headers. SAS keeps the first and renames later duplicates with numeric suffixes, and blank headers get generic names. Fix them in Excel.
Tips
- Always add
REPLACEwhile developing, otherwise a second run fails because the output table already exists. - Set
options validvarname=any;if headers contain spaces; SAS then creates names like'Order Value'n, which you must quote everywhere, so v7 with clean headers is usually easier. - Use
MIXED=YESon thePROC IMPORTstatement if a column holds both numbers and text and you want text preserved rather than the column being treated as numeric with errors. - Keep the import in a named library, such as
out=mylib.sales, so the table survives the session and can be inspected in the library viewer. - Put
PROC IMPORTcode in a saved program, not in point-and-click menus, so the header row and range are documented and repeatable. - Test with a query using
OBS=orDATASTEPsubsets before loading the full workbook if the file is large.
Troubleshooting
- "File does not exist" or a path error. Check the path from the SAS session's point of view, not from your desktop. Upload the file or use a full network path the server can reach.
- "Sheet not found". Verify the tab name, including trailing spaces, and whether the range quotes it properly. Sheet names are case-sensitive in some engines, so match the label exactly.
- Headers appear as data. Add
getnames=yes;and make sure theRANGEstarts on the header row. - Data starts one row too early. The range is one row above the header; move the start cell down.
- Leading zeros or dates are wrong. Read the column as text, or use
GUESSINGROWS=with a higher value (for exampleguessingrows=maxon supported engines) so SAS scans all rows before choosing a type. If types still misbehave, write your ownDATAstep with explicit informats. - Only part of the data loaded. Your range had a fixed last row that is now too low, or blank rows inside the data ended the read. Extend or drop the upper bound.
- Variable names are truncated. V7 names are limited to 32 characters; use
validvarname=anyor rename in aDATAstep.
Frequently Asked Questions
Can I name the columns myself in PROC IMPORT?
Not directly. PROC IMPORT takes names from the header row. Import, then rename with a DATA step and RENAME=.
What does GETNAMES=NO do?
It reads the first row as data and generates names such as F1, F2. Use it only when the file has no headers at all.
How do I skip rows after the headers?
Set the range to end before those rows, or use DATA step logic to drop them after import, for example with WHERE on a key column.
Why did SAS change my numeric column to character?
Because at least one cell in the scanned rows held text. Raise GUESSINGROWS= or import the column explicitly as numeric with an informat.
Does this work with .xlsb files?
Not with the XLSX engine. Save the workbook as .xlsx in Excel first.
Where can I check the generated code?
SAS writes the DATA step it used to the log. Copy it into your program and edit the lengths and informats for a stable import.