Examples


Example 1: Creating a Report with the DATA Step and the Default Table Definition

ODS features:

  • FILE PRINT ODS statement:

  • PUT _ODS_ statement

ODS destinations:

  • LISTING

This example uses the DATA step and ODS to create a listing report. It uses the default table definition (template) for the DATA step and writes an output object to the LISTING destination (the default).

Program

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.

 options nodate pageno=1 linesize=64 pagesize=60; 

Specify a title. The TITLE statement specifies a title for the output.

 title 'Leading Grain Producers'; 

Create a user -defined format. PROC FORMAT creates the format $CNTRY. for the variable COUNTRY.

 proc format;     value $cntry 'BRZ'='Brazil'                  'CHN'='China'                  'IND'='India'                  'INS'='Indonesia'                  'USA'='United States';  run; 

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_; 

Define variables, assign lengths and formats, read a record, and assign values to four variables . The LENGTH statement defines a length that is shorter than the default to two character variables. The FORMAT statement assigns a user-defined format to the variable COUNTRY. The LABEL statement assigns a label to the variable TYPE. The INPUT statement reads a record from the datalines and assigns a value to four variables.

 length Country $ 3 Type $ 5;  format country $cntry.;  label type='Grain';  input Year country $ type $ Kilotons; 

Use the default table definition (template) to create simple listing output. The combination of the fileref PRINT and the ODS option in the FILE statement routes the DATA step output to ODS. The only open ODS destination is the LISTING destination, which is open by default when you begin your SAS session. Because no suboptions are specified, ODS uses the default DATA step table definition (template). This FILE PRINT ODS statement creates an output object and binds it to the default template.

 file print ods; 

Write the variables to the data component. The _ODS_ option in the PUT statement writes every variable to the buffer that the PUT statement writes to the data component. Because no formats or labels are specified for individual columns , ODS uses the defaults.

 put _ods_; 

The data provide information on 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  ; 
Listing Output
Output 3.1: Listing Output Created with the Default DATA Step Table Definition
start example

The default table definition produces a column for each variable in the DATA step. The order of the columns is determined by their order in the program data vector. Because no attributes are specified for individual columns, ODS uses the default column headers and formats.

 Leading Grain Producers                        1     Country          Grain        Year             Kilotons  Brazil              Wheat            1995               1516  Brazil              Rice             1995              11236  Brazil              Corn             1995              36276  China               Wheat            1995             102207  China               Rice             1995             185226  China               Corn             1995             112331  India               Wheat            1995              63007  India               Rice             1995             122372  India               Corn             1995               9800  Indonesia           Wheat            1995                  .  Indonesia           Rice             1995              49860  Indonesia           Corn             1995               8223  United States       Wheat            1995              59494  United States       Rice             1995               7888  United States       Corn             1995             187300  Brazil              Wheat            1996               3302  Brazil              Rice             1996              10035  Brazil              Corn             1996              31975  China               Wheat            1996             109000  China               Rice             1996             190100  China               Corn             1996             119350  India               Wheat            1996              62620  India               Rice             1996             120012  India               Corn             1996               8660  Indonesia           Wheat            1996                  .  Indonesia           Rice             1996              51165  Indonesia           Corn             1996               8925  United States       Wheat            1996              62099  United States       Rice             1996               7771  United States       Corn             1996             236064 
end example
 

Example 2: Producing ODS Output That Contains Selected Variables

ODS features:

  • FILE PRINT ODS statement:

    • VARIABLES= suboption

  • ODS HTML statement:

  • BODY= option

    • URL= suboption

  • PUT _ODS_ statement

ODS destinations:

  • HTML

  • LISTING

Format:

  • $CNTRY. on page 42

This example selects variables to include in the output. The resulting output is produced in two formats, listing and HTML. The listing output is produced by default, and the HTML output is requested by the ODS HTML statement.

Note: 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 Appendix 3, 'ODS HTML Statements for Running Examples in Different Operating Environments,' on page 649.

Program

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 HTML output and store it in the specified file. The ODS HTML statement opens the HTML destination; any procedure or DATA step output created will be routed to this destination (and any others that are open) and will, therefore, format the output in HTML. The BODY= option sends all output objects to the HTML file that you specify. Some browsers require an extension of HTM or HTML on the filename.

 ods html body='  your-html-file.html  '; 

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 meet 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 LISTING 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 on 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  ; 

