Statistical
Software > SAS/WIN Web > Entering Data
SAS Online Tutorial
VII. Entering data into a
SAS data set
Before you can run an analysis,
write a report, or do anything with your data, SAS must be able to read
your data. In order for SAS to read your data, the data must be in a special
form called a SAS dataset. There are several ways of getting your data into
a SAS dataset:
Use of Viewtable to enter data into a SAS data set
Besides the 5 main windows
mentioned previously, there are other SAS windows available for various
tasks. The Viewtable window is an easy way to create new data sets or
browse and edit existing data sets. The Viewtable window displays tables
(another name for datasets) in a tabular format, that looks like a spreadsheet. It is NOT a spreadsheet. To open the Viewtable
window, select Table Editor from the TOOLS menu. An empty Viewtable window
will appear:
The table contains no data. Instead you see rows (or observations) labeled
with numbers and columns (or variables) labeled with letters. We will
now enter the small data set that was used in the section "Getting your
program into the Editor Window". First we will give the columns more meaningful
names. Click on the column heading for A and type the word name;
then tab to column heading B and type height and so forth. When you have
enterd the appropriate column headings (variable names), type
in the data shown in the following Viewtable window. SAS will automatically
figure out if your columns are numeric or character based on the first
row of data.
Saving your data in Work directory and giving it a name
When you have typed in your
data and are satisfied with the accuracy you need to save your data and
give it a name. To do this from the FILE menu, select Save as. You will
then see the following Save as dialog box:
From the Save As dialog box, select a library and then specify the member
name of your data (or table). Libraries are directories (folder). Members
are files. Initially, you are presented with some libraries that have been
created by SAS and are controlled by SAS: Maps, Sashelp Sasuser and Work. There may also be a Gismaps library.
The only one of these you should
select is Work. This is a temporary library or workspace that exists for
the duration of your SAS session but is erased at the end of each session.
Because the Work library is erased at the end of each session, your data
set will be temporary, i.e. good for the duration of the session
but erased at the end of the session. Later, you will learn how
to make permanent SAS datasets. For now, double-click the Work library and type
a name (test1) for your dataset in the Member Name box. Click OK. You have
now saved data into a SAS dataset whose name is Work.test1. (Note: the
title bar of the Viewtable now says "Saved As: Work.test1".) The prefix WORK always indicates a temporary SAS dataset.
Importing an Excel file
In order to do this exercise
you must enter and save the data as an excel 97 or later file with variable
names in the first row and data in the following rows as follows: (Note:
You must save and close the excel file before you can import it into SAS.)
From the File menu, select Import Data.
Note: The pictures illustrate the dialogs using SAS 8. SAS 9 dialogs look slightly different, but will walk you through the same steps.
Choose Microsoft Excel 97 or 2000(*.xls), then click Next. You will then
get the following dialog box:
Type in the excel file name and location (c:\MyDocuments\exercise.xls)
or use Browse to select the file. If an Excel file has multiple worksheets, Click Options to select the worksheet to import. Click Next.
Here you type a the name for your SAS data set (e.g. test2) in the Member field. Click
Finish. SAS will then create a temporary SAS data set called work.test2.
You should see the following note in the log window: Work.test2 was successfully
created.
Reading data from the Program Editor Window
For small datasets, or for testing programs, you may want to type or copy/paste
your data directly into the Program Editor window.
You can either type your data records directly into the Editor window
or, if they are already stored in a text file (i.e. readable, printable), open the data file with any text editor (e.g. notepad, wordpad),
and copy and paste the data lines to the SAS Program Editor.
You will then need to insert a data step with the data, input, and datalines statements to read the data in the Program Editor into a SAS dataset. The example below (used earlier in "Getting your program into the Editor window"),
creates a SAS data set called exercise with 5 observations
and 5 variables.
data exercise;
input name $ height weight jog $ tennis $;
datalines;
Robert 68 150 yes yes
George 67 180 no no
Agatha 63 110 no no
Sandy 60 125 yes yes
Bill 65 160 yes no
run;
Type or copy/paste the above program into the Editor window and submit.
You then receive the following Log window:
The Log window says that WORK.EXERCISE has been created. Whenever the
name you use on the data step specifies no prefix (library), SAS assigns it the WORK prefix, which means it is stored in the temporary library WORK.
Reading Ascii or text data from an external file
If you already have an ascii
(text) data file in an external file, it is usually better to keep it
there rather than copy/paste into the Editor Window. This eliminates the
chance that your data will accidentally be altered when you are editing
your SAS program. It also makes your program easier to view. You need a program similar to the one used to read data
from the program editor, but with an INFILE statement instead of DATALINES.
The INFILE statement tells SAS the filename and location of the external file
containing the data. The INFILE statement follows the DATA statement and
must precede the INPUT statement.
Suppose we have an ascii
data file minidata.dat on a diskette (a:\). You can get a copy of this
data from http://www-unix.oit.umass.edu/~statdata/statdata.
Click on Index to all Local Datasets and download the minidat data file. This is what the data in minidat.dat data file represents:
The first 10 lines of the minidat.dat file are as follows:
Type or copy/paste the following SAS program into the Editor
Window to read this data into a SAS dataset. (If you stored the file somewhere other than on your a:\ drive, you will need to adjust the infile statement to put in the correct drive/path.)
data minidat;
infile 'a:\minidat.dat';
input pulse1 pulse2 ran smoke sex height weight activity;
run;
proc print data=minidat(obs=10);
run;
The DATA statement begins the data step and tells SAS to name the SAS
dataset minidat. Since it does not specify a prefix for the name, the dataset will be in the temporary WORK library. The INFILE statement tell SAS where to find the ASCII
data file called minidat.dat. Unlike with SAS datasets, you must specify
the entire filename, including the file extension. The INPUT statement
gives a name, type ($ = character, otherwise numeric) and (optionally)
a location for each piece of information in the minidat.dat file. If the
location is not given, as in our example, SAS will read the data " free" format. This means that each value on a data line is separated
from the next by at least one blank, character values are eight characters
or less and any missing data values are represented by a period rather than left blank.
For the data in this example,
all variables are numeric so we dont have to use the $. (If the data for variable sex had been entered as "m"
and "f", then we would follow the name sex with a $, i.e. sex$.
This would tell SAS to expect the data as characters rather than numbers.)
The
RUN statement tells SAS to process the previous DATA step. Finally
PROC PRINT tells SAS to print the first 10 observations in the Output
window. If you now submit the above program your log and output window
should be as follows:

Home | Back | Next
|