Examples: FORMAT Procedure


Several examples in this section use the PROCLIB.STAFF data set. In addition, many of the informats and formats that are created in these examples are stored in LIBRARY.FORMATS. The output data set shown in Output Control Data Set on page 458 contains a description of these informats and the formats.

 libname proclib '  SAS-data-library  '; 

Create the data set PROCLIB.STAFF. The INPUT statement assigns the names Name , IdNumber, Salary, Site, and HireDate to the variables that appear after the DATALINES statement. The FORMAT statement assigns the standard SAS format DATE7. to the variable HireDate.

 data proclib.staff;     input Name & . IdNumber $ Salary           Site $ HireDate date7.;     format hiredate date7.;     datalines;  Capalleti, Jimmy  2355 21163 BR1 30JAN79  Chen, Len         5889 20976 BR1 18JUN76  Davis, Brad       3878 19571 BR2 20MAR84  Leung, Brenda     4409 34321 BR2 18SEP74  Martinez, Maria   3985 49056 US2 10JAN93  Orfali, Philip    0740 50092 US2 16FEB83  Patel, Mary       2398 35182 BR3 02FEB90  Smith, Robert     5162 40100 BR5 15APR86  Sorrell, Joseph   4421 38760 US1 19JUN93  Zook, Carla       7385 22988 BR3 18DEC91  ; 

The variables are about a small subset of employees who work for a corporation that has sites in the U.S. and Britain. The data contain the name, identification number, salary (in British pounds ), location, and date of hire for each employee.

Example 1: Creating a Picture Format

Procedure features:

  • PROC FORMAT statement options:

    • LIBRARY=

  • PICTURE statement options:

    • MULT=

    • PREFIX=

  • LIBRARY libref

  • LOW and HIGH keywords

Data set:

  • PROCLIB.STAFF on page 463.

This example uses a PICTURE statement to create a format that prints the values for the variable Salary in the data set PROCLIB.STAFF in U.S. dollars.

Program

Assign two SAS library references (PROCLIB and LIBRARY). Assigning a library reference LIBRARY is useful in this case because if you use PROC FORMAT, then SAS automatically searches for informats and formats in any library that is referenced with the LIBRARY libref.

 libname proclib '  SAS-data-library-1  ';  libname library '  SAS-data-library-2  '; 

Set the SAS system options. The NODATE option suppresses the display of the date and time in the output. PAGENO= specifies the starting page number. LINESIZE= specifies the output line length, and PAGESIZE= specifies the number of lines on an output page.

 options nodate pageno=1 linesize=80 pagesize=40; 

Specify that user -defined formats will be stored in the catalog LIBRARY.FORMATS. The LIBRARY= option specifies a SAS catalog that will contain the formats or informats that you create with PROC FORMAT. When you create the library named LIBRARY, SAS automatically creates a catalog named FORMATS inside LIBRARY.

 proc format library=library; 

Define the USCurrency. picture format. The PICTURE statement creates a template for printing numbers . LOW-HIGH ensures that all values are included in the range. The MULT= statement option specifies that each value is multiplied by 1.61. The PREFIX= statement adds a US dollar sign to any number that you format. The picture contains six digit selectors, five for the salary and one for the dollar sign prefix.

 picture uscurrency low-high='000,000' (mult=1.61 prefix='$');  run; 

Print the PROCLIB.STAFF data set. The NOOBS option suppresses the printing of observation numbers. The LABEL option uses variable labels instead of variable names for column headings.

 proc print data=proclib.staff noobs label; 

Specify a label and format for the Salary variable. The LABEL statement substitutes the specific label for the variable in the report. In this case, Salary in US Dollars is substituted for the variable Salary for this print job only. The FORMAT statement associates the USCurrency. format with the variable name Salary for the duration of this procedure step.

 label salary='Salary in U.S. Dollars';  format salary uscurrency.; 

