If you want us to fix your code for you, you have to post it (completely, of course), including all information about available memory, disk space etc, so we can get a picture where your bottlenecks are.
Keep in mind that this is a forum where tips and hints are given, and code snippets for particular problems. Reworking a whole design is consultant work and usually done for money.
Ready to join fellow brilliant minds for the SAS Hackathon?
Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.