Example 3: Assigning Attributes to Columns in ODS Output

Features:
FILE PRINT ODS statement::
OBJECTLABEL= suboption
VARIABLES= suboption
LABEL= suboption
FORMAT= suboption

PUT _ODS_ statement

Format: $CNTRY.
ODS destinations: HTML

RTF

PRINTER (PDF)

Details

This example assigns a label to the output object that it creates. It also specifies a label and a format for individual columns. This example uses filenames that might not be valid in all operating environments. To successfully run the example in your operating environment, you might need to change the file specifications. See ODS HTML Statements for Running Examples in Different Operating Environments.

Program

options pagesize=60 linesize=64 nodate pageno=1;
ods html body='your_body_file.html'
        
contents='your_contents_file.html'
        
frame='your_frame_file.html';
ods printer
file='your_postscript_file.ps';
title 'Leading Grain Producers'; 
title2 'for 1996'; 
data _null_;
         
   length Country $ 3 Type $ 5;
   format country $cntry.;
   label type='Grain';
   input Year country $ type $ Kilotons;
   if year=1996;
   file print ods= (objectlabel='1996 Grain Production'
           variables=(country
                      type(label='Type of Grain')
                      kilotons(format=comma12.))
      );
   put _ods_;
   datalines;
1995 BRZ  Wheat    1516
1995 BRZ  Rice     11236
1995 BRZ  Corn     36276
1995 CHN  Wheat    102207
1995 CHN  Rice     185226
1995 CHN  Corn     112331
1995 IND  Wheat    63007
1995 IND  Rice     122372
1995 IND  Corn     9800
1995 INS  Wheat    .
1995 INS  Rice     49860
1995 INS  Corn     8223
1995 USA  Wheat    59494
1995 USA  Rice     7888
1995 USA  Corn     187300
1996 BRZ  Wheat    3302
1996 BRZ  Rice     10035
1996 BRZ  Corn     31975
1996 CHN  Wheat    109000
1996 CHN  Rice     190100
1996 CHN  Corn     119350
1996 IND  Wheat    62620
1996 IND  Rice     120012
1996 IND  Corn     8660
1996 INS  Wheat    .
1996 INS  Rice     51165
1996 INS  Corn     8925
1996 USA  Wheat    62099
1996 USA  Rice     7771
1996 USA  Corn     236064
;
run;
ods _all_ close;

Program Description

Set the SAS system options. The NODATE option suppresses the display of the date and time in the output. The PAGENO= option specifies the starting page number. The LINESIZE= option specifies the output line length, and the PAGESIZE= option specifies the number of lines on an output page. These options affect the LISTING output, but none of them affects the HTML output.
options pagesize=60 linesize=64 nodate pageno=1;
Specify that you want to create HTML output. Also specify where to store the HTML output: the body file, the contents file, and the frame file. The ODS HTML statement opens the HTML destination and creates HTML output. The BODY= option identifies the file that contains the HTML output. The CONTENTS= option identifies the file that contains a table of contents to the HTML output. The contents file links to the body file. The FRAME= option identifies the file that integrates the table of contents, the page contents, and the body file. If you open the frame file, you see a table of contents, a table of pages, or both, as well as the body file.
ods html body='your_body_file.html'
        
contents='your_contents_file.html'
        
frame='your_frame_file.html';
Specify that you want PostScript output. Also specify where to store the PostScript output. The ODS PRINTER statement opens the PRINTER destination and creates PostScript output by default. The FILE= option sends all output objects to the external file in the current directory.
ods printer
file='your_postscript_file.ps';
Specify the titles. The TITLE statements provide titles for the output.
title 'Leading Grain Producers'; 
title2 'for 1996'; 
Begin a DATA step that does not create an output data set.Using _NULL_ saves computer resources because it prevents the DATA step from creating an output data set.
data _null_;
         
Assign lengths other than the default to two character variables. Also assign a user-defined format to one variable and a label to another. The LENGTH statement assigns lengths to COUNTRY and TYPE. The FORMAT statement assigns a format to the variable COUNTRY. The LABEL statement assigns a label to the variable TYPE.
   length Country $ 3 Type $ 5;
   format country $cntry.;
   label type='Grain';