Specify the title.

 title 'PROCLIB.STAFF with a Format for the Variable Salary';  run; 

Output

 PROCLIB.STAFF with a Format for the Variable Salary              1                                Salary in                        Id        U.S.                  Hire  Name                Number     Dollars     Site       Date  Capalleti, Jimmy     2355      ,072     BR1     30JAN79  Chen, Len            5889      ,771     BR1     18JUN76  Davis, Brad          3878      ,509     BR2     20MAR84  Leung, Brenda        4409      ,256     BR2     18SEP74  Martinez, Maria      3985      ,980     US2     10JAN93  Orfali, Philip       0740      ,648     US2     16FEB83  Patel, Mary          2398      ,643     BR3     02FEB90  Smith, Robert        5162      ,561     BR5     15APR86  Sorrell, Joseph      4421      ,403     US1     19JUN93  Zook, Carla          7385      ,010     BR3     18DEC91 

Example 2: Creating a Format for Character Values

Procedure features:

  • VALUE statement

    • OTHER keyword

Data set:

  • PROCLIB.STAFF on page 463.

Format: USCurrency. on page 465

This example uses a VALUE statement to create a character format that prints a value of a character variable as a different character string.

Program

Assign two SAS library references (PROCLIB and LIBRARY). Assigning a library reference LIBRARY is useful in this case because if you use PROC FORMAT, then SAS automatically searches for informats and formats in any library that is referenced with the LIBRARY libref.

 libname proclib '  SAS-data-library-1  ';  libname library '  SAS-data-library-2  '; 

Set the SAS system options. The NODATE option suppresses the display of the date and time in the output. PAGENO= specifies the starting page number. LINESIZE= specifies the output line length, and PAGESIZE= specifies the number of lines on an output page.

 options nodate pageno=1 linesize=80 pagesize=40; 

Create the catalog named LIBRARY.FORMATS, where the user-defined formats will be stored . The LIBRARY= option specifies a permanent storage location for the formats that you create. It also creates a catalog named FORMAT in the specified library. If you do not use LIBRARY=, then SAS temporarily stores formats and informats that you create in a catalog named WORK.FORMATS.

 proc format library=library; 

Define the $CITY. format. The special codes BR1, BR2, and so on, are converted to the names of the corresponding cities. The keyword OTHER specifies that values in the data set that do not match any of the listed city code values are converted to the value INCORRECT CODE .

 value $city 'BR1'='Birmingham UK'                 'BR2'='Plymouth UK'                 'BR3'='York UK'                 'US1'='Denver USA'                 'US2'='Miami USA'                 other='INCORRECT CODE';  run; 

Print the PROCLIB.STAFF data set. The NOOBS option suppresses the printing of observation numbers. The LABEL option uses variable labels instead of variable names for column headings.

 proc print data=proclib.staff noobs label; 

Specify a label for the Salary variable. The LABEL statement substitutes the label Salary in U.S. Dollars for the name SALARY.

 label salary='Salary in U.S. Dollars'; 

Specify formats for Salary and Site. The FORMAT statement temporarily associates the USCurrency. format (created in Example 1 on page 464) with the variable SALARY and also temporarily associates the format $CITY. with the variable SITE.

 format salary uscurrency. site $city.; 

Specify the titles.

 title 'PROCLIB.STAFF with a Format for the Variables';     title2 'Salary and Site';  run; 

Output

 PROCLIB.STAFF with a Format for the Variables                1                            Salary and Site                                Salary in                        Id        U.S.                            Hire  Name                Number     Dollars     Site                 Date  Capalleti, Jimmy     2355      ,072     Birmingham UK     30JAN79  Chen, Len            5889      ,771     Birmingham UK     18JUN76  Davis, Brad          3878      ,509     Plymouth UK       20MAR84  Leung, Brenda        4409      ,256     Plymouth UK       18SEP74  Martinez, Maria      3985      ,980     Miami USA         10JAN93  Orfali, Philip       0740      ,648     Miami USA         16FEB83  Patel, Mary          2398      ,643     York UK           02FEB90  Smith, Robert        5162      ,561     INCORRECT CODE    15APR86  Sorrell, Joseph      4421      ,403     Denver USA        19JUN93  Zook, Carla          7385      ,010     York UK           18DEC91 

