Caroline Bahler, Meridian Software, Inc., Raleigh NC Eric Brinsfield, Meridian Software, Inc., Raleigh NC
Caroline Bahler, Meridian Software, Inc., Raleigh NC Eric Brinsfield, Meridian Software, Inc., Raleigh NC
Paper 61-27
ABSTRACT COMPONENTS
Reports and tables can be created using PROC REPORT, PROC
PRINT and other procedures. However, there are times when the Output printer/external file location definition:
standard procedure output cannot meet the report requirements.
In those cases, reports creation using the DATA _NULL_ step is § FILE – this statement defines the destination for PUT
the solution. The objective of this paper is to discuss the statement output. FILE can point to PRINT, LOG, or a
techniques used to create a data _NULL_ report. physical location to write the report to along with any
properties of that file or a printer1. FILE is used only within
INTRODUCTION: THE _NULL_ DATA STEP the DATA step and can utilize the FILEREF defined within a
FILENAME1 or the ODS 2 statements.
Report creation within base SAS® can be accomplished through
the use of multiple procedures such as PROC REPORT and § The FILE statement deserves special study by beginning
PROC PRINT. The choice of procedure is dependent upon the SAS programmers. Examine all of the options available in
needs of the report. In general, the strength of reporting the FILE statement, because it can provide valuable
procedures is to present data in a tabular form and allows information, such as the number of lines left on the page
summarization of the data and formatting of the output. Although (LINESLEFT=).
capable of handling about 90% of typical report requests, these § FILENAME (optional) – this statement is used to define a
procedures do have limits. There are cases when the report physical location of an external file. A FILEREF is defined
requirements are not satisfied by any of the standard reporting within the statement and used later within a FILE statement.
procedures. The solution in those cases is to use the _NULL_ This statement is specified outside of a DATA step.
DATA step.
§ ODS (optional) – the ODS (Output Delivery System)
The _NULL_ DATA step is a specialized case of the DATA step. statement is used to define the type of file that will be written
When you use the _NULL_ key word, the DATA statement to the defined physical location. This statement is specified
processes all statements within the DATA step without data set outside of a DATA step.
creation1.
Some of the uses of DATA _NULL_ are: Report Output Definition
§ To create customized reports with and without ODS. The
reports can be routed to the Output window, directly to a § PUT – this statement defines the information written to the
printer, to an external file, or any destination that can be file defined within the FILE statement1. A PUT statement
defined by the FILE and FILENAME statement. can contain both variables and text contained within quotes.
The PUT statement is similar to the INPUT statement and
§ To create macro variables based on values obtained from an the same options apply1. Multiple PUT statements can be
input SAS data set or external file. used within the DATA step. The following example
§ To create ASCII data (flat) files for importing into MS Excel, illustrates the use of the PUT statement to create a report
MS Access or other programs and databases. row containing a label and variable value:
§ To execute special functions or call routines available within
the DATA step, such as CALL EXECUTE. DATA step statements …………
The objective of this paper is to discuss the major aspects of PUT @01 ‘Total Sales’
creating a custom report using the DATA _NULL_ step. @15 sales dollar10.2;
CREATING CUSTOM REPORTS In the example above, the text will be written starting in the
first column of the file being created and the actual sales
PREPARATION amount will be written starting in the 15th column.
Specification of the actual column to write the report
A custom report needs a set of requirements that define precisely information is not necessary but useful in certain situations.
how the report should be laid out. The requirements should § BY – is used to define data display, paging, and
identify:
summarization order within a report. This statement is used
§ Type of output needed: printed, viewed in Web browser, or with previously sorted data sets and contains a list of
routed to a file variables in the specific sort order1. Use of the BY statement
§ Titles, footnotes causes the creation of FIRST. and LAST. variables or each
variable listed within the BY statement3. These variables
§ Summary lines can be used to determine summarization points and when to
§ Columns within the report and how the columns should be output data to the report file or printer.
summarized FIRST.variable – defines the first occurrence of a BY
§ Placement of information on the report “page” and timing of variable value within the data set3.
page breaks LAST.variable - defines the last occurrence of a BY variable
§ Depending on the type of report being created preparation value within the data set3.
may include creating a mock-up of the report that contains For example the following statement,
column placement for each element in the report. BY STATE;
defines the variables LAST.STATE and FIRST.STATE.
1
SUGI 27 Beginning Tutorials
§ SET – there are several options of interest that can be used o ODS RTF –creates rich text format files (used by
to create reports. word processors).
o END = var - this option create a variable that To create an HTML file, for example –
contains an identifier that specifies the end of the ODS HTML BODY=”D:\HTML\RPT.HTM” ;
file.
data _NULL_;
§ _N_ - this is an automatic counter variable created by the set salesdata;
DATA step. It can be used to output information at specific FILE PRINT ODS=options;
observations.
put _ods_;
For example, when generating HTML, several statements run;
need to occur at the top of the file only. The following
statements will write the information only for the first Note: The FILE statement specifies the PRINT fileref and
observation. ODS keyword. This indicates that an ODS destination has
been defined.
data _NULL_;
set salesdata; EXAMPLE REPORTS
file RPT;
EXAMPLE1: SIMPLE CASE WITH NO INPUT DATA
if _N_ = 1 then
put @01 “<HTML>” ……..; As a simple example that shows the use of many of the
run; components described above, the following program creates a
daily calendar for any time period specified in the do-loop. More
sophisticated uses would utilize the FILE statement’s HEADER=
§ Formats – PROC FORMAT can be used to create custom for controlling the page headers and column labels and
formats that can be used to label variables within a report. LINESLEFT= to control the paging and construct the footnotes.
Program comments have been removed from all sample code to
REPORT OUTPUT ROUTING save space in this document.
§ Sending output to the OUTPUT window – using the FILE %let stopdate =30apr2002;
PRINT statement when in Display Manager will send the options linesize=76 pagesize=74;
report output to the OUTPUT window. This behavior is %let col1=16;
useful for previewing the report. For example:
%let col2=26;
DATA _null_;
data _NULL_;
file print notitles;
set salesdata;
do date="&startdate"d to "&stopdate"d;
FILE PRINT;
page+1;
…
PUT …….. pagec=left(put(page,3.));
§ Sending output to an external file – the report output is sent put @&col2 +2 48*'-' @&col2 +50 '*';
to an external file by either specifying an external file location end;
within the FILE or FILENAME statements. For example: run;
o Using a FILE statement –
FILE ‘D:\OUTPUT\REPORT1’;
2
SUGI 27 Beginning Tutorials
In this case, the programmer assumes that all lines for one day
will fit on one page; so intelligent paging was not necessary. The if first.region then regtot=count;
output is routed to the default print file, which is the OUTPUT
else regtot+count;
window in Display Manager. When writing custom report
programs, you have to coordinate the number of lines you want to
print on each page and the number of columns with the SYSTEM In this usage, the values of REGTOT are automatically
OPTIONS PAGESIZE and LINESIZE. retained, because of the missing equal sign in the statement:
To end the HTML the END = option was used to define the
end of file.
if eof then
put @01 '</table>'
/@01 '</body>'
/@01 '</html>'
;
Figure 1 HTML Report Mockup To generate the same report using ODS, the PROC TEMPLATE
statement will be used to specify the special formatting for total
The report in Figure 1 has total lines in blue text and lines with lines (in blue) and counts below 6,000 in red.
counts below 6,000 in red. To generate the HTML for this report
PROC TEMPLATE is a procedure that can be used to specify
within the DATA _NULL_ step:
how different portions of a HTML file should be rendered in
Summarize the data and perform any other manipulations needed HTML. A set of default templates has been defined for all
before going into the last DATA step. procedures and the DATA step. In this example, PROC
Totals can be created within the DATA _NULL_ step so make TEMPLATE will be used to define custom column styles.
sure that the data is sorted appropriately. In this case, by region
and area. Note that a BY statement is needed after the SET
statement.
data _null_;
set mallard end=eof;
by region area;
Create the DATA _NULL_ step.
§ Define the file where the HTML will be written:
File ‘H:\HTML\MALCOUNT.HTM’;
§ Create the totals using a RETAIN statement1 and FIRST.
and LAST. logic1.
retain regtot;
if first.region then regtot=count;
else regtot=regtot + count;
3
SUGI 27 Beginning Tutorials
4
SUGI 27 Beginning Tutorials
5
SUGI 27 Beginning Tutorials
C='Western' generic=on;
; justify=on;
run; cellstyle _val_ < low as data
{foreground=red font_weight=bold},
/* Sort data set Mallard by region and area */ _val_ <= high as data,
6
SUGI 27 Beginning Tutorials
template='shared.cellstyle'
columns=(
char_var=reglabel(generic=on)
char_var=arealab(generic=on)
num_var=count(generic=on)
)
);
put _ods_;
run;