Importing Data into SAS

This tutorial will show you how to input 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 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;
   CARDS; 
1 F 3 17 6 24
;
proc print;
run;

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

Method I : PROC IMPORT

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. 



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.;
run;
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;
run;

SAS Tutorials : 100 Free SAS Tutorials

Get Free Email Updates :
*Please confirm your email address by clicking on the link sent to your Email*

Related Posts:

20 Responses to "Importing Data into SAS"

  1. KEEP POSTING AS YOUR ARTICLES REALLY HELP TO GET GROUNDING ON BASICS.

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

    Thanks a lots! :)

    ReplyDelete
    Replies
    1. Thank you for your lovely words. Cheers!

      Delete
  3. Thanks for providing these valuable topics

    ReplyDelete
  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.

    ReplyDelete
    Replies
    1. Thank you for your lovely words. Cheers!

      Delete
  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.

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

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

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

      Delete
  8. Replies
    1. Thank you for stopping by my website. Cheers!

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

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

      Delete
  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

    Thanks,
    Vivek

    ReplyDelete
  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.

    ReplyDelete

  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/‌74e55a26-d843-46e2-a137-40f76f12a6fc.sas (No such file or directory)


    ReplyDelete

Next → ← Prev