Administrative Information Services
Data Administration
VISION:Results Workshop #15
NON-SEQUENTIAL FILE PROCESSING IN VISION:Results
Up to this point in your VISION:Results programming, you have learned to process files sequentially. Most files that you encounter will be sequential files. However, you will occasionally need to access files via other methods. To address this need, you need to learn how to process VSAM files.
VSAM is an acronym for Virtual Sequential Access
Method. Files created as VSAM files have special properties that allows them to be
accessed in more powerful ways than simply reading them sequentially. There are three
different types of VSAM files. They are ESDS (Entry Sequenced Data Sets), RRDS (Relative
Record Data Sets), and KSDS (Key Sequenced Data Sets). We will limit our discussion to the
most commonly used, KSDS.
B. Difference Between Sequential/VSAM
When a sequential dataset is created, each
record is written to the file, one right after the other. The records are physically
located in the file in the sequence in which they were written. When the sequential
dataset is processed, you begin reading at the top of the file. Your program reads each
record in the file and applies selection criteria to see if that record is needed for your
program. This sequential processing method is very effective when you will be selecting
the majority of the data on the file. As you can see, however, it would be rather
inefficient to process an entire file, checking each record on the file to see if it's one
that you need, when you will only be selecting a handful of records from that file.
VSAM allows us to begin reading anywhere in the
file that we need. When you process a VSAM file, you give it a 'key'. The system uses that
key to find the specific record that you are asking for. This process frees us from having
to read every record on a file in order to find a specific record. Records in a KSDS VSAM
file are stored in order by their key fields.
Every VSAM file has a corresponding index. This index is updated each time the VSAM file is updated and contains 'pointers' that tell the system where to find each record, according to it's key value. The 'key' positions and locations are defined when the VSAM file is originally created. As a programmer processing a VSAM file, the information on the key fields for a specific VSAM file needs to be obtained from the applications support section that created the file.
A. Matching Sequentially VS Randomly
In order to match two sequential files, you are
forced to read every record on both the primary and secondary files, comparing key fields
to see if you have a match. This process is very effective for sequential files,
especially when there is a strong correlation between the two files. However, if there are
only a few matches in relation to the number of records on the file, your program has to
perform a substantial amount of additional input and selection on extraneous records..
Fortunately, as long as the secondary file of a
match is a VSAM file with the match field defined as a key, you can search for the match
without processing the entire file. You simply move the value you want to search for to
the key field for the VSAM file and then read the VSAM file.
VSAM files allow online systems to run quickly
and efficiently. For example, let's look at ENROLL. If the enroll system had to read the
massive student database sequentially, looking at each record on the file before it
brought up a student's enrollment information, you can imaging how long it would take for
a student to get their data on screen for queries and updates!!
Through the use of VSAM files, once a student
enters their student number and PAN number, the system quickly brings back the student's
information to the screen. It only needed to read information pertaining to that one
student.
III. Processing VSAM Sequentially
Although VSAM files provide direct access, they can also be read sequentially, just like any other file. There are several client offices that currently process VSAM files in this manner. You would process a VSAM file sequentially if:
1) you are interested in processing all or the
majority of the records on the file
2) you are using this file as the primary file in a
match
There is a minimal amount of additional file set-up required. Once that additional set-up is defined, you simply read it like any other file, processing until the end of the file.
A file statement defined to process a VSAM file sequentially would look like the following:
FILE CLASSVS KSDS F
The KSDS tells the system that we will be using a key sequenced dataset and the F tells the system that the record length is fixed.
The following is a sample of the logic used to process a VSAM file sequentially:
SEQUENTIAL READ LOGIC SAMPLE
READ CLASSVS
DOWHILE CLASSVS.STATUS NE 'E'
;PROCESS RECORD
READ CLASSVS
ENDDO
STOP
When you read a file randomly, you tell the
system which record you wish to read by placing the value you wish to read for in a
predefined key field. When the read is executed, the file is then searched for the
specified key field value. If there is such a record, it is returned for processing.
Otherwise an indicator is returned with a value specifying that there was no such record
on the file with the specified value. The record that you are searching for could be the
first or last record on the file, or any record in between.
Although VSAM files require a bit more system
overhead than sequential files do, this method of file processing provides you with a very
effective tool for match processing.
1. The VSAM File Statement
In order to process a VSAM file randomly, you need to tell the system that the file you will be working with is VSAM. You do this by adding a few new parameters to the FILE statement in your VISION:Results program. Each parameter of the FILE statement for a VSAM file is discussed in the text following this sample file statement:
FILE CLASSVS KSDS F 999 RANDOM CLASSVSKEY KEYLEN 999
FILE the portion of the statement that tells the system that you are about to define a file to be used in the program's processing. It is a required part of the FILE statement.
CLASSVS file name
KSDS required for all of the VSAM files you'll be using. This tells the system what type of VSAM file it is processing. KSDS stands for Keyed Sequential Dataset.
F Required. This tells the system that the file you're reading has a fixed length.
999 used in conjunction with the F and tells the system the length of each record on this fixed file.
RANDOM keyword that tells the system that you'll be processing random reads against the file that is being defined. Other valid keywords here could be SKIP (which will be discussed later) and SEQUENTIAL (which is the default).
CLASSVSKEY defines the field that you want to use as the 'key' to the records on the file. Normally this name reflects the file that this field is the key of. For example, for the CLASSVS file, I might call this field CLASSVSKEY. The default field type of the keyfield is CH. If you wish the key to be numeric or packed, you must indicate such by placing the field type after the keyfield name on the file statement. For example:
FILE CLASSVS KSDS F 36 RANDOM CLASSVSKEY NU KEYLEN 5
indicates that the KEYFIELD called CLASSVSKEY is numeric and five bytes long.
KEYLEN tells the system that you're about to define the length of the field defined as the KEYFIELD.
999 corresponds with the KEYLEN
keyword and tells the system the length of the KEYFIELD.
2. Reading the VSAM File Randomly
In order to access records from a VSAM file randomly, we need to tell VISION:Results what record we're looking for. You do his by moving a 'key' value into the KEYFIELD that you've defined on the file statement. Then you simply issue a READ command. The statements:
MOVE EMPLOYEE_NUMBER TO
CLASSVSKEY ;put a value in key field
READ
CLASSVS
;read the file
will cause the current value of EMPLOYEE_NUMBER to be moved into the KEYFIELD parameter named CLASSVSKEY. When the read is issued on CLASSVS, the system will try to find a record that contains a key value equal to the value we just moved into the KEYFIELD.
If there was no record on the file that matched the key field, the file STATUS field will be equal to blanks. If a match was found, STATUS will equal "Y".
The following code illustrates the logic for matching using a random read. This sample assumes that all required OPTION, REPORT999, FILE statements, and workareas have already been defined.
RANDOM READ MATCHING LOGIC SAMPLE
READ CLASSFL ;GET A CLASSFL RECORD
DOWHILE CLASSFL.STATUS NE
"E"
;WHILE THERE ARE
;RECORDS ON
;CLASSFL
MOVE EMPLOYEE_NUMBER TO
CLASSVSKEY ;SET UP THE KEY
READ
CLASSVS
;RANDOM READ
IF CLASSVS.STATUS EQ
"Y"
;IF THERE WAS A MATCH
;PERFORM PROCESSING ON THE MATCH HERE
ENDIF
READ
CLASSFL
;GET ANOTHER FINDER RECORD
ENDDO
STOP
A. Principles
SKIP sequential processing allows you to use a direct read to get to a certain point in the file and then read sequentially from that point on until some condition is met. For example, if you are reading a file that contains several records per department, you might want to do a direct READ to get to a specific department and then process each of the records for that department sequentially.
B. Syntax
1. The VSAM File Statement for SKIP Sequential Processing
SKIP processing allows you to get directly to a certain point in a file and then read sequentially from that point on. In order to process in skip mode, simply use the SKIP option on your file statement as opposed to RANDOM.
The POSITION parameter defines where the processing should begin with a SKIP key read. In most cases, you will want to move an "E" into the variable for the POSITION parameter. The default is to return you the first record whose value is equal to or greater than the specified key value. The following FILE statement defines a VSAM file for SKIP sequential processing:
FILE CLASSVS KSDS F 36 SKIP CLASSVSKEY NU KEYLEN 5 POSITION CLASSVSPOS
To specify that you want the record equal to the specified key value, you would add the following statement at the beginning of your logic code: MOVE 'E' TO CLASSVSPOS
2. Reading the VSAM File Using Skip Sequential Processing
The following sample illustrates the logic necessary to process a file in skip mode. This sample assumes that all required OPTION, REPORT999, FILE statements, and workareas have already been defined.
SKIP MODE PROCESSING LOGIC SAMPLE
MOVE 'E' TO
CLASSVSPOS
;RETURN RECORD EQ TO KEY READ CLASSFL
DOWHILE CLASSFL.STATUS NE
'E'
;WHILE THERE ARE RECORDS
;ON THE FINDER FILE
MOVE DEPARTMENT TO
CLASSVSKEY ;SET UP THE
KEY
READ
CLASSVS
;SKIP READ FOR DEPARTMENT
DOWHILE CLASSVS.STATUS EQ 'Y'
AND ;WHILE THERE ARE RECORDS
CLASSVSKEY EQ
DEPARTMENT
;AND DEPARTMENT MATCHES
LIST CLASSVS.EMPLOYEE_NUMBER
CLASSVS.SOC#
CLASSVS.NAME
READ
CLASSVS
;SEQUENTIAL READ
ENDDO
READ CLASSFL
ENDDO
STOP
C. Partial Keys
You may need to process a VSAM file for which you only have a portion of the key field available to you. For example: we had a file of employee data that had a KEYFIELD comprised of EMPLOYEE_NUMBER and HIREDATE and my primary file only has employee number on it. SKIP sequential processing can be used to process these records when you only have a partial key value.
To process a file with a partial key, we need to provide an additional parameter to the FILE statement:
FILE CLASVS4 KSDS F 14 SKIP CLASVS4KEY NU KEYLEN 11 PARTKEY 5 POSITION CLASVS4POS
The PARTKEY parameter tells the system we will be using a partial key and the number following tells the system how long that partial key is. In this example, we have a full key length of 11 bytes and our partial key is the first 5 positions of that key.
The following is a sample of the logic necessary for Partial Key Skip Sequential Processing:
PARTIAL KEY SKIP MODE PROCESSING LOGIC SAMPLE
WORKAREA NAMED CLASVS4PART
CLASVS4_KEY 11 1 NU
CLASVS4_EMP_NUM 5 1 NU
CLASVS4_HIREDATE 6 6 CH VALUE " "
READ CLASSFL
VI. Combining VSAM Access Methods
As your programming needs become more complex, you will find times when you need to process multiple files, both sequential and VSAM. It is not unusual to see different access methods combined into the same program and/or report step. You might process a file sequentially, using selected records to process direct random reads to a VSAM file. You might then use parts of that retrieved VSAM record to build a key to read another VSAM file, and so on... You can combine these access methods in any logical order. The only requirement is that you utilize a key value to link each file to the next.
* * * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* INPUT FILE: CLASSVS FORMAT: KSDS F 36
* EMPLOYEE FILE
* * * * * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * *
FILE CLASSVS KSDS F
EMP_NO 5 1 NU ; EMPLOYEE NUMBER *KEY FIELD*
SOC# 5 6 PD ; SOCIAL SECURITY NUMBER
EMP_NAME 20 11 CH ; EMPLOYEE NAME
REGION 1 31 CH ; REGION
BRANCH 2 32 CH ; BRANCH
DEPT 3 34 NU ; DEPARTMENT
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
EMP_NO 5 1 NU ; EMPLOYEE NUMBER *KEY FIELD*
STREET 20 6 CH ; STREET
CITY 12 26 CH ; CITY
STATE 2 38 CH ; STATE
ZIP 5 40 NU ; ZIP CODE
HIREDATE 6 45 NU ; HIREDATE
MONTH 2 45 CH
DAY 2 47 CH
YEAR 2 49 CH
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
SOC# 5 1 PD ; * KEY FIELD *
; SOCIAL SECURITY NUMBER
PAY_RATE 4 6 NU ; EMPLOYEE PAY RATE
GROSS_PAY 4 10 PD 2 ; GROSS_PAY
NET_PAY 4 14 PD 2 ; NET_PAY
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
REGION 1 1 CH ; REGION * KEY FIELD *
BRANCH 2 2 CH ; BRANCH * KEY FIELD *
RB_NAME 18 4 CH ; REGION BRANCH NAME
CITY_NAME 14 22 CH ; CITY NAME
Michigan State University Copyright 1998. All rights reserved. This page last updated on 06/02/02 10:55 PM.
If you have comments or suggestions about this webpage Email us. (Please do not change the subject line)