O'Reilly logo

SAS 9.4 Language Reference, 6th Edition by SAS Institute

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

run; 5
proc print data=investment; 6
format Capital dollar12.2; 7
run; 8
1
Begin the DATA step and create a SAS data set called Investment.
2
Calculate a value based on a $2,000 capital investment and 7% interest each year
from 1990 to 2009. Calculate variable values for one observation per iteration of the
DO loop.
3
Write each observation to data set Investment.
4
Write a note to the SAS log proving that the DATA step iterates only once.
5
Execute the DATA step.
6
To see your output, print the Investment data set with the PRINT procedure.
7
Use the FORMAT statement to write numeric values with dollar signs, commas, and
decimal points.
8
Execute the PRINT procedure.
Writing a Report with a DATA Step
Example 1: Creating a Report without Creating a Data Set
You can use a DATA step to generate a report without creating a data set by using
_NULL_ in the DATA statement. This approach saves system resources because SAS
does not create a data set. The report can contain both TITLE statements and
FOOTNOTE statements. If you use a FOOTNOTE statement, be sure to include
FOOTNOTE as an option in the FILE statement in the DATA step.
title1 'Budget Report'; 1
title2 'Mid-Year Totals by Department';
footnote 'compiled by Manager,
Documentation Development Department'; 2
data _null_; 3
set budget; 4
file print footnote; 5
MidYearTotal=Jan+Feb+Mar+Apr+May+Jun; 6
if _n_=1 then 7
do;
put @5 'Department' @30 'Mid-Year Total';
end;
put @7 Department @35 MidYearTotal; 8
run; 9
1
Define titles.
2
Define the footnote.
3
Begin the DATA step. _NULL_ specifies that no data set is created.
4
Read one observation per iteration from data set Budget.
Writing a Report with a DATA Step 419
5
Name the output file for the PUT statements and use the PRINT fileref. By default,
the PRINT fileref specifies that the file contains carriage-control characters and
titles. The FOOTNOTE option specifies that each page of output contains a footnote.
6
Calculate a value for the variable MidYearTotal on each iteration.
7
Write variable name headings for the report on the first iteration only.
8
Write the current values of variables Department and MidYearTotal for each
iteration.
9
Execute the DATA step.
The example above uses the FILE statement with the PRINT fileref to produce LISTING
output. If you want to print to a file, specify a fileref or a complete filename. Use the
PRINT option if you want the file to contain carriage-control characters and titles. The
following example shows how to use the FILE statement in this way.
file 'external-file' footnote print;
You can also use the data _null_; statement to write to an external file. For more
information about writing to external files, see the FILE statement in SAS Statements:
Reference, and the SAS documentation for your operating environment.
Example 2: Creating a Customized Report
You can create very detailed, fully customized reports by using a DATA step with PUT
statements. The following example shows a customized report that contains three
distinct sections: a header, a table, and a footer. It contains existing SAS variable values,
constant text, and values that are calculated as the report is written.
420 Chapter 18 DATA Step Processing

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required