My understanding is that you have a list of dataset names in a dataset. You need to get each dataset in that list in turn. You want to copy the first and last records of each dataset into a new file - 1 new file for each dataset.
If that is correct then:
Q1. How many datasets can be in the list?
Q2. How many records in each dataset?
Q3. What is to happen if any of the datasets are empty?
Q4. What are the LRECL and RECFM of each dataset?
How you extract the records to be written to the new datasets depends on the answer to Q2. If a small number then it can be done purely within Rexx otherwise your Rexx program will have to set up a call to SORT.
The problem I have is that people can explain things quickly but I can only comprehend slowly.