KSDS (for Beginners)


  • KSDS is a short form of Key Sequential Data Set.
  • KSDS datasets contain DATA and INDEX components. The DATA component has all the data records in a logical ascending order based on the key. The INDEX component has pointers to all data records (every data record has a corresponding pointer) to access data.
  • KSDS allows fixed-length or variable-length records.
  • The records are always inserted in sorted order based on the key value. For example, let us assume we are trying to insert a record with a 100 key value; it inserts after the record with the highest value of less than 100.
  • The records are reorganized as specified below for each operation -
    • If a record is newly inserted, then the records after the newly inserted record will be reorganized to maintain the logical order for easy retrieval.
    • If any record gets deleted, the records after the deleted one will be reorganized to maintain the logical order for easy retrieval.
    • If any record is updated, there will be no change in positions.

Key Restrictions -


  • Key length should be the same in all logical records.
  • Key length can be from 1 to 255 bytes.
  • Key offset (from the record starting) should be the same in all logical records.
  • The key value should be unique and no duplicates allowed in all logical records.

Allowed Accessing Types -


VSAM KSDS always uses the INDEX component to access records from the DATA component in ascending or descending order by key.

KSDS supports three types of accesses, and those are -

Access Type Description
Sequential Access Sequential access uses to retrieve (reading) or inserting (writing) the records sequentially.
Direct Access Direct access uses to process the records directly by using key values. The application program should provide a key value for each record to be processed (read or written).
Dynamic Access
or
Skip Sequential
Skip-sequential access is a combination of the above two accesses. The application provides a key value, and all records are processed sequentially from the located logical record.

DEFINE KSDS Syntax -


IDCAMS DEFINE CLUSTER command with INDEXED parameters used to create KSDS dataset.

Syntax - JCL for creating KSDS dataset with minimum parameters

//JOBCARD
//*------------------------------------------------------------------
//* Definition of KSDS
//*------------------------------------------------------------------
//STEP01  EXEC PGM=IDCAMS
//SYSPRINT DD SYSOUT=*   
//SYSOUT   DD SYSOUT=*  
  DEFINE CLUSTER(NAME(userid.CLUSTER.NAME)	-     
    RECORDSIZE(avg max)    				-                     
    CYLINDERS(primary secondary)    		-                     
    FREESPACE(primary secondary)    		-                     
    KEYS(length,offset)            		-                     
    CISZ(ci-size)           				-                     
    VOLUMES(volume-name)      				-                     
    INDEXED									-                     
    REUSE )             					-                     
  INDEX(NAME(userid.CLUSTER.NAME.INDEX))	-        
  DATA(NAME(userid.CLUSTER.NAME.DATA))             	 
