Processing an RRDS in a SAS Job |
Three Ways of Reading Records from an RRDS |
You can read RRDS records with sequential access, direct access, and with a combination of both sequential and direct access. (See Access Types for RRDS Operations). The type of RRDS Read operation is specified with the appropriate options in the SAS INFILE statement. You must specify either the VSAMREAD or the VSAMUPDATE system option in order to read VSAM data sets.
Reading an RRDS with Sequential Access |
With sequential access, the RRDS records are read in relative-record order; that is, they are read from the first record to the last. This is the default.
data one; infile myrrds vsam; input; ...more SAS statements...
If the BACKWARD option is specified, the data set is read backward, starting with the last record and ending with the first.
Reading an RRDS with Direct Access |
An RRDS is read directly using keyed direct access where the relative-record number (RRN) is treated as a key. For this type of access to be meaningful, you must know the RRNs of the records that you want to read. You might know the RRN of a record because it has some relationship to the record contents or because you have obtained it in some other way.
To read an RRDS with keyed direct access, use the RRN= option in the INFILE statement. The RRN= option defines a SAS variable whose value you set to the RRN of the record that you want SAS to read. The variable is created if it does not exist and is not added to the output data set.
In the following program, the RRDS data set is read sequentially, and the relative-record numbers are obtained from the automatic variable _RRN_ and stored in the SAS data set RRNS. DATA TWO uses the RRNS SAS data set to process the RRDS by relative-record number.
data rrns; infile myrrds vsam ; input; rrnvar=_rrn_ ; keep rrnvar; run; data two; set rrns; infile myrrds vsam rrn=rrnvar; input; ...more SAS statements...
Reading an RRDS with Skip Sequential Access |
With skip sequential access, the initial record of a series is located with keyed direct access. After the first record is obtained, subsequent records are retrieved sequentially. Skip sequential processing improves performance because sequential retrieval requires less overhead and is faster than direct retrieval. Skip sequential access is also useful when you know the RRN of the first record that you want but do not know (or do not want to specify) the RRN of subsequent records.
Use the SKIP option in the INFILE statement to specify skip sequential processing. Retrieve the first record directly by specifying the RRN of the record that you want with the RRN= option in the INFILE statement. With the SKIP option, leaving the value specified by the RRN= variable unchanged turns off direct access and indicates that subsequent records are to be retrieved with sequential access. The relative-record number of each record that is retrieved is returned in the _RRN_ automatic variable. The relative-record numbers might not be consecutive, because some of the slots might be empty.
When you process skip sequentially, you must specify a means of stopping the DATA step. In the following example, end-of-file sets the feedback code to 4, and the IF FDBK=4 clause stops the DATA step. Note that the SKIP option retrieves only the one record with an RRN that matches the value of the RRN= variable value. You must supply statements to read additional records.
The following example processes an RRDS skip sequentially. For meaningful output, this example assumes that the data set was sorted by class before it was loaded. The program reads in the RRNUMS data set, reads all the records in the PROCESS data set that have those RRNs, and then writes them to a procedure output file. Note that the SKIP option retrieves only the records that are identified by the RRNs. You must supply statements to read additional records. In the following example, the program sequentially reads other records in the same class:
data rrnums; input idnum class $; cards; 0001 FR 0013 JU 0025 SO ; run; data process; set rrnums; file print; if _n_=1 then do; put 'The RRDS records selected skip sequentially are: '; put; end; /* Get the first record wanted with direct access. */ infile myrrds vsam rrn=idnum skip feedback=fdbk; input @; /* Stop if the FEEDBACK= variable indicates end-of-file */ /* or if the RRN slot is empty or invalid. */ if fdbk=4 | fdbk=16 | fdbk=192 then do; _error_=0; if fdbk=4 then stop; else do; put 'RRN slot is empty, or invalid RRN. The feedback ' 'code is ' fdbk ' and RRN is ' idnum; fdbk =0; return; end; end; /* Read next records sequentially while the class matches. */ /* Write the records to the procedure output file. */ input @86 classnow $ 86-87; do while (classnow=class); put _infile_; /* Stop if the FEEDBACK= variable indicates end-of-file */ /* or if the RRN slot is empty or invalid. */ if fdbk=4 | fdbk=16 | fdbk=192 then do; _error_=0; if fdbk=4 then stop; else do; put 'RRN slot is empty, or invalid RRN. The feedback ' 'code is ' fdbk; fdbk=0; return; end; end; input @86 classnow $ 86-87; end; run;
Operation |
Read
(INFILE/INPUT Statements) |
Write
(FILE/PUT Statements) |
---|---|---|
Read | Sequential | Does not apply |
Direct with RRN= option | ||
Skip sequential with SKIP and RRN= options | ||
Add* | Direct with RRN= option | Must be direct: use the RRN= option |
Sequential with SEQUENTIAL and RRN= options | ||
Update | Sequential | Direct: the record read is the record updated |
Direct with RRN= option | ||
Erase | Sequential | Direct: the record read is the record erased |
Direct with RRN= option | ||
Load | Does not apply | Sequential: in relative-record order |
Direct with RRN= option | ||
* The INPUT statement is not required. |
Copyright © 2008 by SAS Institute Inc., Cary, NC, USA. All rights reserved.