|
|
|
||||
|
|
|||||
This module will illustrate how to read and write SAS system files.
A SAS system file is a data file that, in addition to having the information contained in a raw data file, contains variable labels, values labels, and other formatting that a raw data (ASCII) file cannot contain. One advantage of creating a SAS system file is that for large data sets, SAS can read a SAS system data file faster than it can read instream data or data from an external file. Not every situation, however, is suited for creating a SAS system file. For small datasets, SAS syntax for reading data instream may be more appropriate. However, if you have a large dataset that you'd like to access without having to re-read and re-format the data each time you run an analysis, then you'll probably want to create a SAS system file. It should be noted that SAS system files can only be read by the SAS system and are platform dependent - that is, a SAS system file created on a Macintosh CANNOT be read by a DOS (Windows) version of SAS (and vice versa).
Reading or writing a SAS system file requires knowledge of two essential features of the SAS system - the libname statement and the data step. This module will assume that the reader understands the basic ideas behind these two concepts.
In order to read a SAS system file, the location of the SAS system file must first be designated with a libname statement. For example, if you are working on a DOS machine (Windows) and you want to read the SAS system file cars.sd2 from the directory c:\carsdata, and then print a summary of the contents of the file, as well as the first five observations in the file, use the following syntax.
LIBNAME in "c:\carsdata"; PROC CONTENTS DATA=in.cars; RUN; PROC PRINT DATA=in.cars(obs=5); RUN;
The UNIX version of this, assuming that the file cars.ssd01 is located in the directory ~/carsdata, would use the following syntax:
LIBNAME in "~/carsdata"; PROC CONTENTS DATA=in.cars; RUN; PROC PRINT DATA=in.cars(obs=5); RUN;
Note that the "~" in the UNIX pathname above refers to the user's HOME directory. Hence, this example assumes that a directory called /carsdata is located in the users HOME directory.
The Macintosh version of this, assuming that the file cars.sd2 is located in a folder named carsdata, which is in turn located on a hard drive named Hard Drive, would use the following syntax:
LIBNAME in "Hard Drive:carsdata"; PROC CONTENTS DATA=in.cars; RUN; PROC PRINT DATA=in.cars(obs=5); RUN;
Note that the only difference in the examples above (for DOS, UNIX and Macintosh) occurs in the directory locations that are in quotes in the libname statement. In general, SAS syntax remains the same across platforms.
Here is the output produced by the proc contents and proc print statements above.
Data Set Name: IN.CARS Observations: 5
Member Type: DATA Variables: 5
Engine: V612 Indexes: 0
Created: 13:35 Monday, July 26, 1999 Observation Length: 36
Last Modified: 13:35 Monday, July 26, 1999 Deleted Observations: 0
Protection: Compressed: NO
Data Set Type: Sorted: NO
Label:
-----Engine/Host Dependent Information-----
Data Set Page Size: 8192
Number of Data Set Pages: 1
File Format: 607
First Data Page: 1
Max Obs per Page: 226
Obs in First Data Page: 5
-----Alphabetic List of Variables and Attributes-----
# Variable Type Len Pos
-----------------------------------
1 MAKE Char 5 0
2 MODEL Char 7 5
3 MPG Num 8 12
5 PRICE Num 8 28
4 WEIGHT Num 8 20
OBS MAKE MODEL MPG WEIGHT PRICE
1 AMC Concord 22 2930 4099
2 AMC Pacer 17 3350 4749
3 AMC Spirit 22 2640 3799
4 Buick Century 20 3250 4816
5 Buick Electra 15 4080 7827
For the remainder of this module, examples will be presented using DOS directories and pathnames. However, these examples will generalize to UNIX and Macintosh users, simply by using the pathname conventions illustrated above.
In order to write (save) a SAS system file from a raw data file, a libname statement and a data step are required. For example, let's assume that you have the following raw data file called cars1.dat.
AMC Concord2229304099 AMC Pacer 1733504749 AMC Spirit 2226403799 BuickCentury2032504816 BuickElectra1540807827
If you'd like to read this raw data file and write (save) the file out as a SAS system file named cars2.sd2 to the directory c:\carsdata, use the following syntax.
LIBNAME out "c:\carsdata"; DATA out.cars2; INFILE "c:\sas\cars1.dat"; INPUT make $ 1-5 model $ 6-12 mpg 13-14 weight 15-18 price 19-22; RUN; PROC CONTENTS DATA=out.cars2; RUN; PROC PRINT DATA=out.cars2(obs=5); RUN;
Here is the output produced by the proc contents and proc print statements above.
Data Set Name: OUT.CARS2 Observations: 5
Member Type: DATA Variables: 5
Engine: V612 Indexes: 0
Created: 14:18 Monday, July 26, 1999 Observation Length: 36
Last Modified: 14:18 Monday, July 26, 1999 Deleted Observations: 0
Protection: Compressed: NO
Data Set Type: Sorted: NO
Label:
-----Engine/Host Dependent Information-----
Data Set Page Size: 8192
Number of Data Set Pages: 1
File Format: 607
First Data Page: 1
Max Obs per Page: 226
Obs in First Data Page: 5
-----Alphabetic List of Variables and Attributes-----
# Variable Type Len Pos
-----------------------------------
1 MAKE Char 5 0
2 MODEL Char 7 5
3 MPG Num 8 12
5 PRICE Num 8 28
4 WEIGHT Num 8 20
OBS MAKE MODEL MPG WEIGHT PRICE
1 AMC Concord 22 2930 4099
2 AMC Pacer 17 3350 4749
3 AMC Spirit 22 2640 3799
4 Buick Century 20 3250 4816
5 Buick Electra 15 4080 7827
In order to write (save) a SAS system file from an existing SAS system file, a libname statement and a data step are required. For example, if you want to: (1) read the existing SAS system file cars2.sd2 from the directory c:\carsdata, (2) create a new variable called pricempg (which is equal to the price of each car for every mile per gallon it gets), and (3) write (save) the file as a new SAS system file called cars3.sd2 to the directory c:\carsdata, use the following syntax:
LIBNAME disk "c:\carsdata";
DATA disk.cars3;
SET disk.cars2;
pricempg=price/mpg;
RUN;
PROC PRINT DATA=disk.cars3(obs=5);
RUN;
Here is the output produced by the proc print statement above.
Obs make model mpg weight price pricempg 1 AMC Concord 22 2930 4099 186.318 2 AMC Pacer 17 3350 4749 279.353 3 AMC Spirit 22 2640 3799 172.682 4 Buick Century 20 3250 4816 240.800 5 Buick Electra 15 4080 7827 521.800
SAS V612 stands for SAS version 6.12 and SAS system files created with version 6.12 are known as SAS V612 system files. Older versions of SAS had V5 and V6 system files. SAS V612 system files have special extensions that are used by the operating system to "identify" them as SAS V612 files. In UNIX, the extension is .ssd01; in DOS, the extension is .sd2 . Macintosh also uses the file extension .sd2. By default, SAS writes all SAS V612 system files with the extensions .ssd01 or .sd2 (depending on which operating system SAS is running on - UNIX, DOS, or Macintosh). Note that SAS system files MUST have these extensions in the file name or else SAS will not be able to read it.
UCLA Researchers are invited to our Statistical Consulting Services
We recommend others to our list of Other Resources for Statistical Computing Help
These pages are Copyrighted (c) by UCLA Academic Technology Services