This is happening because when you delete the record, it is still physically there, but the pointer is gone. If you did a DSPFD, you would see it has 0 records and 4 deleted records.
If you did a RGZPFM command, it would show 0 records and 0 deleted records and your process would return 1,2,3,4 as you desire.
But the real question is what exactly are you trying to do. If you build the file with SQL, you can have a field that would automatically increment by 1 whenever a record is added.
This is some background informaiton, if you give us more specifics on your intent, we can give you more assistance.
RRN() does not return a row-number. It is the “relative record number of a row”. If the first row is in relative record number 5, then RRN() will return (5), not (1).
ROW_NUMBER (or ROWNUMBER) is a very different value. It is available in V5R4 and later in OLAP queries. You might use something like:<pre>
SELECT row_number() over () as RowNum FROM mytable</pre>
The OVER () clause specifies ordering for the ROW_NUMBER() determination.