Example 3: Writing a Format for Dates Using a Standard SAS Format

Procedure features:

  • VALUE statement:

    • HIGH keyword

Data set:

  • PROCLIB.STAFF on page 463.

Formats:

  • USCurrency. on page 465 and $CITY. on page 467.

This example uses an existing format that is supplied by SAS as a formatted value. Tasks include

  • creating a numeric format

  • nesting formats

  • writing a format using a standard SAS format

  • formatting dates.

Program

This program defines a format called BENEFIT, which differentiates between employees hired on or before 31DEC1979. The purpose of this program is to indicate any employees who are eligible to receive a benefit, based on a hire date on or prior to December 31, 1979. All other employees with a later hire date are listed as ineligible for the benefit.

Assign two SAS library references (PROCLIB and LIBRARY). Assigning a library reference LIBRARY is useful in this case because if you use PROC FORMAT, then SAS automatically searches for informats and formats in any library that is referenced with the LIBRARY libref.

 libname proclib '  SAS-data-library-1  ';  libname library '  SAS-data-library-2  '; 

Set the SAS system options. The NODATE option suppresses the display of the date and time in the output. PAGENO= specifies the starting page number. LINESIZE= specifies the output line length, and PAGESIZE= specifies the number of lines on an output page.

 options nodate pageno=1 linesize=80 pagesize=40; 

Store the BENEFIT. format in the catalog LIBRARY.FORMATS. The LIBRARY= option specifies the permanent storage location LIBRARY for the formats that you create. If you do not use LIBRARY=, then SAS temporarily stores formats and informats that you create in a catalog named WORK.FORMATS.

 proc format library=library; 

Define the first range in the BENEFIT. format. This first range differentiates between the employees who were hired on or before 31DEC1979 and those who were hired after that date. The keyword LOW and the SAS date constant 31DEC1979 D create the first range, which includes all date values that occur on or before December 31, 1979. For values that fall into this range, SAS applies the WORDDATE w . format. [*]

 value benefit low-'31DEC1979'd=[worddate20.] 

Define the second range in the BENEFIT. format. The second range consists of all dates on or after January 1, 1980. The SAS date constant 01JAN1980 D and the keyword HIGH specify the range. Values that fall into this range receive ** Not Eligible ** as a formatted value.

 '01JAN1980'd-high='  ** Not Eligible **';  run; 

Print the data set PROCLIB.STAFF. The NOOBS option suppresses the printing of observation numbers. The LABEL option uses variable labels instead of variable names for column headings.

 proc print data=proclib.staff noobs label; 

Specify a label for the Salary variable. The LABEL statement substitutes the label Salary in U.S. Dollars for the name SALARY.

 label salary='Salary in U.S. Dollars'; 

Specify formats for Salary, Site, and Hiredate. The FORMAT statement associates the USCurrency. format (created in Example 1 on page 464) with SALARY, the $CITY. format (created in Example 2 on page 466) with SITE, and the BENEFIT. format with HIREDATE.

 format salary uscurrency. site $city. hiredate benefit.; 

Specify the titles.

 title 'PROCLIB.STAFF with a Format for the Variables';     title2 'Salary, Site, and HireDate';  run; 

