IV. SAS data
handling - Data Step
Normally, SAS users do not pay attention to what
type of files SAS uses in a SAS session. This section distinguishes
several types of files that SAS can handle. Knowing this, you will be
able to use each type of file advantageously. The following
flow chart illustrates how SAS data sets are processed:
A data file that has been entered in the SAS Data
step after the CARDS command needs to be converted into a SAS data file
before SAS can use it. The DATA step takes care of this conversion.
Example 1:
Data;
input x y z @@;
datalines;
1 2 3 4 5 6 7 8 9
;
run;
This sample SAS program creates a temporary data
set using the DATALINES statement to read in the in-line
data. The Data statement defines a temporary data set; the
INPUT statement defines the variables and their formats; the DATALINES
statement gives instruction to start reading in the data that
follow. The RUN statement ends this session of the program
and submits for processing.
External Files
The most common type of data is sometimes referred
to as an External File, Raw File, or even a Text File. These files have
the same characteristics: they are made up of numbers and/or characters
and they can be processed by other programming languages as well as
SAS. There are two ways to incorporate this kind of file into a SAS
program. The first and the commonly used one is to put data after a
CARDS command as in the previous example. Another method is to refer to
the location of data in the SAS program. The latter method is more
efficient than the former, because it reduces the size of your SAS
program to a more manageable level, especially, when your data set has
over a thousand observations. The following SAS program shows you how
to accomplish the latter method.
Example 2:
FILENAME DATAIN 'A:\COUNTRY.DAT';
DATA COUNTRY;
INFILE DATAIN;
INPUT DEC 1 ID 2-4 NAME $CHAR26. SSCODE 31-33
CONTIN $ 34-35 DODEV 36 POPULATE 37-43
AREA 44-49 GNP 50-56 MILEXPED 57-64 .1
PEDEXPED 65-71 .1;
PROC PRINT DATA=COUNTRY;
RUN;
The FILENAME statement tells SAS to use DATAIN as
a file reference for the data set named 'COUNTRY.DAT'. The INFILE
command tells SAS to get the data file on drive A: under 'COUNTRY.DAT'.
In SAS 9 supported data files formats include
dBASE files, Lotus 1-2-3, Microsoft Excel spreadsheets and Microsoft
Access tables. Using the Import Wizard, the user will be guided to
create a data set from files in these formats. To import files, click
on File on the menu and select import as in the following:
The Import Wizard will guide you through the
importation process. Once imported successfully; you should see the
output window pop up and contain the complete data (N
= 141 observations). You can download the 'country.dat' data file
here
if you would like to import it for practice.
After reading in the data, you can check the data
under Libraries in the Explorer window. Double-click the
Libraries icon and open the Library window. The data file
just created is in the WORK library.
Exercise A: Importing a PASW/SPSS data
file (ExampleData1.sav).
First, download and save the data to your flash
drive using the link above.
Second, go to File, Import Data..., as shown in
the above screen shots; to open the Import Wizard. Under the Standard
Data Source heading select SPSS file (*.sav) from the data source drop
down menu and click the Next button. Browse to find the Example Data 1
file on your flash drive, select it, and click the Next button. Under
library select Work if it is not already selected. Under Member, simply
type EXAMPLE1 and then click the Next button. Select your flash drive
and type ExampleData1 in place of the * and notice the .sas file
extension. Click the Save button. Then click the Finish button.
Now; there are a few of ways to ensure the data
has been successfully imported. First, the Log window should display
Notes indicating the successful importation of the data (e.g. the
number of observations and variables). Second, in the Explorer window,
you can double click on the Libraries icon, then double click on the
Work icon and see the newly created Example1 file. Third, you can run a
print procedure to view the data using the following simple syntax in
the editor window:
PROC PRINT DATA=EXAMPLE1;
RUN;
Exercise B: Importing an Excel data file (ExampleData2.xls).
First, download and save the data to your flash
drive using the link above.
Second, make sure you click on the editor window
to activate it; otherwise you will not be able to get to the Import
Wizard. Go to File, Import Data..., as shown in the above screen shots;
to open the Import Wizard. Make sure Microsoft Excel Workbook is
selected and click the Next button. Browse to find the file on your
flash drive, select it, click Open, then OK. Then click on the Options
button to review the options. Given the nature of this particular
example (shown below), the default options will not need to be changed.
Click OK and Next. As a destination, choose the
library Work and type the member EXAMPLE2, then click Next. Click
Browse to find your flash drive and then type ExampleData2 in place of
the * to name the file and specify a location to save the SAS version
of it; make sure the .sas extension is present and then click Save. Now
click Finish.
Again; there are a few of ways to ensure the data
has been successfully imported. First, the Log window should display a
Note indicating the successful importation of the data. Second, in the
Explorer window, you should be able to see the Example2 file inside the
Libraries and then Work directories. Third, you can run a print
procedure to view the data using the following simple syntax in the
editor window:
PROC PRINT DATA=EXAMPLE2;
RUN;
You will be able to see the distinction between
Example 1 and Example 2 because, Example 2 contains missing values
(e.g. blank cells).
|