I suggest you do some debugging by using %PUT statements inside the macro program, so you can track how the various values are getting set/changed.
Also, I would recommend testing this macro program as a standalone program. My rule for writing a stored process (SP) is to have a working SAS program -- one that executes correctly in a standalone SAS session (such as running from Display Manager or an EG code node) BEFORE turning that program and/or its macro programs into a stored process.
You might also consider working with Tech Support on this issue, as it is hard to debug a stored process without seeing how the SP is defined, what server it's running on, what parameters are being passed, etc, etc. In fact, this code snippet doesn't look like -ALL- of your stored process -- it looks like a macro that's being used -inside- a bigger stored process program. Where are your %stpbegin/%stpend -- what output is being generated by the SP - -what is the purpose of this macro program??? Tech Support could look at your WHOLE stored process and all of your code and they may be able to help you come to a quicker resolution.