Output

 PROCLIB.STAFF with a Format for the Variables                1                             Salary, Site, and HireDate                                Salary in                        Id        U.S.  Name                Number     Dollars     Site                  HireDate  Capalleti, Jimmy     2355      ,072     Birmingham UK      January 30, 1979  Chen, Len            5889      ,771     Birmingham UK         June 18, 1976  Davis, Brad          3878      ,509     Plymouth UK      ** Not Eligible **  Leung, Brenda        4409      ,256     Plymouth UK      September 18, 1974  Martinez, Maria      3985      ,980     Miami USA        ** Not Eligible **  Orfali, Philip       0740      ,648     Miami USA        ** Not Eligible **  Patel, Mary          2398      ,643     York UK          ** Not Eligible **  Smith, Robert        5162      ,561     INCORRECT CODE   ** Not Eligible **  Sorrell, Joseph      4421      ,403     Denver USA       ** Not Eligible **  Zook, Carla          7385      ,010     York UK          ** Not Eligible ** 

Example 4: Converting Raw Character Data to Numeric Values

Procedure feature:

  • INVALUE statement

This example uses an INVALUE statement to create a numeric informat that converts numeric and character raw data to numeric data.

Program

This program converts quarterly employee evaluation grades, which are alphabetic, into numeric values so that reports can be generated that sum the grades up as points.

Set up two SAS library references, one named PROCLIB and the other named LIBRARY.

 libname proclib '  SAS-data-library-1  ';  libname library '  SAS-data-library-2  '; 

Set the SAS system options. The NODATE option suppresses the display of the date and time in the output. PAGENO= specifies the starting page number. LINESIZE= specifies the output line length, and PAGESIZE= specifies the number of lines on an output page.

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

Store the Evaluation. informat in the catalog LIBRARY.FORMATS.

 proc format library=library; 

Create the numeric informat Evaluation. The INVALUE statement converts the specified values. The letters O (Outstanding), S (Superior), E (Excellent), C (Commendable), and N (None) correspond to the numbers 4, 3, 2, 1, and 0, respectively.

 invalue evaluation 'O'=4                        'S'=3                        'E'=2                        'C'=1                        'N'=0;  run; 

Create the PROCLIB.POINTS data set. The instream data, which immediately follows the DATALINES statement, contains a unique identification number (EmployeeId) and bonus evaluations for each employee for each quarter of the year (Q1 “Q4). Some of the bonus evaluation values that are listed in the data lines are numbers; others are character values. Where character values are listed in the data lines, the Evaluation. informat converts the value O to 4, the value S to 3, and so on. The raw data values 0 through 4 are read as themselves because they are not referenced in the definition of the informat. Converting the letter values to numbers makes it possible to calculate the total number of bonus points for each employee for the year. TotalPoints is the total number of bonus points.

 data proclib.points;     input EmployeeId $ (Q1-Q4) (evaluation.,+1);     TotalPoints=sum(of q1-q4);     datalines;  2355 S O O S  5889 2 2 2 2  3878 C E E E  4409 0 1 1 1  3985 3 3 3 2  0740 S E E S  2398 E E C C  5162 C C C E  4421 3 2 2 2  7385 C C C N  ; 

Print the PROCLIB.POINTS data set. The NOOBS option suppresses the printing of observation numbers.

 proc print data=proclib.points noobs; 

Specify the title.

 title 'The PROCLIB.POINTS Data Set';  run; 

Output

 The PROCLIB.POINTS Data Set                     1  Employee                                Total     Id          Q1    Q2    Q3    Q4    Points    2355          3     4     4     3      14    5889          2     2     2     2       8    3878          1     2     2     2       7    4409          0     1     1     1       3    3985          3     3     3     2      11    0740          3     2     2     3      10    2398          2     2     1     1       6    5162          1     1     1     2       5    4421          3     2     2     2       9    7385          1     1     1     0       3 

Example 5: Creating a Format from a Data Set

Procedure features:

  • PROC FORMAT statement option:

    • CNTLIN=

  • Input control data set

Data set:

  • WORK.POINTS, created from data lines in the sample code.

This example shows how to create a format from a SAS data set. Tasks include

  • creating a format from an input control data set

  • creating an input control data set from an existing SAS data set.

Program

