Reading Data Cases using Infile, Cards, or Set Keywords

Overview

This page describes how to access data in an external file, an existing SAS dataset, or directly from the SAS program itself.

Using the Cards keyword

The cards keyword is used when you have your data integrated in your SAS program as part of the data statement. To use this method follow the following steps:
  1. After you have completed your input statement enter the line:
    cards; (note: semicolon)
  2. Enter each case of data accordingly to your input statement.
  3. When finished, place a semicolon at the beginning of a line.

Normally, this method is not done often because it can make the initial SAS program more complicated. However, for a small number of cases this method is perfectly acceptable.

Using the Infile keyword

The infile keyword is used to read data that exists in an external file. This method is similar to the cards keyword shown above. The infile statement proceeds the input statement in a SAS program. The form of the infile statement is:

infile '~/mydata.dat';

The statement above reads the file mydata.dat as if it was placed directly into your SAS program. The tilde character "~" is a Unix terminology for specifying your default login directory. Thus the command above would find the file mydata.dat which is located in your default login directory.

The sas command FILENAME allows a program to associate a sas internal name with an external file. The format of the FILENAME command is:
FILENAME data1 '~/exam1.dat';

You can now use the internal-name with the infile keyword. For the example above you would then read this data using:
infile data1;

One interesting use of the filename keyword is to couple it with the power of the Unix. For example, adding the PIPE keyword to the filename command you can do this:

FILENAME indata PIPE 'unix-command';

The above FILENAME command associates a input file to the output of running the unix command(s) in the quotes. In this example, when the data file is referenced, the unix commands inside the quotes will be executed and what normally is written to standard output (your terminal) will become the sas data file for this run. One use of this commmand is that you can write a program will analyze a data base and retrieve data based on some criteria (such as todays date), the data retrieved that day will then be loaded into sas to be analyzed as the data for a program.

Using the Set Keyword

The set keyword causes SAS to read a permanent SAS dataset into the data statement. When using the set keyword you do not need to specify the input keyword. With each permanent SAS dataset, SAS records both the variable names and the corresponding data values; thus you don't need to specify variable names since they are already defined.

To use the set keyword, you must first specify which directory the existing SAS dataset is in through the libname command. The LIBNAME command in sas specifies a directory where SAS dataset (SSD) files will be stored or accessed. It is possible to have multiple LIBNAME commands in the same sas program. The format of the LIBNAME command is the following:

LIBNAME libref 'external-directory-file' ;

where libref is a one to eight character name to be used by sas, and external-directory is the name of the directory to find the SAS dataset files. As an example, to access a SSD file named data1.ssd residing in the directory '~/sasfiles' , it would be necessary to do the following:

LIBNAME ssddir '~/sasfiles';
data test;
set ssddir.data1;

One reason to use external SAS datasets is that you can easily manipulate these datasets by adding or transforming variables to the dataset. Not having to work directly with the raw data file.


Author - Jack Suess
UMBC University Computing Services
Created - 1/15/96