Close the HTML destination so that you can view the output. The ODS HTML statement closes the HTML destination and all the files that are associated with it. You must close the destination before you can view the output with a browser. Also, closing the destination prevents all subsequent ODS jobs from automatically producing HTML output.

 ods html close; 
HTML Output
click to expand
Display 3.1: HTML Body File Produced by ODS
Listing Output
Output 3.2: Listing Output Produced by the LISTING Destination
start example
 Leading Grain Producers                  1                 for 1996     Country       Grain      Kilotons  Brazil           Wheat            3302  Brazil           Rice            10035  Brazil           Corn            31975  China            Wheat          109000  China            Rice           190100  China            Corn           119350  India            Wheat           62620  India            Rice           120012  India            Corn             8660  Indonesia        Wheat               .  Indonesia        Rice             1165  Indonesia        Corn             8925  United States    Wheat           62099  United States    Rice             7771  United States    Corn           236064 
end example
 

Example 3: Assigning Attributes to Columns in ODS Output

ODS features:

  • FILE PRINT ODS statement:

    • OBJECTLABEL= suboption

    • VARIABLES= suboption

      • LABEL= suboption

      • FORMAT= suboption

  • PUT _ODS_ statement

ODS destinations:

  • HTML

  • Listing

  • Printer (PS)

Format:

  • $CNTRY. on page 42

This example assigns a label to the output object that it creates. It also specifies a label and a format for individual columns.

Note: 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 Appendix 3, 'ODS HTML Statements for Running Examples in Different Operating Environments,' on page 649.

Program

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 thatintegrates the tableof 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 meet 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 header 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 on 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  ; 

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; 
HTML 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 header.

The format for KILOTONS was supplied by the FORMAT= suboption in the FILE statement.

click to expand
Display 3.2: HTML Frame File Produced by ODS
Printer Output

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 header.

The format for KILOTONS was supplied by the FORMAT= suboption in the FILE statement.

click to expand
Display 3.3: Printer Output Viewed with Ghostview
Listing 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.

 Leading Grain Producers                    1                 for 1996                   Type                    of     Country       Grain      Kilotons  Brazil           Wheat           3,302  Brazil           Rice           10,035  Brazil           Corn           31,975  China            Wheat         109,000  China            Rice          190,100  China            Corn          119,350  India            Wheat          62,620  India            Rice          120,012  India            Corn            8,660  Indonesia        Wheat               .  Indonesia        Rice           51,165  Indonesia        Corn            8,925  United States    Wheat          62,099  United States    Rice            7,771  United States    Corn          236,064 

Example 4: Creating and Using a User-Defined Table Definition Template

ODS features:

  • PROC TEMPLATE

  • FILE PRINT ODS statement:

    • COLUMNS= suboption:

      • FORMAT= suboption

      • DYNAMIC= suboption

      • GENERIC= suboption

    • TEMPLATE=

  • PUT _ODS_ statement:

    • column pointer controls

    • line pointer controls

ODS destination:

  • RTF

This example shows how to:

  • create a simple user-defined template (table definition) with PROC TEMPLATE

  • use a simple user-defined template in the DATA step

  • use pointer controls in the PUT _ODS_ statement.

Note: 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 Appendix 3, 'ODS HTML Statements for Running Examples in Different Operating Environments,' on page 649.

Program: Creating the User-Defined Table Definition (Template)

Define the table definition PHONELIST . This PROC TEMPLATE step defines a table definition named PHONELIST.

The template defines two columns: NAME and PHONE.

The GENERIC=ON attribute defines the column for NAME as one that the DATA step can use for multiple variables.

The column definition uses dynamic headers; that is, a variable that uses this column definition takes the value of the header at run time from the DATA step that uses this template. Thus, each variable can have a different column header.

The STYLE= attribute specifies that the style element DATA be used as the basis for generating the data in this column. The font face and font size that DATA normally uses are replaced by the ones that are specified in the STYLE= attribute.

The header for PHONE is hard-coded as Telephone. The STYLE= attribute specifies a style element to use for the data in this column. For information on PROC TEMPLATE, see Chapter 7, 'TEMPLATE Procedure: Overview,' on page 261.

 proc template;  define table phonelist;        column name phone;        dynamic colheader;     define name;        generic=on;        header=colheader;        style=data{font_style=italic font_size=5};     end;     define phone;        header='Telephone';        style=datafixed;     end;  end;  run; 
Program: Using the User-Defined Template (Table Definition)

Specify that you do not want to produce the default listing output. The ODS LISTING CLOSE statement closes the listing destination to conserve resources. The listing destination is open by default when you open your SAS session.

 ods listing close; 

