Importing Data into SAS

This tutorial will show you how to read data into SAS. It also covers how to import external data to SAS. It includes examples of importing most common formats such as CSV, Excel File and Text Files etc. After finishing this tutorial, you would be comfortable how to extract data into SAS.
import data sas
Import Data to SAS
I. Entering Data Directly in SAS Program
You can enter your lines of data directly in your SAS program by using a DATALINES statement.

Let's start out by clarifying the main keywords associated with the following program.

The keywords are as follows:
1. DATA -  The DATA step always begins with a DATA statement. The purpose of the DATA statement is to tell SAS that you are creating a new data set i.e. outdata.
2. INPUT -  To define the variables used in data set.
3. Dollar sign ($) - To identify variable as character.
4. DATALINES - To indicate that lines following DATALINES statement a real data.
5. PROC PRINT - To print out the contents of data set in output window.
6. RUN - The DATA step ends with a RUN statement.

You can also use CARDS instead of DATALINES. Both means the same. There is no difference between these two keywords. See the program below -
DATA outdata;
   INPUT age gender $ dept obs1 obs2 obs3;
1 F 3 17 6 24
proc print;

Reading Delimited Data

The default delimiter is blank. If you have a data file with other delimiters such as comma or tab you need to define the delimiter before defining the variables using INFILE and DLM = options.
Syntax : Infile 'file-description' dlm=','
  1. For tab delimiter, the syntax would be infile 'file-description' dlm='09'x
  2. For colon delimiter, the syntax would be infile 'file-description' dlm=':'


Importing External Data into SAS


PROC IMPORT is a SAS procedure to import external files into SAS. It automates importing process. You don't need to specify variable type and variable length to import an external file. It supports various formats such as excel file, csv, txt etc.

1. Importing an Excel File into SAS
The main keywords used in the following program are :
1. OUT - To specify name of a data set that SAS creates. In the program below, outdata is the data set saved in work library (temporary library)
2. DBMS - To specify the type of data to import.
3. REPLACE - To overwrite an existing SAS data set.
4. SHEET - To import a specific sheet from an excel workbook
5. GETNAMES - To include variable names from the first row of data. 

If you are using SAS University Edition
You need to find shared folder which is generally available as Folders > My Folders
In SAS University edition, file location would be
DATAFILE = "/folders/myfolders/sampledata.xls"

2. Importing a Tab-Delimited File into SAS
The program below is similar to the code of importing excel file. The only difference is DBMS = DLM and delimter = '09'x.

3. Importing a Comma-Delimited File with TXT extension

To get comma separated file with a txt extension into SAS, specify delimeter = ','

4. Importing a Comma-Delimited File with CSV extension

To get comma separated file into SAS, specify DBMS= CSV

5. Importing a Space-Delimited File

To extract a space delimited file, specify delimiter = '20'x

6. Importing a file containing multiple delimiter

If two or more delimiters, such as comma and tabs, quote them following delimiter = option

Method II : Get External File - INFILE

In SAS, there is one more method called INFILE to import an external file. It's a manual method of importing an external file as you need to specify variables and its types and length.

1. Reading a CSV File

INFILE statement - To specify path where data file is saved.
DSD - To set the default delimiter from a blank to comma.
FIRSTOBS=2 : To tell SAS that first row contains variable names and data values starts from second row.

2. Reading a TAB Delimited File

We can use DLM='09'x to tell SAS that we are going to import a tab delimited file. The TRUNCOVER statement tells SAS to assign the raw data value to the variable even if the value is shorter than expected by the INPUT statement.
data outdata;
  infile 'c:\deepanshu\dummydata.txt' DSD dlm='09'x truncover;
  input employee :$30. DOJ :mmddyy8. state :$20.;
How to handle an external file :
Using a FILENAME statement to handle an external file.
FILENAME sample 'c:\deepanshu\sampledata.csv' ;
DATA outdata;
infile sample dsd;
INPUT age gender $ dept obs1 obs2 obs3;
Spread the Word!
Related Posts
About Author:

Deepanshu founded ListenData with a simple objective - Make analytics easy to understand and follow. He has over 10 years of experience in data science. During his tenure, he has worked with global clients in various domains like Banking, Insurance, Private Equity, Telecom and Human Resource.

