P.S.: I've just read PGStats' solution more closely, and mine isn't that much different than PGStats' solution. Use whichever one works for you. But do wrap your head around "greedy" vs. "non-greedy" - it applies to both solutions.
Please post your question as a self-contained data step in the form of "have" (source) and "want" (desired results).
I won't contribute to your post if I can't cut-and-paste your syntactically correct code into SAS.