You may say that it is pretty superfluous to create your own program to perform a CHANGE. First of all it is a command in the ISPF editor and if the ISPF editor is not available or not able to do the job then FILEAID/Batch or DFSORT may help. All these tools are very good, but some time ago I ended up in a situation where the dataset I wanted to change was too big for the ISPF editor. The data I needed to change was not in any fixed position which excluded DFSORT. FILEAID/Batch was not available at the installation and other tools could not help because I did not want to change all instances of the search string. So I was in deep trouble.
In my search for a solution I stumbled across an example in a PL/I manual which shows how to update (read CHANGE) a sequential file. When you know how to do that you can code exactly the CHANGE you are in need of and that is what I did. Before I show you a little example I just need to warn you that it is performing an "update in place". If the program fails during execution your dataset is corrupted. So please make a copy before execution. On the other hand you may change almost any type of dataset. And now the example:
This example performs what is the equivalent to a CHANGE DSN=OLDPRE. DSN=NEWPRE. ALL in the ISPF editor. Data is read from DD-name SOURCE and data is also written to DD-name SOURCE, because of the OPEN FILE(target) OUTPUT TITLE('SOURCE') statement. Actually you have your dataset opened twice at the same time in the program.
If you need to make a change to a string with another length than the source string, you must define what to do with the empty space you may create or the characters you may not have any place for any more. Please remember it is an update in place, so you cannot change the length of the record without corrupting the dataset. I have managed so far without trouble because my source strings had the same length as the target strings. Therefore I do not have any recommendation to what may be the best approach when having different lengths. It will probably depend on the situation.