Import data into SAS3 min read

Importing data into SAS is one of the basic concepts you need to know in order to work with data manipulation or analytics.

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

Methods to import data into SAS

There are 3 popular methods for importing data into SAS.

  • Using the infile statement.
  • Filename statements

Infile Statement

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

INFILE also work with other SAS statements such as FILENAME, DATALINES, PUT 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, which is an external file or in-stream data.

Options: There are a large number of options that you can use 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 that 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 

In this example, each line of data 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;
Import data into SAS 3
title "List of Vegetables";
proc print data=veg;
run;

The INPUT statement shown here is one of the methods that SAS has for reading raw data.

The default data delimiter for SAS is the blank. SAS can also read data separated by any other delimiter (for example, 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 as 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

To read excel data in SAS you can use the Proc import procedure.

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;

Read PROC IMPORT Procedure in detail.

Leave a Comment

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

Share via
Copy link