Set the SAS system options. The NODATE option suppresses the display of the date and time in the output. PAGENO= specifies the starting page number. LINESIZE= specifies the output line length, and PAGESIZE= specifies the number of lines on an output page.

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

Create a temporary data set named scale. The first two variables in the data lines, called BEGIN and END, will be used to specify a range in the format. The third variable in the data lines, called AMOUNT, contains a percentage that will be used as the formatted value in the format. Note that all three variables are character variables as required for PROC FORMAT input control data sets.

 data scale;     input begin $ 1-2  end $ 5-8 amount $ 10-12;     datalines;  0   3    0%  4   6    3%  7   8    6%  9   10   8%  11  16   10%  ; 

Create the input control data set CTRL and set the length of the LABEL variable. The LENGTH statement ensures that the LABEL variable is long enough to accommodate the label ***ERROR*** .

 data ctrl;     length label $ 11; 

Rename variables and create an end-of-file flag. The data set CTRL is derived from WORK.SCALE. RENAME= renames BEGIN and AMOUNT as START and LABEL, respectively. The END= option creates the variable LAST, whose value is set to 1 when the last observation is processed .

 set scale(rename=(begin=start amount=label)) end=last; 

Create the variables FMTNAME and TYPE with fixed values. The RETAIN statement is more efficient than an assignment statement in this case. RETAIN retains the value of FMTNAME and TYPE in the program data vector and eliminates the need for the value to be written on every iteration of the DATA step. FMTNAME specifies the name PercentageFormat, which is the format that the input control data set creates. The TYPE variable specifies that the input control data set will create a numeric format.

 retain fmtname 'PercentageFormat' type 'n'; 

Write the observation to the output data set.

 output; 

Create an other category. Because the only valid values for this application are 0 “16, any other value (such as missing) should be indicated as an error to the user. The IF statement executes only after the DATA step has processed the last observation from the input data set. When IF executes, HLO receives a value of O to indicate that the range is OTHER, and LABEL receives a value of ***ERROR*** . The OUTPUT statement writes these values as the last observation in the data set. HLO has missing values for all other observations.

 if last then do;        hlo='O';        label='***ERROR***';        output;     end;  run; 

Print the control data set, CTRL. The NOOBS option suppresses the printing of observation numbers.

 proc print data=ctrl noobs; 

Specify the title.

 title 'The CTRL Data Set';  run; 

Note that although the last observation contains values for START and END, these values are ignored because of the O value in the HLO variable.

 The CTRL Data Set                               1  label          start    end        fmtname        type    hlo  0%              0       3      PercentageFormat    n  3%              4       6      PercentageFormat    n  6%              7       8      PercentageFormat    n  8%              9       10     PercentageFormat    n  10%             11      16     PercentageFormat    n  ***ERROR***     11      16     PercentageFormat    n      O 

Store the created format in the catalog WORK.FORMATS and specify the source for the format. The CNTLIN= option specifies that the data set CTRL is the source for the format PTSFRMT.

 proc format library=work cntlin=ctrl;  run; 

Create the numeric informat Evaluation. The INVALUE statement converts the specified values. The letters O (Outstanding), S (Superior), E (Excellent), C (Commendable), and N (None) correspond to the numbers 4, 3, 2, 1, and 0, respectively.

 proc format;     invalue evaluation 'O'=4                        'S'=3                        'E'=2                        'C'=1                        'N'=0;  run; 

Create the WORK.POINTS data set. The instream data, which immediately follows the DATALINES statement, contains a unique identification number (EmployeeId) and bonus evaluations for each employee for each quarter of the year (Q1 “Q4). Some of the bonus evaluation values that are listed in the data lines are numbers; others are character values. Where character values are listed in the data lines, the Evaluation. informat converts the value O to 4, the value S to 3, and so on. The raw data values 0 through 4 are read as themselves because they are not referenced in the definition of the informat. Converting the letter values to numbers makes it possible to calculate the total number of bonus points for each employee for the year. TotalPoints is the total number of bonus points. The addition operator is used instead of the SUM function so that any missing value will result in a missing value for TotalPoints.

 data points;     input EmployeeId $ (Q1-Q4) (evaluation.,+1);     TotalPoints=q1+q2+q3+q4;     datalines;  2355 S O O S  5889 2 . 2 2  3878 C E E E  4409 0 1 1 1  3985 3 3 3 2  0740 S E E S  2398 E E   C  5162 C C C E  4421 3 2 2 2  7385 C C C N  ; 

