In reply to Rebekah.

Thank you, Rebekah, for sharing your code iteration. Very impressive and efficient use of FILENAME() function and FILEVAR= option of FILE statement!

One area which could be improved on (at least stylistically) is the IF _n_=1 THEN DO; ... END;
First of all, RETAIN statement is declarative (non-executable) statement and it should not be placed into conditional DO-group. It will be "executed" regardless of whether condition is true or false as it takes effect right after compilation before DATA step iterations. It behaves similar to SAS global statements (see my recent post How to conditionally execute SAS global statements.) Second, you can assign initial values in the RETAIN statement itself, so instead of the whole IF-THEN-DO construct you can make it a one-liner:
retain stem "&filestem" maxrecs &maxrecs filenum 0;

Another, in your outfile = cats("'",stem,filenum,"'"); you don't need those "'", you can write it simply as outfile = cats(stem,filenum); as you don't need single quotation marks to be part of the outfile variable value.

Will it work for you?
Best regards,
Leonid