Import Data into SAS

Methods to Import data into SAS

  • Post author:
  • Post category:Base SAS
  • Post comments:0 Comments
  • Reading time:6 mins read

Whether you’re a data analyst or a beginner in the field of data manipulation, importing data into SAS is a fundamental concept you must master. In this post, we’ll discuss the various methods of importing data into SAS, including the INFILE statement, Proc Import, and Filename statements.

SAS can read data from almost any source. The Common data sources can be raw text files, Microsoft Office Excel spreadsheets, Access databases, and some popular database systems such as DB2 and Oracle.

Refer to the Supported Databases for Data Storage page for SAS’s complete list of supported databases.

Methods to import data into SAS

  • Using the infile statement.
  • Proc import
  • Filename statements

Infile Statement

A link between the program and the desired file is required to access a file from any programming language. Therefore, the INFILE statement is generally used in SAS to link to raw files.

INFILE also works with other SAS statements such as FILENAMEDATALINESPUT and INPUT to provide extensive data input and output in the DATA step.

INFILE Syntax:

INFILE file-specification ;
INFILE DBMS-specifications;

Arguments

File specification identifies the source of the input data records, an external file or in-stream data.

Options: You can use many options on the infile statement.

Using the INFILE/FILE statement

Because the INFILE statement identifies the file to read, it must execute before the INPUT statement reads the input data records.

Because the INFILE statement identifies the file to read, it must execute before the INPUT statement reads the input data records.

Example:

We have a text file with data on vegetable seeds. Each line of the file contains the following pieces of information (separated by spaces):

vegetable Name, vegetable code, days to germination and number of seeds.

In SAS, each piece of information is called a variable (column in other systems). A few sample lines from the file are shown below:

Cucumber 50104-A 55 30 195 
Cucumber 51789-A 56 30 225 
Carrot 50179-A 68 1500 395 

Each data line produces what SAS calls an observation (also referred to as a row in other systems).

data veg;
infile "c:\books\learning\veggies.txt";
input Name $ Code $ Days Number Price;
run;
Methods to Import data into SAS

The INPUT statement is one of SAS’s methods for reading raw data.

The default data delimiter for SAS is blank. However, SAS can also read data separated by any other delimiter (commas, tabs) with a minor change to the INFILE statement.

When you use the list input method for reading data, you only need to list the names you want to give each data value. SAS calls these variable names.

The INFILE statement tells SAS where to find the data, and the INPUT statement contains the variable names you want to associate with each data value.

The dollar sign($) following variable names tells SAS that values for Vegetable Name and code are character values.

Finally, the DATA step ends with a RUN statement.

Importing data into SAS using Proc Import

You can use the Proc import procedure to read Excel data in SAS.

Syntax:

PROC IMPORT DATAFILE=filename OUT=sas-dataset DBMS=data-source-identifier REPLACE;
SHEET=sheet-name GETNAMES=Yes/No DATAROW=n;
RANGE=range-values 
RUN;

So, these are the ways to import data in SAS. We hope that you have found it helpful.

You may also want to see our detailed guide on Importing data using Proc Import.

Moreover, if you have any other suggestions regarding other import methods, suggest us below in the comment section. We will take those lists in our future blog post.

Thanks for reading!

Every week we'll send you SAS tips and in-depth tutorials

JOIN OUR COMMUNITY OF SAS Programmers!

Subhro

Subhro provides valuable and informative content on SAS, offering a comprehensive understanding of SAS concepts. We have been creating SAS tutorials since 2019, and 9to5sas has become one of the leading free SAS resources available on the internet.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.