38 Responses to "Importing Data into SAS"

  2. Clear and Crisp explanation! You made learning SAS easy....i thought it would be very tough to learn.

    Thanks a lots! :)

  3. Thanks for providing these valuable topics

  4. This site is really awesome, to learn and i can say compare to all sites this is best and very informative, after reading basic from this site, my most of doubts are cleared,
    Thank you sir, please keep posting.

  5. This site is really awesome, to learn and i can say compare to all sites this is best and very informative, after reading basic from this site, my most of doubts are cleared,
    Thank you sir, please keep posting.

  6. Hi, can you please elaborate more about truncover and DSD in infile. Urgent!! Also, please explain with examples.

  7. Hey, brilliant job done.
    Could you please help me out with Proc import if i wish to import selected data ?

    1. You can specify RANGE= option in PROC IMPORT. For eg. RANGE="Sheet1$B2:D10"

  8. thank you for giving wedsite

  9. I follow u on quora , a big fan of urs Excellent work sir :)

    1. Thank you Abhishek. Your compliment gave me motivation to work harder and much better than before. Cheers mate!

  10. Hi Deepanshu,

    I have worked as a data analyst in an e-commerce company for 2 years. But the tool i used for analysing is Excel now i want to shift to SAS. What should i do in this career shift process. I have few more doubts in getting placed should i do certification? only then will i get a job, because i don have hands on experience in SAS i practised it by myself with the help of internet. What are the things can be done to get a real time experience and get placed soon in a good job

    Pls do reply am in a crisis kinda situation in my career


  11. Deepanshu,
    One doubt, In "Importing a Comma-Delimited File with CSV extension". The raw file extension should be .csv in infile statement. Am I right? please suggest.

    1. No, I've tried it using txt file with dlm=csv.


  12. Hi ,

    I just ran the first program and getting below error. Please help me out.

    /‌pbr/‌biconfig/‌940/‌Lev1/‌Web/‌WebAppServer/‌SASServer1_1/‌temp/‌SASStudioSessions/‌84920dae-c29e-4c52-96cc-085a498c522b/‌ (No such file or directory)

  13. sir if data contains commas in id variable and one comma in same name variable or different how to write code for dlm dsd; its getting blank in varibles
    id name gender
    101,kk f
    102, hh, m
    103, hhj m
    but in 2nd obs its getting blank in gender how to solve the query sir

  14. sir can u please show me how to get output for two variables with where statement
    where cylinders=4;
    where length=180;
    but its taking only cylinders not executing the data of length in cars dataset
    pls solve the problem sir

    1. To check for for both criteria :
      where cylinders=4 and length=180;
      This will keep only the observations having cylinders=4 and length=180

    2. With Multiple 'where' statements only the last where statement is executed.
      so its wise to use the condition with AND OR conditions.

  15. Hi sir this is clarity explanation and it is good & easy to understand.. and it is helpful in learning stage. I following the this website daily.. so thank you...

  16. hi,sir u gave us very good examples but sir,you are mentioned text files in the import statement but where are the files sir,would you provide that dummy files

  17. Thanks for your informative helps me understand sas concepts more easily...Keep posting! Thanks again

  18. The code mentioned for "importing a file with multiple delimiter" is not working. i am using SAS EG 7.1 64bit

  19. I tried to import csv file using infile option. In my csv file I have 3 dates variables. When I m importing sas shows "invalid data for date_variable1"

    Please help to resolve this

  20. excellent tutorials. where is the sample data files?? reading any articles facing the same problem.

  21. great content

  22. The content is well structured and of great help. Thank you for creating this.

  23. so mush helpful.

  24. hi i'm trying to read delimited file by import proceudre please help me with the code
    proc import
    datafile="C:\Users\Madhu\Documents\SOURCE DATA RAR FILES\SOURCE DATA\DLM-TXT\data5.txt"
    out = work.data5
    dbms =dlm
    this is the data
    101 kiran kumar 34 male Asp 05mg Asian
    102 pavan 45 male Asp 10mg African
    103 laxmi knath 56 female Asp 15mg Asian
    i tried with datastep option and it read the data values efficiently but im unable to read the values by import procedure

  25. is it tab delimited or space deliimited file?


Next → ← Prev

Looks like you are using an ad blocker!

To continue reading you need to turnoff adblocker and refresh the page. We rely on advertising to help fund our site. Please whitelist us if you enjoy our content.