Generate a report for WORK.POINTS and associate the PTSFRMT. format with the TotalPoints variable. The DEFINE statement performs the association. The column that contains the formatted values of TotalPoints is using the alias Pctage. Using an alias enables you to print a variable twice, once with a format and once with the default format. See Chapter 43, The REPORT Procedure, on page 861 for more information about PROC REPORT.

 proc report data=work.points nowd headskip split='#';     column employeeid totalpoints totalpoints=Pctage;     define employeeid / right;     define totalpoints / 'Total#Points' right;     define pctage / format=PercentageFormat12. 'Percentage' left;     title 'The Percentage of Salary for Calculating Bonus';  run; 

Output

Output 23.3
start example
 The Percentage of Salary for Calculating Bonus               1        Employee      Total              Id     Points  Percentage            2355         14  10%            5889          .  ***ERROR***            3878          7  6%            4409          3  0%            3985         11  10%            0740         10  8%            2398          .  ***ERROR***            5162          5  3%            4421          9  8%            7385          3  0% 
end example
 

Example 6: Printing the Description of Informats and Formats

Procedure features:

  • PROC FORMAT statement option:

    • FMTLIB

  • SELECT statement

Format:

  • NOZEROS on page 444.

Informat:

  • Evaluation. on page 471

This example illustrates how to print a description of an informat and a format. The description shows the values that are input and output.

Program

Set up a SAS library reference named LIBRARY.

 libname library '  SAS-data-library  '; 

Set the SAS system options. The NODATE option suppresses the display of the date and time in the output. PAGENO= specifies the starting page number. LINESIZE= specifies the output line length, and PAGESIZE= specifies the number of lines on an output page.

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

Print a description of Evaluation. and NOZEROS. The FMTLIB option prints information about the formats and informats in the catalog that the LIBRARY= option specifies. LIBRARY=LIBRARY points to the LIBRARY.FORMATS catalog.

 proc format library=library fmtlib; 

Select an informat and a format. The SELECT statement selects EVAL and NOZEROS, which were created in previous examples. The at sign (@) in front of EVAL indicates that EVAL. is an informat.

 select @evaluation nozeros; 

Specify the titles.

 title 'FMTLIB Output for the NOZEROS. Format and the';     title2 'Evaluation. Informat';  run; 

Output

The output is described in Procedure Output on page 461.

 FMTLIB Output for the NOZEROS. Format and the                 1                              Evaluation. Informat  ----------------------------------------------------------------------------        FORMAT NAME: NOZEROS  LENGTH:   5   NUMBER OF VALUES:   4               MIN LENGTH:   1  MAX LENGTH:  40 DEFAULT LENGTH   5  FUZZ: STD           --------------------------------------------------------------------------  START           END             LABEL (VER. V7V8   10APR2002:18:55:08)   ----------------+----------------+----------------------------------------  LOW                           -100.00              P-  F  M100                          -1<               0<99                 P-. F  M100                           0               1<99                 P.  F  M100                           1HIGH            00.00              P   F  M100            ---------------------------------------------------------------------------- ----------------------------------------------------------------------------                    INFORMAT NAME: @EVALUATION LENGTH:1                         MIN LENGTH:   1  MAX LENGTH:  40  DEFAULT LENGTH  1 FUZZ: 0              --------------------------------------------------------------------------  START           END             INVALUE(VER. 9.00   10APR2002:18:55:11)   ----------------+----------------+----------------------------------------  C               C                                                      1  E               E                                                      2  N               N                                                      0  O               O                                                      4  S               S                                                      3  ---------------------------------------------------------------------------- 

