Importing Data in SAS Using PROC IMPORT

Welcome back to Mindful Data Minds! In the last session, we learned how to import flat files using the INFILE statement. Today, we’ll explore a more powerful method — the PROC IMPORT procedure. With PROC IMPORT, you can import not only flat files (like CSV or TXT) but also structured files such as Excel.

What is PROC IMPORT?

PROC IMPORT is a SAS procedure that reads external files and converts them into SAS datasets. It automatically detects column names, data types, and lengths, making it easier than manual coding.

Watch the Full Tutorial

What You Will Learn

  • How to use PROC IMPORT to read CSV, TXT, and Excel files.
  • The role of DBMS in specifying file type.
  • How to use GETNAMES to read headers.
  • How to avoid truncation with GUESSINGROWS.
  • How to import specific sheets from Excel.

Step 1: Importing a CSV File

Suppose we have a file sample.csv with supplier data (8 columns, 29 records). Here’s how to import it:

proc import datafile="C:\tutorial\CSV Sample.csv"
    out=csv_sample
    dbms=csv
    replace;
    getnames=yes;
run;

Explanation:

  • datafile= → Path to the file.
  • out= → Name of the SAS dataset to create.
  • dbms=csv → File type (CSV in this case).
  • replace → Overwrites existing dataset if it already exists.
  • getnames=yes → Uses the first row as column names.

Step 2: Handling Column Lengths

By default, SAS reads the first 20 rows to determine variable lengths. This can cause truncation if later rows have longer values.

Example: Address column limited to 24 characters → longer addresses get cut off.

Solution: GUESSINGROWS

proc import datafile="C:\tutorial\CSV Sample.csv"
    out=csv_sample1
    dbms=csv
    replace;
    guessingrows=29;
    getnames=yes;
run;

Reads all 29 rows to determine correct lengths. You can set any number or use MAX (not recommended for very large datasets).

Step 3: Importing a Tab-Delimited File

For tab-delimited files (.txt):

proc import datafile="C:\tutorial\Tab Sample.txt"
    out=tab_sample
    dbms=tab
    replace;
    getnames=yes;
run;

Result: Dataset with 8 columns and 29 rows.

Step 4: Importing an Excel File

PROC IMPORT can also handle Excel files (.xlsx):

proc import datafile="C:\tutorial\Excel Sample.xlsx"
    out=excel_sample
    dbms=xlsx
    replace;
    getnames=yes;
run;

Notes:

  • By default, SAS imports the first sheet.
  • To import another sheet, use:
sheet="Sheet2";
  • Excel files already have structured formatting, so SAS doesn’t need GUESSINGROWS.

Download Section

Next Step

Continue learning by exploring the next tutorial in this series. Also subscribe to get notified about new lessons.

Have a Question?

Drop your doubts in the comments below or contact us.

Leave a Reply

Your email address will not be published. Required fields are marked *