And what is the question? You have code posted above, some of which you have marked as the answer to your question. If this does not fill your needs then open a new topic, post test data in the form of a datastep, post example output, and what you have tried.