Specify that you want the output formatted in RTF. The ODS RTF statement opens the RTF destination and creates RTF output for use by Microsoft Word. Subsequent output objects are sent to the body file.

 ods rtf body='  your_rtf_file.rtf  '; 

Specify a title. The TITLE statement provides a title for the output.

 title 'New Subscriber Telephone List'; 

Create a format for telephone numbers . PROC FORMAT creates a user-defined format for telephone numbers.

 proc format;     picture phonenum .='Not available'                  other='0000)000   0000' (prefix='(');  run; 

Create the PHONES data set. The data set PHONES contains names and their corresponding phone numbers. Some observations contain missing values for the business or home phone numbers.

 data phones;     length first_name  last_name ;     input first_name $ last_name $ business_phone home_phone;     datalines;  Jerome Johnson 9193191677 9198462198  Romeo Montague 8008992164 3609736201  Imani Rashid 5088522146 5083669821  Palinor Kent . 9197823199  Ruby Archuleta . .  Takei Ito 7042982145 .  Tom Joad 2099632764 2096684741  ; 

Sort the PHONES data set by last name. PROC SORT sorts the data set PHONES by LAST_NAME and replaces the original data set with the sorted data set.

 proc sort data=phones;     by last_name;  run; 

Begin a DATA step that does not create an output data set. Read an observation from the PHONES data set. Using _NULL_ saves computer resources because it prevents the DATA step from creating an output data set.

 data _null_;     set phones; 

Request that ODS output be created and use the template named PHONELIST. The combination of the fileref PRINT and the ODS option in the FILE statement sends the results of the DATA step to ODS. ODS creates an output object and binds it to the PHONELIST template. Only RTF output is created because only the RTF destination is open.

The TEMPLATE= suboption tells ODS to use the template PHONELIST, which was created previously in the PROC TEMPLATE step.

 file print ods=(template='phonelist' 

Place variable values in columns. The COLUMNS= suboption places values of variables into columns that are defined in the template.

Values for both the LAST_NAME and FIRST_NAME variables are written to columns that are defined as NAME in the template.

The GENERIC=ON suboption must be set in both the template and the ODS= option in order for you to use a column definition for more than one column.

The value of the variable BUSINESS_PHONE is placed in a column that is defined as PHONE.

The DYNAMIC= suboption assigns a value to the variable COLHEADER. This value is passed to the template when the output object is created, and the template uses it for the column header. Thus, even though the variables use the same column definition from the template, the columns in the output object have different column headers.

The FORMAT= suboption assigns the format PHONENUM. to the column named PHONE.

 columns=            (name=last_name              (generic=on               dynamic=(colheader='Last Name'))             name=first_name              (generic=on               dynamic=(colheader='First Name'))             phone=business_phone               (format=phonenum.))); 

The following IF/THEN-ELSE statements execute a different PUT _ODS_ statement based on the specified conditions:

  • If BUSINESS_PHONE contains missing values, then the PUT statement writes values for LAST_NAME, FIRST_NAME, and BUSINESS_PHONE (the columns that are defined in the ODS= option) into the output buffer. The PUT statement then writes the value for HOME_PHONE in column 3, overwriting the missing value of BUSINESS_PHONE.

  • If HOME_PHONE contains a missing value, then the PUT statement simply writes values for LAST_NAME, FIRST_NAME, and BUSINESS_PHONE to the buffer.

  • Finally, if both phone numbers have values, then the PUT statement writes values for LAST_NAME, FIRST_NAME, and BUSINESS_PHONE to the buffer in the first line. SAS then goes to the next line (as directed by the line pointer control /) and writes the value of HOME_PHONE in the third column of the next line.

 if (missing(business_phone)) then     put _ods_ @3 home_phone;  else if (missing(home_phone)) then     put _ods_;  else     put _ods_ / @3 home_phone;  run; 

Close the RTF destination so that you can view the output. The ODS RTF statement closes the RTF destination and all the files that are associated with it. You must close the destination before you can view the output in Microsoft Word. Also, closing the output prevents all subsequent ODS jobs from automatically producing RTF output.

 ods RTF close; 
RTF Output
click to expand
Display 3.4: RTF Output Viewed with Microsoft Word



SAS 9.1 Output Delivery System. Users Guide
SAS 9.1 Output Delivery System Users Guide
ISBN: 1590472187
EAN: 2147483647
Year: 2004
Pages: 99
Authors: SAS Institute

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net