📥 PROC IMPORT in SAS: A Complete Guide to Importing External Data Files

One of the most common tasks in data analysis is importing external files into your SAS environment. Fortunately, SAS provides a powerful and flexible procedure called PROC IMPORT that allows you to bring in data from Excel, CSV, and text files with ease.

In this blog post, we'll explore everything you need to know about PROC IMPORT in SAS, including syntax, file types, options, practical examples, and visual aids to make your learning easier.


🔍 What is PROC IMPORT?

PROC IMPORT is a SAS procedure that allows you to read data from various file types—most commonly:
  • Microsoft Excel files (.xls, .xlsx)
  • Comma-separated values files (.csv)
  • Tab-delimited or plain text files (.txt)

The IMPORT procedure will, by default, get variable names from the first line in your data file. If you do not want this, then add the GETNAMES=NO statement after the PROC IMPORT statement. PROC IMPORT will assign the variables the names VAR1, VAR2, VAR3, and so on. Also if your data file is type DLM, PROC IMPORT assumes that the delimiter is a space. If you have a different delimiter, then specify it in the DELIMITER= statement. The following shows both these statements: 

🧾 Basic Syntax

proc import datafile="path-to-your-file"
out=work.output_dataset dbms=file_type replace; getnames=yes; run;

✅ Parameters Explained:

OptionDescription
datafile=Path to the input file
out=Name of the output SAS dataset
dbms=Specifies the file type (CSV, XLSX, etc.)
replaceOverwrites the existing dataset if it exists
getnames=YES to use first row as variable names; NO to assign default names


Examples

Here is a Sample Data for a Superstore: 
sas dataset download

Sample Superstore Data



Lets Try to import this in a SUPERSTORE Dataset

PROC IMPORT DATAFILE='/home/../New Folder/Sample - Superstore.xls'

DBMS=XLS

OUT=WORK.SUPERSTORE;

GETNAMES=YES;

RUN;


LOG: 

1 OPTIONS NONOTES NOSTIMER NOSOURCE NOSYNTAXCHECK;
70
71 PROC IMPORT DATAFILE='/home/../New Folder/Sample - Superstore.xls'
72 DBMS=XLS
73 OUT=WORK.SUPERSTORE;
74 GETNAMES=YES;
75 RUN;
NOTE: One or more variables were converted because the data type is not supported by the V9 engine. For more details, run with
options MSGLEVEL=I.
NOTE: The import data set has 9994 observations and 21 variables.
NOTE: WORK.SUPERSTORE data set was successfully created.
NOTE: PROCEDURE IMPORT used (Total process time):
real time 0.05 seconds
user cpu time 0.04 seconds
system cpu time 0.01 seconds
memory 7090.71k
OS Memory 35640.00k
Timestamp 12/19/2020 08:52:35 PM
Step Count 24 Switch Count 2
Page Faults 0
Page Reclaims 1831
Page Swaps 0
Voluntary Context Switches 21
Involuntary Context Switches 0
Block Input Operations 6536
Block Output Operations 7432

OUTPUT:

proc import in SAS

You can Also Specify the Range of your Data you want to Import from Excel,

Example-

PROC IMPORT DATAFILE="filename" | TABLE="tablename" OUT=<libref.>SAS-data-set <(SAS-data-set-options)> <DBMS=identifier><REPLACE> ;
RANGE="sheet1$A1:C20";
sheet="Sheet1"
GETNAMES=yes;
run;

If you Specify Getnames=no; in Proc Import than PROC IMPORT will not consider your First row as Variable Name, It will treat First Row as Actual Data.

PROC IMPORT DATAFILE='/home/../New Folder/Sample - Superstore.xls'
DBMS=XLS
OUT=WORK.SUPER_NOCOL;
GETNAMES=NO;
RANGE="ORDERS$A1:B14";
RUN;

LOG:

1 OPTIONS NONOTES NOSTIMER NOSOURCE NOSYNTAXCHECK;
70
71 PROC IMPORT DATAFILE='/home/../New Folder/Sample - Superstore.xls'
72 DBMS=XLS
73 OUT=WORK.SUPER_NOCOL;
74 GETNAMES=NO;
75 RANGE="ORDERS$A1:B14";
76 RUN;
NOTE: One or more variables were converted because the data type is not supported by the V9 engine. For more details, run with
options MSGLEVEL=I.
NOTE: The import data set has 14 observations and 2 variables.
NOTE: WORK.SUPER_NOCOL data set was successfully created.
NOTE: PROCEDURE IMPORT used (Total process time):
real time 0.01 seconds
user cpu time 0.00 seconds
system cpu time 0.01 seconds
memory 5722.62k
OS Memory 32824.00k
Timestamp 12/19/2020 09:09:50 PM
Step Count 30 Switch Count 2
Page Faults 0
Page Reclaims 1416
Page Swaps 0
Voluntary Context Switches 14
Involuntary Context Switches 0
Block Input Operations 0
Block Output Operations 264

OUTPUT:

Proc Import Getnames No

⚙️ Additional Options

SHEET=

When working with Excel files, you can specify the worksheet to import:

proc import datafile="C:\data\sales.xlsx"

    out=work.sales_data

    dbms=xlsx

    replace;

    sheet="Q1_Sales";

run;

GUESSINGROWS=

This option increases the number of rows SAS scans to determine variable types (default is 20).

proc import datafile="C:\data\longdata.csv"

    out=work.long_data

    dbms=csv

    replace;

    guessingrows=1000;

run;

🚫 Common Errors and Fixes

Error MessageSolution
File not foundCheck file path and permissions
Worksheet not foundEnsure the correct sheet name
Data truncated or misreadUse guessingrows= for better type detection
Invalid file formatVerify the correct dbms= type is used

📌 Tips for Using PROC IMPORT

Always verify the imported dataset using PROC CONTENTS:

proc contents data=work.sales_data;
run;

Use a LIBNAME for Excel if you need to import multiple sheets.
Prefer DATA step for cleaner control on structure if importing repeated formats.

🧠 When to Use PROC IMPORT vs DATA step

Use CaseRecommended Approach
One-time simple importPROC IMPORT
Repetitive/automated loadsDATA step with INFILE or LIBNAME
Needs transformation while importingDATA step preferred

📌 Summary

PROC IMPORT is a fast and efficient way to bring external data into SAS, especially when working with Excel or CSV files. By mastering its options and structure, you can simplify the data preparation process and save time for analysis.

🏁 Final Thoughts

Whether you're a beginner or an experienced analyst, PROC IMPORT is one of those SAS procedures that you'll use repeatedly. Mastering it gives you more control over your data and streamlines your workflow.


Post a Comment

0 Comments