My previous blog entry, Code Formatter, showed a technique to clean up ugly SAS code. This entry utilizes the %codeformat macro to make macro code processed using the mprint and mfile options easier to read.
A special filename reference called MPRINT and the options MPRINT and MFILE must be turned on prior to running the macro code. In order to handle this, the first parameter of macroformatter contains the macro call to be processed without the leading % sign. The output of the rendered MPRINT/MFILE is difficult to read because each line starts in the first column. That is where the codeformatter macro comes into play to make it easier to read.
Consider the following macro that contains multiple commands on a single line followed by the output using MPRINT/MFILE
%macro x( what ) ; data x ; set sashelp.class; saywhat = "&what." ; run ; title "%sysfunc(date(), downame. ) report" ; proc print data = x ; var name age weight saywhat ; run ; %mend ; %x( hello ) data x ; set sashelp.class; saywhat = "hello" ; run ; title " Monday report" ; proc print data = x ; var name age weight saywhat ; run ; filename mprint clear ;If you were to call the %x(what) macro using the macroformatter you'd get the output that follows:
%macroformatter( mname = x( hello ), outputfile = c:\temp\tdb.sas ) data x ; set sashelp.class; saywhat = "hello" ; run ; title " Monday report" ; proc print data = x ; var name age weight saywhat ; run ; filename mprint clear ;
Below is the source code.
/************************************************************************** * Program: macroformatter.sas * Author: Tom Bellmer * Created: 12OCT2014 * Purpose: writes SAS macro to a file and formats it **************************************************************************/ %macro macroformatter( mname =, outputfile = c:\temp\formattedcode.sas ) ; %local tempfile fref rc ; %*-- delete tempfile to keep it from being appended ; %let tempfile = %sysfunc( pathname( work ) )/macroformatter.txt ; %let rc = %sysfunc( filename( fref, &tempfile. ) ) ; %let rc = %sysfunc( fdelete( &fref. ) ) ; filename mprint "&tempfile." ; options mprint mfile ; %&mname. filename mprint clear ; options nomprint nomfile ; %codeformatter( inputfile = &tempfile., outputfile = &outputfile ) %mend ;
No comments:
Post a Comment