SAS Data Sets
Creating a SAS data set
If you are handling a large data file (large numbers of cases/variables) it is advisable to create a SAS data set and work with that. A SAS data set is a specially structured files readable only by the SAS System from the operating system where the file was created. A stored SAS file cannot be edited. SAS data sets are referenced with a one- or two-level name. The two-level name is of the form libref.member-name, where libref refers to the directory in which the data set is to be stored or read, and member-name refers to the name of the SAS data file to be created or read. If a two-level name is not used, SAS stores files in a temporary work library that is deleted when you exit SAS.
Suppose you wished to read in an external file, and store the data in a permanent SAS data set. The program file you would create would look something like the one below, which is a subset of the sample data and program files described previously. Note the first two lines in the following program where the libname command is issued to reference the directory in which the SAS file is to be created, and the two-level name links the member-name (e.g., anxiety) to the libref (e.g., try1).
INPUT ID 1-2 SEX $ 3 EXP 4 SCHOOL 5
(C1-C10) (1.) (M1-M10) (1.) (MATHSCOR COMPSCOR) (2.);
IF MATHSCOR=99 THEN MATHSCOR=.;
IF COMPSCOR=99 THEN COMPSCOR=.;
C3=6-C3; C5=6-C5; C6=6-C6; C10=6-C10;
M3=6-M3; M7=6-M7; M8=6-M8; M9=6-M9;
COMPOPI = SUM(OF C1-C10);
MATHATTI = M1+M2+M3+M4+M5+M6+M7+M8+M9+M10;
LABEL ID='STUDENT IDENTIFICATION' SEX='STUDENT GENDER'
EXP='YRS OF COMP EXPERIENCE' SCHOOL='SCHOOL REPRESENTING'
MATHSCOR='SCORE IN MATHEMATICS'
COMPSCOR='SCORE IN COMPUTER SCIENCE'
COMPOPI='TOTAL FOR COMP SURVEY'
MATHATTI='TOTAL FOR MATHATTI SCALE';
Replace C:\TEMP\ with the appropriate directory. When the job is executed a SAS data set named anxiety.sas7bdat will be stored in the directory assigned.
Accessing a SAS data set
To read an existing SAS data set, use a two-level name of the form libref.member-name. The following example illustrates how to access the SAS data set (e.g., anxiety.sas7bdat) created and run some SAS procedures with it. (Note that try2 is given as libref and anxiety is given as member-name. The SET command used below reads the SAS data set called try2.anxiety into the data area called test.
VAR COMPSCOR MATHSCOR COMPOPI MATHATTI;
It is also possible to retrieve only a subset of the original data using an IF statement. For example, if you wanted to retrieve only the female respondents from the anxiety.ssd file, the DATA step of your program would look something like this:
The IF statement in this example tells SAS to only read in those observations where the character variable SEX is equal to "f." All other cases will be ignored. Several conditions can be set with an IF statement.
SAS transport libraries
SAS also handles transport format data sets. You create a transport format file when you want to move your SAS data set to another operating system (e.g., UNIX). Also, if you are bringing SAS data sets from another operating system into SAS for Windows, you must first save the file in transport format.
Suppose you want to create a SAS transport format file from the SAS data file, anxiety.sas7bdat. Define a libname to read the SAS data set, and another libname to write a SAS transport file. The XPORT (for transport engine) parameter is used to indicate that you want to create a transport format file. A transport format file always has a fixed block size with a record length of 80, and a block size of 8000. The select statement may be omitted if there is only one SAS file stored in the directory or if you want to convert all the members of a single SAS data library into SAS transport format file.
LIBNAME test2 XPORT 'C:\TEMP\trans.dat';
PROC COPY IN=test1 OUT=test2;
Once the job is executed, a file, trans.dat, will be created and stored in the directory specified.
To read a transport format file (e.g., trans.dat) stored on the disk and create a SAS data file, as in the example given above, define two libnames (one for reading, and one for writing) as in:
LIBNAME test2 XPORT 'C:\TEMP\trans.dat';
PROC COPY IN=test2 OUT=test1;
The select statement may be dropped if you want to convert all the members of the transport library into a SAS file, or if there is only one member in the specified transport library. If you want to read/write SAS transport files involving format library use the CIMPORT/CPORT procedures instead of the COPY procedure.
Next: Using SAS/Graph
Up: Table of Contents
Prev: Sample SAS Data Sets