When making code dynamic, you need to follow these steps:
have working code for a single instance (you have that in KF.sas)
identify all things in the code (statements, values, variable names etc) that have to change from one run to another
then decide if you can set up an identifier that allows BY-group processing
only if the previous point can't be done, prepare a macro:
replace all changing elements with macro variables
wrap the code into a macro definition, where the changing elements are supplied as parameters (or derived from the parameters)
set up a means to call the macro repeatedly (from a loop, from a dataset, ...)
So you need to show us first what is relevant with regards to #2.
... View more