SAS Tutorial: One PUT Does It All

The Problem

You have been asked to write a program that will produce from a master data source a number of identically formatted reports. The only difference between these reports is the selection criteria used to select individual records for each of the reports.

As a novice, I would have coded one data step in its entirety for each report. I have found that the most onerous part of this is the coding of the PUT statement for the variables that are to be output, especially if there are a lot of variables. However, once I had the first PUT coded, I could just copy it to the subsequent data steps. Oops, the users have just requested that I change the output format - now I have to go back and modify every one of the PUT statements. What a pain, especially since I know that they will probably ask for more changes in the future, whether it is to add a new variable, drop an existing variable, or rearrange the variable order. All in all, I would much rather be surfing the web than coding PUTs.

The Solution

I have found it very useful to make just one PUT statement and store it in a macro. Obviously, the more variables that are contained in the PUT statement and the more times it is used, the greater the savings in time that you the programmer will gain. Further, by restricting yourself to using just one version of the PUT statement, you only have to worry about changing one version of the PUT rather than changing it for each and every data step it is used in.

A Code Example

The following is a simplified example of what such a macro would look like and how it could be used.

**************************************************;
* STEP01: CREATE A MACRO FOR THE PUT STATEMENT   *
**************************************************;
%MACRO DOPUT;
PUT @001 VARIABLE1 $CHAR5.
    @008 VARIABLE2 $CHAR3.
    @014 VARIABLE3 9.2
    @026 VARIABLE4 7.0
    ;
%MEND DOPUT;
RUN; 
**************************************************;
* STEP02: CREATE THE FIRST REPORT OUTPUT DATASET *
**************************************************;
DATA _NULL_;
SET MASTERDB.SOURCEDATA;
FILE OUTPUT1 NOPRINT NOTITLES;
IF VARIABLEX='CRITERIA 1';
%DOPUT
RUN; 
**************************************************;
* STEP0n: CREATE THE LAST REPORT OUTPUT DATASET  *
**************************************************;
DATA _NULL_; 
SET MASTERDB.SOURCEDATA;
FILE OUTPUTn NOPRINT NOTITLES;
IF VARIABLEX='CRITERIA n';
%DOPUT
RUN;

Conclusion

SAS macros are an execellent way to make SAS code reuseable. I highly recommend the use of SAS macros as a way of storing and accessing any block of code, or any very long statement like a PUT, that is to be reused multiple times within the same program.

Alternatively, if your code block is duplicated across a number of SAS programs, you should consider storing the macro code in its own dataset and using the SAS %INCLUDE statement to access it.


Return to the Mainframe section index.