Example 7: Retrieving a Permanent Format

Procedure features:

  • PROC FORMAT statement options:

    • LIBRARY=

Other features:

  • FMTSEARCH= system option

Data sets:

  • SAMPLE on page 443.

This example uses the LIBRARY= option and the FMTSEARCH= system option to store and retrieve a format stored in a catalog other than WORK.FORMATS or LIBRARY.FORMATS.

Program

Set up a SAS library reference named PROCLIB.

 libname proclib '  SAS-data-library  '; 

Set the SAS system options. The NODATE option suppresses the display of the date and time in the output. PAGENO= specifies the starting page number. LINESIZE= specifies the output line length, and PAGESIZE= specifies the number of lines on an output page.

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

Store the NOZEROS. format in the PROCLIB.FORMATS catalog.

 proc format library=proclib; 

Create the NOZEROS. format. The PICTURE statement defines the picture format NOZEROS. See Building a Picture Format: Step by Step on page 443.

 picture nozeros                low  -    -1 = '00.00' (prefix='-'          )                 -1 <-<    0 =    '99' (prefix='-.' mult=100)                  0  -<    1 =    '99' (prefix='.'  mult=100)                  1  -  high = '00.00';  run; 

Add the PROCLIB.FORMATS catalog to the search path that SAS uses to find user-defined formats. The FMTSEARCH= system option defines the search path. The FMTSEARCH= system option requires only a libref. FMTSEARCH= assumes that the catalog name is FORMATS if no catalog name appears. Without the FMTSEARCH= option, SAS would not find the NOZEROS. format. [*]

 options fmtsearch=(proclib); 

Print the SAMPLE data set. The FORMAT statement associates the NOZEROS. format with the Amount variable.

 proc print data=sample;     format amount nozeros.; 

Specify the titles.

 title1 'Retrieving the NOZEROS. Format from PROCLIB.FORMATS';     title2 'The SAMPLE Data Set';  run; 

Output

 Retrieving the NOZEROS. Format from PROCLIB.FORMATS     1                  The SAMPLE Data Set                     Obs    Amount                      1     -2.05                      2      -.05                      3      -.01                      4       .00                      5       .09                      6       .54                      7       .55                      8      6.60                      9     14.63 

Example 8: Writing Ranges for Character Strings

Data sets:

  • PROCLIB.STAFF on page 463.

This example creates a format and shows how to use ranges with character strings.

Program

 libname proclib '  SAS-data-library  '; 

Set the SAS system options. The NODATE option suppresses the display of the date and time in the output. PAGENO= specifies the starting page number. LINESIZE= specifies the output line length, and PAGESIZE= specifies the number of lines on an output page.

 options nodate pageno=1 linesize=80 pagesize=40; 

Create the TRAIN data set from the PROCLIB.STAFF data set. PROCLIB.STAFF was created in Examples: FORMAT Procedure on page 463.

 data train;     set proclib.staff(keep=name idnumber);  run; 

Print the data set TRAIN without a format. The NOOBS option suppresses the printing of observation numbers.

 proc print data=train noobs; 

Specify the title.

 title 'The TRAIN Data Set without a Format';  run; 
 The TRAIN Data Set without a Format                     1                             Id       Name                Number       Capalleti, Jimmy     2355       Chen, Len            5889       Davis, Brad          3878       Leung, Brenda        4409       Martinez, Maria      3985       Orfali, Philip       0740       Patel, Mary          2398       Smith, Robert        5162       Sorrell, Joseph      4421       Zook, Carla          7385 

Store the format in WORK.FORMATS. Because the LIBRARY= option does not appear, the format is stored in WORK.FORMATS and is available only for the current SAS session.

 proc format; 

