Example 2: Producing ODS Output That Contains Selected Variables

Features:

FILE PRINT ODS statement:: VARIABLES= suboption

ODS PDF statement:: FILE= option

PUT _ODS_ statement

Format: $CNTRY.
ODS destinations: HTML

PRINTER (PDF)

Details

This example selects variables to include in the output. The resulting output is produced in two formats, PDF and HTML. The HTML output is produced by default, and the PDF output is requested by the ODS PDF statement. 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 nodate pageno=1 linesize=64 pagesize=60;
ods pdf
file='your-html-file.pdf';
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=(variables=(country
                              type
                              kilotons));
      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 pdf 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. None of these options affects the HTML output.
options nodate pageno=1 linesize=64 pagesize=60;
Specify that you want ODS to create PDF output and store it in the specified file. The ODS PDF statement opens the PDF destination. Any procedure or DATA step output that is created is routed to this destination (and any others that are open) and is, therefore, formatted as PDF. The FILE= option sends all output objects to the PDF file that you specify.
ods pdf
file='your-html-file.pdf';
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 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 whatever ODS destinations are open. Specify the variables and their order in the data component that is created. The combination of the fileref PRINT and the ODS option in the FILE statement sends the results of the DATA step to ODS. Two ODS destinations, the PDF and the HTML destinations, are open. Because no table definition is specified, ODS uses the default DATA step definition. The VARIABLES= suboption specifies that the resulting data component will contain three columns in the order that is listed.
   file print ods=(variables=(country
                              type
                              kilotons));
Write values for all variables that are specified with the VARIABLES= suboption in the FILE statement. The _ODS_ option in the PUT statement writes variable values to the data component. It writes only those variables that were specified with the VARIABLES= suboption in the FILE statement. Because no formats or labels are specified for these ODS columns, ODS uses the defaults.
      put _ods_;
The data provides information about the amounts of wheat, rice, and corn that were produced by the five leading grain-producing nations 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;
Close the PDF destination so that you can view the output. The ODS PDF statement closes the PDF destination and all the files that are associated with it. You must close the destination before you can view the output. Also, closing the destination prevents all subsequent ODS jobs from automatically producing PDF output.
ods pdf close;

Output

HTML Body File Produced by ODS
HTML Body File Produced by ODS
PDF Output
PDF Output