Read a record from the input data, assign values to four variables. Continue to process only observations that match the criterion. The INPUT statement reads a single record and assigns values to four variables. The subsetting IF statement causes the DATA step to continue to process only those observations that contain the value 1996 for YEAR.
   input Year country $ type $ Kilotons;
   if year=1996;
Send the DATA step output to the open destinations, specify a label for the output object, and specify the variables to write to the data component and the order in which to write them. The combination of the fileref PRINT and the ODS option in the FILE statement sends the results of the DATA step to ODS. The LISTING, the HTML, and the PRINTER destinations are open. Because no table definition is specified, ODS uses the default DATA step definition. The OBJECTLABEL= suboption specifies the label '1996 Grain Production' to the output object. This label appears in the Results folder and in the HTML contents file. The VARIABLES= suboption specifies the variables to write to the data component and the order in which to write them. The LABEL= suboption specifies a label for the variable TYPE. The label specified here takes precedence over the LABEL statement assignment that was made previously in the DATA step, so it is used as the column heading for TYPE. The FORMAT= suboption assigns a format for the variable KILOTONS.
   file print ods= (objectlabel='1996 Grain Production'
           variables=(country
                      type(label='Type of Grain')
                      kilotons(format=comma12.))
      );
Write the variables to the buffer. The _ODS_ option in the PUT statement writes all of the variables that are defined to ODS (in the FILE PRINT ODS statement) to a special buffer. It uses default attributes for COUNTRY, and it uses any attributes specified in the VARIABLES= suboption for the other variables. For attributes that might be specified elsewhere in the DATA step but are not specified in VARIABLES=, it uses the defaults.
   put _ods_;
The data provides information about the amounts of wheat, rice, and corn that five leading grain-producing nations produced during 1995 and 1996.
   datalines;
1995 BRZ  Wheat    1516
1995 BRZ  Rice     11236
1995 BRZ  Corn     36276
1995 CHN  Wheat    102207
1995 CHN  Rice     185226
1995 CHN  Corn     112331
1995 IND  Wheat    63007
1995 IND  Rice     122372
1995 IND  Corn     9800
1995 INS  Wheat    .
1995 INS  Rice     49860
1995 INS  Corn     8223
1995 USA  Wheat    59494
1995 USA  Rice     7888
1995 USA  Corn     187300
1996 BRZ  Wheat    3302
1996 BRZ  Rice     10035
1996 BRZ  Corn     31975
1996 CHN  Wheat    109000
1996 CHN  Rice     190100
1996 CHN  Corn     119350
1996 IND  Wheat    62620
1996 IND  Rice     120012
1996 IND  Corn     8660
1996 INS  Wheat    .
1996 INS  Rice     51165
1996 INS  Corn     8925
1996 USA  Wheat    62099
1996 USA  Rice     7771
1996 USA  Corn     236064
;
run;
To view the HTML output and print the PostScript output, close both the HTML and PRINTER destinations. This statement closes the LISTING, HTML, and PRINTER destinations and all the files that are associated with them. You must close the HTML destination before you can view the output with a browser. You must close the PRINTER destination before you can print the output on a physical printer. If you do not close these destinations, then output created in subsequent sessions will be routed to them, and you might inadvertently continue to generate both HTML and PostScript output.
ods _all_ close;

Output

In this HTML frame file, the object's label, '1996 Grain Production' was supplied by the OBJECTLABEL= suboption. It appears in the table of contents as the link to the output object. In the body file, the label 'Type of Grain' that was supplied by the LABEL= suboption for the variable TYPE becomes its column heading. The format for KILOTONS was supplied by the FORMAT= suboption in the FILE statement.
HTML Frame File Produced by ODS
HTML Frame File Produced by ODS
Just as in the HTML body file and in the LISTING output, the PostScript output displays the label 'Type of Grain' that was supplied by the LABEL= suboption for the variable TYPE as its column heading. The format for KILOTONS was supplied by the FORMAT= suboption in the FILE statement.
PDF Output
PDF Output
Just as in the HTML body file and the PostScript output, the LISTING output displays the label 'Type of Grain' that was supplied by the LABEL= suboption for the variable TYPE. The format for KILOTONS was supplied by the FORMAT= suboption in the FILE statement.
RTF Output
RTF Output