Create the $SkillTest. format. The $SKILL. format prints each employee s identification number and the skills test that they have been assigned. Employees must take either TEST A, TEST B, or TEST C, depending on their last name. The exclusion operator (<) excludes the last value in the range. Thus, the first range includes employees whose last name begins with any letter from A through D, and the second range includes employees whose last name begins with any letter from E through M. The tilde (~) in the last range is necessary to include an entire string that begins with the letter Z.

 value $skilltest 'a'-<'e','A'-<'E'='Test A'                   'e'-<'m','E'-<'M'='Test B'                   'm'-'z~','M'-'Z~'='Test C';  run; 

Generate a report of the TRAIN data set. The FORMAT= option in the DEFINE statement associates $SkillTest. with the NAME variable. The column that contains the formatted values of NAME is using the alias Test. Using an alias enables you to print a variable twice, once with a format and once with the default format. See Chapter 43, The REPORT Procedure, on page 861 for more information about PROC REPORT.

 proc report data=train nowd headskip;     column name name=test idnumber;     define test / display format=$skilltest. 'Test';     define idnumber / center;     title 'Test Assignment for Each Employee';  run; 

Output

 Test Assignment for Each Employee                      1  Name              Test    IdNumber  Capalleti, Jimmy  Test A    2355  Chen, Len         Test A    5889  Davis, Brad       Test A    3878  Leung, Brenda     Test B    4409  Martinez, Maria   Test C    3985  Orfali, Philip    Test C    0740  Patel, Mary       Test C    2398  Smith, Robert     Test C    5162  Sorrell, Joseph   Test C    4421  Zook, Carla       Test C    7385 

Example 9: Filling a Picture Format

Procedure features:

  • PICTURE statement options:

    • FILL=

    • PREFIX=

This example

  • prefixes the formatted value with a specified character

  • fills the leading blanks with a specified character

  • shows the interaction between the FILL= and PREFIX= options.

Program

Set the SAS system options. The NODATE option suppresses the display of the date and time in the output. PAGENO= specifies the starting page number. LINESIZE= specifies the output line length, and PAGESIZE= specifies the number of lines on an output page.

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

Create the PAY data set. The PAY data set contains the monthly salary for each employee.

 data pay;     input Name $ MonthlySalary;        datalines;     Liu   1259.45     Lars  1289.33     Kim   1439.02     Wendy 1675.21     Alex  1623.73     ; 

Define the SALARY. picture format and specify how the picture will be filled. When FILL= and PREFIX= PICTURE statement options appear in the same picture, the format places the prefix and then the fill characters . The SALARY. format fills the picture with the fill character because the picture has zeros as digit selectors. The leftmost comma in the picture is replaced by the fill character.

 proc format;     picture salary low-high='00,000,000.00' (fill='*' prefix='$');  run; 

Print the PAY data set. The NOOBS option suppresses the printing of observation numbers. The FORMAT statement temporarily associates the SALARY. format with the variable MonthlySalary.

 proc print data=pay noobs;     format monthlysalary salary.; 

Specify the title.

 title 'Printing Salaries for a Check';  run; 

Output

 Printing Salaries for a Check                1      Name     MonthlySalary      Liu      ****,259.45      Lars     ****,289.33      Kim      ****,439.02      Wendy    ****,675.21      Alex     ****,623.73 

[*] For more information about SAS date constants, see the section on dates, times, and intervals in SAS Language Reference: Concepts . For complete documentation on WORDDATE w ., see the section on formats in SAS Language Reference: Dictionary .

[*] For complete documentation on the FMTSEARCH= system option, see the section on SAS system options in SAS Language Reference: Dictionary .




Base SAS 9.1.3 Procedures Guide (Vol. 1)
Base SAS 9.1 Procedures Guide, Volumes 1, 2, 3 and 4
ISBN: 1590472047
EAN: 2147483647
Year: 2004
Pages: 260

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