In reply to Jack Hamilton.

Thank you, Jack, for your feedback.

You brought up a broader topic of conditional code generation. For this purpose, in general, the method that you describe (writing to a file and then %include) provides greater flexibility than CALL EXECUTE which I used as an example of conditional execution of SAS global statements:
1) you can %include not just right after the data step where this code was generated (as CALL EXECUTE does), but in other downstream places of your SAS program.
2) you can generate not just SAS code, but any language code, including OS scripts, which can then be executed from your SAS code using X-statement or CALL SYSTEM().