/*

In the above syntax -

  • The INDEXED parameter specifies that the file created is KSDS.

Refer IDCAMS DEFINE command for full set of parameters.

Creating Fixed-length KSDS Example -


Requirement - Creating KSDS with fixed-length records of size 47 bytes.

Code -

----+----1----+----2----+----3----+----4----+----5----+----6----+----7--
***************************** Top of Data ******************************
//MATEGJK JOB (123),'MTH',CLASS=A,MSGCLASS=A,MSGLEVEL=(1,1),            
//             NOTIFY=&SYSUID                                           
//************************************************************          
//* DEFINE VSAM FIXED-LENGTH KSDS CLUSTER                               
//************************************************************          
//STEP01   EXEC PGM=IDCAMS                                              
//SYSPRINT DD SYSOUT=*                                                  
//SYSOUT   DD SYSOUT=*                                                  
//SYSIN    DD  *                                                        
  DEFINE CLUSTER(NAME(MATEGJ.TEST.KSDS)  -                              
    RECORDSIZE(47,47)    -                                              
    CYLINDERS(2,1)       -                                              
    FREESPACE(10,20)     -                                              
    KEYS(3,0)            -                                              
    CISZ(4096)           -                                              
    VOLUMES(DEVHD4)      -                                              
    INDEXED              -                                              
    REUSE  )             -                                              
  INDEX(NAME(MATEGJ.TEST.KSDS.INDEX)) -                                 
  DATA(NAME(MATEGJ.TEST.KSDS.DATA))                                     
/*                                                                      
**************************** Bottom of Data ****************************

In the above JCL, MATEGJ is the userid and change them all as required.

Output -

Once the above JCL is submitted, check the MAXCC of the job for any errors. If the MAXCC is 00 or 04, RRDS successfully created.

Create KSDS Output

Verify the KSDS in 3.4 (Dataset List utility) or any File management tools.

Create KSDS Output

Explaining Example -

In the above example,

  • RECORDSIZE(47,47) specifies the record average length is 47, and the maximum length is 47. So the KSDS file we are creating is fixed-length.
  • CYLINDERS(2,1) specifies the primary memory allocation is 2 CYLINDERS, and secondary memory allocation is 1 CYLINDER.
  • KEYS(3,0) specifies the key length is 3 bytes from the starting position 0.
  • CISZ(4096) specifies the control interval size is 4096.
  • VOLUMES(DEVHD4) specifies that allocate the ESDS on volume DEVHD4.
  • INDEXED parameter specifies the file is to create KSDS.
  • REUSE specifies the memory can reuse immediately once the file gets deleted.

Creating Variable-length KSDS Example -


Requirement - Creating KSDS with variable-length records of average size 47 and maximum size 67 bytes.

Code -

----+----1----+----2----+----3----+----4----+----5----+----6----+----7--
***************************** Top of Data ******************************
//MATEGJK JOB (123),'MTH',CLASS=A,MSGCLASS=A,MSGLEVEL=(1,1),            
//             NOTIFY=&SYSUID                                           
//************************************************************          
//* DEFINE VSAM VARIABLE LENGTH KSDS CLUSTER                            
//************************************************************          
//STEP01   EXEC PGM=IDCAMS                                              
//SYSPRINT DD SYSOUT=*                                                  
//SYSOUT   DD SYSOUT=*                                                  
//SYSIN    DD  *                                                        
  DEFINE CLUSTER(NAME(MATEGJ.TEST.VKSDS)  -                             
    RECORDSIZE(47,67)    -                                              
    CYLINDERS(2,1)       -                                              
    FREESPACE(10,20)     -                                              
    KEYS(3,0)            -                                              
    CISZ(4096)           -                                              
    VOLUMES(DEVHD4)      -                                              
    INDEXED              -                                              
    REUSE  )             -                                              
  INDEX(NAME(MATEGJ.TEST.VKSDS.INDEX)) 	-                                
  DATA(NAME(MATEGJ.TEST.VKSDS.DATA))                                    
/*                                                                      
**************************** Bottom of Data ****************************

In the above JCL, MATEGJ is the userid and change them all as required.

Output -

Once the above JCL is submitted, check the MAXCC of the job for any errors. If the MAXCC is 00 or 04, RRDS successfully created.

Create KSDS Output

Verify the KSDS in 3.4 (Dataset List utility) or any File management tools.

Create KSDS Output

Explaining Example -

In the above example,

  • RECORDSIZE(47,67) specifies that the record average length is 47, and the maximum length is 47. So the KSDS file we are creating is fixed-length.
  • CYLINDERS(2,1) specifies the primary memory allocation is 2 CYLINDERS, and secondary memory allocation is 1 CYLINDER.
  • KEYS(3,0) specifies the key length as 3 bytes from the starting position 0.
  • CISZ(4096) specifies that the control interval size is 4096.
  • VOLUMES(DEVHD4) specifies that allocate the ESDS on volume DEVHD4.
  • INDEXED parameter specifies the file is to create KSDS.
  • REUSE specifies that the memory can reuse immediately once the file gets deleted.

Using utilities -


IDCAMS utility is used to load the data to the KSDS file from another file or instream data.

Requirement - Copy data from PS file to KSDS file.

Input PS File - MATEGJ.TEST.DATA

Code -

----+----1----+----2----+----3----+----4----+----5----+----6----+----7--
***************************** Top of Data ******************************
//MATEGJR JOB (123),'MTH',CLASS=A,MSGCLASS=A,MSGLEVEL=(1,1),            
//             NOTIFY=&SYSUID                                           
//************************************************************          
//* LOADING DATA TO KSDS FROM PS(FLAT FILE)                             
//************************************************************          
//STEP01   EXEC PGM=IDCAMS                                              
//SYSPRINT DD   SYSOUT=*                                                
//INPUT    DD   DSN=MATEGJ.TEST.DATA,DISP=SHR                           
//OUTPUT   DD   DSN=MATEGJ.TEST.KSDS,DISP=SHR                           
//SYSIN    DD   *                                                       
      REPRO    -                                                        
           INFILE(INPUT)   -                                            
           OUTFILE(OUTPUT)                                              
/*                                                                      
**************************** Bottom of Data ****************************

In the above JCL, MATEGJ.TEST.DATA is the flat file and MATEGJ.TEST.KSDS is the KSDS file.

Output -

Once the above JCL is submitted, check the MAXCC of the job for any errors. If the MAXCC is 00 or 04, RRDS loaded successfully with records from the PS file.

loading KSDS Output

Verify the KSDS in 3.4 (Dataset List utility) or any File management tools.

loading KSDS Output

Explaining Example -

In the above example, all the records from the MATEGJ.TEST.DATA(flat file of length 47) file copied to MATEGJ.TEST.KSDS(KSDS file of length 47) file.

Note! The flat file(PS) and KSDS file should have the same record length(47 in the above example) to load the data successfully.

Delete KSDS Example -


Requirement - Delete KSDS using the IDCAMS utility.

Code -

----+----1----+----2----+----3----+----4----+----5----+----6----+----7--
***************************** Top of Data ******************************
//MATEGJD JOB (123),'MTH',CLASS=A,MSGCLASS=A,MSGLEVEL=(1,1),            
//             NOTIFY=&SYSUID                                           
//************************************************************          
//* DELETING KSDS                                                       
//************************************************************          
//STEP01   EXEC PGM=IDCAMS                                              
//SYSPRINT DD   SYSOUT=*                                                
//SYSIN    DD   *                                                       
      DELETE 'MATEGJ.TEST.KSDS'                                         
/*                                                                      
**************************** Bottom of Data ****************************

In the above JCL, MATEGJ.TEST.KSDS is the KSDS name that is planned to delete.

Output -

Once the above JCL is submitted, check the MAXCC of the job for any errors. If the MAXCC is 00 or 04, KSDS successfully got deleted.

DELETE KSDS Output

Verify the KSDS in 3.4 (Dataset List utility) or any File management tools if required to double-check.

DELETE KSDS Output

Explaining Example -

In the above example, the MATEGJ.TEST.KSDS (KSDS file) successfully deleted and uncatalogued(space released).

Note! If the file is already deleted and trying to delete the file again, we may receive MAXCC as 08. So, verify the file once before proceeding for delete.