I’m not sure I follow what you mean by this. Do you mean build a logical file from an RPG program with key fields determined by the program?
Can you give a more detailed explanation of what you want to do?
The simplest way to build a “logical file with keys” is to use embedded SQL to run a CREATE INDEX statement. Next simplest but much more flexible, use QCAPCMD or QCMDEXC to run an OPNQRYF command.
Neither of those is particularly difficult.
However, there is no way to use either one in the program if random keyed access is the purpose. The file format with its key structure must exist when you compile the program. Creating it at run-time won’t work. The exception is when you create the LF to match a format that the program is already compiled over.
But that doesn’t seem to be what the question needs to know.
If dynamic creation of a keyed LF must be combined with random keyed access in the same program, you will need to investigate MI functions at a much deeper level.