You should investigate prompt macros with type "token". The article link describes dynamic sorting which is similar to what you are trying to achieve. The bigger challenge will be figuring out how many grouping levels to support.
#prompt('p_SortColumn'...
#prompt('p_SortColumn'...