Sample 25301: FORMAT Procedure, Chapter 18
          These sample files and code examples are provided by SAS Institute
          Inc. "as is" without warranty of any kind, either express or implied, including
          but not limited to the implied warranties of merchantability and fitness for a
          particular purpose. Recipients acknowledge and agree that SAS Institute shall
          not be liable for any damages whatsoever arising out of their use of this material.
          In addition, SAS Institute will provide no support for the materials contained herein.
        
 
 /****************************************************************/
 /*          S A S   S A M P L E   L I B R A R Y                 */
 /*                                                              */
 /*    NAME: BPG18R01                                            */
 /*   TITLE: FORMAT Procedure, Chapter 18                        */
 /* PRODUCT: SAS                                                 */
 /*  SYSTEM: ALL                                                 */
 /*    KEYS: EXAMPLES FROM DOCUMENTATION, PICTURE, INFORMAT,     */
 /*    KEYS: ZIP CODE, STATE, CENSUS, PHONE NUMBER               */
 /*   PROCS: FORMAT PRINT SORT MEANS                             */
 /*    DATA:                                                     */
 /*                                                              */
 /* SUPPORT:                             UPDATE:                 */
 /*     REF: SAS Procedures Guide, CHAPTER 18                    */
 /*    MISC:                                                     */
 /*                                                              */
 /****************************************************************/
options ls=132;
proc format;
   picture phonenum other='000/000-0000';
   picture fax other='0999)999-9999' (prefix='(');
run;
data a;
   input phone fx;
   put phone phonenum.;
   format fx fax.;
   cards;
9196778000 3332211111
9195551212 5556677777
;
run;
proc print data=a;
   format phone phonenum.;
   title "Formatting in the PROC PRINT Step";
run;
proc format;
   invalue grade 'A'=4 'B'=3 'C'=2 'D'=1 'F'=0;
data grades;
   input name $ (course1-course3) (: grade.);
   gpa = mean(of course1-course3);
   cards;
BILL   A B A
JIM    A B B
RICK   B C D
ROBERT D . F
;
proc print data=grades;
   title "Reading Data with an Informat";
run;
proc format;
   picture miles 1-99='000000'
             100-high='>100 MILES' (noedit);
data temp;
   input name $ distance 3.;
   cards;
JOHN 300
MARY 600
DAVID 27
ANN 2
;
proc print data=temp;
   format distance miles.;
   title "NOEDIT Option";
run;
data test;
   a=123.45;
   b=123.45;
   c=12345;
   d=12345;
run;
proc format;
   picture mwdec low-high='00000.0' (mult=.1);
   picture mnodec low-high='00000.0'  (mult=10);
run;
data temp;
   input cents;
   cards;
4123
2130
7250
5379
;
proc format;
   picture dolls low-high='009' (mult=.01);
   run;
proc print;
   format cents dolls.;
   run;
proc format;
   picture feet other='000000009' (mult=5280);
run;
data feet;
   input miles @@;
   format miles feet.;
   cards;
1   1.5   2
;
run;
proc print;
run;
proc format fmtlib;
title "FMTLIB Option";
   value zipne
   01000-02799='Massachusetts'
   02800-02999='Rhode Island' 03000-03899='New Hampshire'
   03900-04999='Maine'      05000-05999='Vermont'
   06000-06999='Connecticut' 07000-08999='New Jersey'
   09000-14999='New York'   15000-19699='Pennsylvania'
   19700-19999='Delaware'
   ;
title "PAGE Option with Picture Formats";
proc format page;
   picture prot    low- -1E5='-OVERFLOW'    (noedit)
                -99999.99-<0='000,009.99' (prefix='-' fill='*')
                 0-999999.99='000,009.99' (fill='*')
                    1E6-high='OVERFLOW'    (noedit);
   picture europe   low-0='00.009,00'  (prefix='-' mult=100)
                   0-high='00.009,00'  (mult=100);
 /* Example 1: PROC FORMAT with the VALUE Statement */
proc format;
      /* ZIPST: converts ZIP codes to state names */
   value zipst
   00600-00999='Puerto Rico'    01000-02799='Massachusetts'
   02800-02999='Rhode Island'   03000-03899='New Hampshire'
   03900-04999='Maine'          05000-05999='Vermont'
   06000-06999='Connecticut'    07000-08999='New Jersey'
   09000-14999='New York'       15000-19699='Pennsylvania'
   19700-19999='Delaware'       20000-20599='District of Columbia'
   20600-21999='Maryland'       22000-24699='Virginia'
   24700-26899='West Virginia'  27000-28999='North Carolina'
   29000-29999='South Carolina' 30000-31999='Georgia'
   32000-34999='Florida'        35000-36999='Alabama'
   37000-38599='Tennessee'      38600-39799='Mississippi'
   40000-42799='Kentucky'       43000-45899='Ohio'
   46000-47999='Indiana'        48000-49999='Michigan'
   50000-52899='Iowa'           53000-54999='Wisconsin'
   55000-56799='Minnesota'      57000-57799='South Dakota'
   58000-58899='North Dakota'   59000-59999='Montana'
   60000-62999='Illinois'       63000-65899='Missouri'
   66000-67999='Kansas'         68000-69399='Nebraska'
   70000-71499='Louisiana'      71600-72999='Arkansas'
   73000-74999='Oklahoma'       75000-79999='Texas'
   80000-81699='Colorado'       82000-83199='Wyoming'
   83200-83899='Idaho'          84000-84799='Utah'
   85000-86599='Arizona'        87000-88499='New Mexico'
   89000-89899='Nevada'         90000-96699='California'
   96700-96899='Hawaii'         96900-96999='Guam'
   97000-97999='Oregon'         98000-99499='Washington'
   99500-99999='Alaska'
   ;
     /* STATE: converts state abbrev. to state names */
   value $state
   'AL'='Alabama'                'NE'='Nebraska'
   'AK'='Alaska'                 'NV'='Nevada'
   'AZ'='Arizona'                'NH'='New Hampshire'
   'AR'='Arkansas'               'NJ'='New Jersey'
   'CA'='California'             'NM'='New Mexico'
   'CO'='Colorado'               'NY'='New York'
   'CT'='Connecticut'            'NC'='North Carolina'
   'DE'='Delaware'               'ND'='North Dakota'
   'DC'='District of Columbia'   'OH'='Ohio'
   'FL'='Florida'                'OK'='Oklahoma'
   'GA'='Georgia'                'OR'='Oregon'
   'HI'='Hawaii'                 'PA'='Pennsylvania'
   'ID'='Idaho'                  'RI'='Rhode Island'
   'IL'='Illinois'               'SC'='South Carolina'
   'IN'='Indiana'                'SD'='South Dakota'
   'IA'='Iowa'                   'TN'='Tennessee'
   'KS'='Kansas'                 'TX'='Texas'
   'KY'='Kentucky'               'UT'='Utah'
   'LA'='Louisiana'              'VT'='Vermont'
   'ME'='Maine'                  'VA'='Virginia'
   'MD'='Maryland'               'WA'='Washington'
   'MA'='Massachusetts'          'WV'='West Virginia'
   'MI'='Michigan'               'WI'='Wisconsin'
   'MN'='Minnesota'              'WY'='Wyoming'
   'MS'='Mississippi'            'RQ'='Puerto Rico'
   'MO'='Missouri'               'GQ'='Guam'
   'MT'='Montana'                '99'='Foreign'
   ;
      /* STATE: official census bureau codes for state */
   value state
   01='Alabama'       30='Montana'
   02='Alaska'        31='Nebraska'
   04='Arizona'       32='Nevada'
   05='Arkansas'      33='New Hampshire'
   06='California'    34='New Jersey'
   08='Colorado'      35='New Mexico'
   09='Connecticut'   36='New York'
   10='Delaware'      37='North Carolina'
   11='D.C.'          38='North Dakota'
   12='Florida'       39='Ohio'
   13='Georgia'       40='Oklahoma'
   15='Hawaii'        41='Oregon'
   16='Idaho'         42='Pennsylvania'
   17='Illinois'      44='Rhode Island'
   18='Indiana'       45='South Carolina'
   19='Iowa'          46='South Dakota'
   20='Kansas'        47='Tennessee'
   21='Kentucky'      48='Texas'
   22='Louisiana'     49='Utah'
   23='Maine'         50='Vermont'
   24='Maryland'      51='Virginia'
   25='Massachusetts' 53='Washington'
   26='Michigan'      54='West Virginia'
   27='Minnesota'     55='Wisconsin'
   28='Mississippi'   56='Wyoming'
   29='Missouri'
   ;
data new;
   input zip abbrev $ censusbu;
   zipst=put(zip,zipst.);
   cstate=put(abbrev,$state.);
   nstate=put(censusbu,state.);
   cards;
27512 NC 37
;
proc print;
   var zip zipst abbrev cstate censusbu nstate;
   title "Formatted State Codes and Abbreviations";
run;
 /* Example 2: PROC FORMAT with the PICTURE Statement */
proc format;
   picture acct    low-<0='000,009.99)' (prefix='(')
                   0-high='000,009.99 ';
   picture prot    low- -1E5='-OVERFLOW'    (noedit)
                -99999.99-<0='000,009.99' (prefix='-' fill='*')
                 0-999999.99='000,009.99' (fill='*')
                    1E6-high='OVERFLOW'    (noedit);
   picture dol     low-<0='000,009.99' (prefix='$-')
                   0-high='000,009.99' (prefix='$');
   picture rsign   low-<0='000,009.99-'
                   0-high='000,009.00+';
   picture credit  low-<0='00,009.99DR'
                   0-high='00,009.99CR';
   picture europe  low-<0='00.009,00' (prefix='-' mult=100)
                   0-high='00.009,00'  (mult=100);
   picture blank   low-<0='000 009.99' (prefix='-')
                   0-high='000 009.99';
   picture thous   0-high='00,009K'     (mult=.001);
   picture phone   other='000/000-0000';
run;
data a;
   input x phone;
   acct=x; prot=x; dol=x; rsign=x; credit=x; europe=x; blank=x; thou=x;
   format acct acct.  prot prot.  dol dol.  rsign rsign.
   credit credit. europe europe. blank blank. thou thous.
   phone phone.  X 12.2;
   cards;
12345   9196778000
0      6778000
-12345  .
-187.65 9196778000
187.65  .
.23      .
101.23  .
1.1E6   .
;
proc print data=a;
   id x;
   var acct prot dol rsign credit europe blank thou phone;
   title 'Print the Formats Created with PROC FORMAT';
run;
 /* Example 3: PROC FORMAT with the INVALUE Statement */
 /* Create informats for grade scales and for sex */
proc format;
   invalue grade 'A+'=4.0 'A'=3.5 'A-'=3.2
                 'B+'=3.0 'B'=2.5 'B-'=2.2
                 'C+'=2.0 'C'=1.5 'C-'=1.2
                 'D+'=1.0 'D'=0.5 'D-'=0.2
                 'E'=0 'I'=.;
   invalue sex 'M'=1 'F'=2;
run;
 /* Read in the student name data */
data students;
   input id sex : sex. name $;
   cards;
003 F Jane
005 F Mary
001 M John
002 F Robin
004 M Rick
;
run;
 /* Sort to be in order by ID */
proc sort;
   by id;
run;
 /* Input the grade information */
data grades;
   input id nclasses @;
   do i=1 to nclasses;
      input grade: grade. @;
      output;
      end;
   keep id grade;
   cards;
003 5 A B+ B C+ A-
002 5 B+ I C C E
001 4 A B C- B-
005 5 A A A A+ A-
004 4 B- D E C
;
run;
 /* Sort to be in order by ID */
proc sort data=grades;
   by id;
run;
 /* Determine the GPAs for the students */
proc means data=grades noprint;
   by id;
   var grade;
   output out=final(drop=_type_) mean=gpa;
run;
 /* Merge GPA into student info */
data students;
  merge students final;
  by id;
run;
 /* Sort to print by SEX */
proc sort data=students;
  by sex;
 /* Print the final results */
proc print data=students;
  by sex;
  title 'Students'' Names and GPAs';
run;
 /* Example 4: PROC FORMAT with the CNTLIN= Option */
data acctinfo;
   input acctnum name $15. @26 opendate date7.;
   cards;
5008074 John Smith       01JAN87
5008075 Bill Jones       10JAN87
5009766 Benjamin Estes   18OCT86
7089477 Mary Wilson      27NOV86
;
 /* First create an input control data set */
data cntlacct(rename=(acctnum=start name=label));
   set acctinfo(keep=acctnum name);
   fmtname='account';
run;
proc print data=cntlacct;
   title "Input Control Data Set";
run;
 /* Read the control data set into PROC FORMAT */
proc format cntlin=cntlacct;
run;
 /* The format is now created and ready to use */
data charges;
   input acctnum transamt;
   put acctnum account. ' account charged ' transamt dollar10.2;
   cards;
5008074 127.86
7089477 100.00
5009766  50.00
;
 /* Example 5: PROC FORMAT with the CNTLOUT= Option */
proc format cntlout=outdata;
  picture phonenum other='000/000-0000';
  invalue grade 'a'=4 'b'=3 'c'=2 'd'=1 'e'=0;
run;
proc print data=outdata;
  title 'Output Control Data Set';
run;
          These sample files and code examples are provided by SAS Institute
          Inc. "as is" without warranty of any kind, either express or implied, including
          but not limited to the implied warranties of merchantability and fitness for a
          particular purpose. Recipients acknowledge and agree that SAS Institute shall
          not be liable for any damages whatsoever arising out of their use of this material.
          In addition, SAS Institute will provide no support for the materials contained herein.
        
 
Formatting in the PROC PRINT Step
OBS       PHONE             FX
 1     919/677-8000    (333)221-1111
 2     919/555-1212    (555)667-7777
Reading Data with an Informat
OBS    NAME      COURSE1    COURSE2    COURSE3      GPA
 1     BILL         4          3          4       3.66667
 2     JIM          4          3          3       3.33333
 3     RICK         3          2          1       2.00000
 4     ROBERT       1          .          0       0.50000
NOEDIT Option
OBS    NAME       DISTANCE
 1     JOHN     >100 MILES
 2     MARY     >100 MILES
 3     DAVID            27
 4     ANN               2
NOEDIT Option
OBS    CENTS
 1      41
 2      21
 3      72
 4      53
NOEDIT Option
OBS    MILES
 1      5280
 2      7920
 3     10560
PAGE Option with Picture Formats
----------------------------------------------------------------------------
|       FORMAT NAME: DOLLS    LENGTH:    3   NUMBER OF VALUES:    1        |
|   MIN LENGTH:   1  MAX LENGTH:  40  DEFAULT LENGTH   3  FUZZ: STD        |
|--------------------------------------------------------------------------|
|START           |END             |LABEL  (VER. 6.12    31MAY05:16:08:00)  |
|----------------+----------------+----------------------------------------|
|LOW             |HIGH            |009                 P   F  M0.01        |
----------------------------------------------------------------------------
----------------------------------------------------------------------------
|       FORMAT NAME: FAX      LENGTH:   13   NUMBER OF VALUES:    1        |
|   MIN LENGTH:   1  MAX LENGTH:  40  DEFAULT LENGTH  13  FUZZ: STD        |
|--------------------------------------------------------------------------|
|START           |END             |LABEL  (VER. 6.12    31MAY05:16:07:59)  |
|----------------+----------------+----------------------------------------|
|**OTHER**       |**OTHER**       |0999)999-9999       P(  F  M1           |
----------------------------------------------------------------------------
----------------------------------------------------------------------------
|       FORMAT NAME: FEET     LENGTH:    9   NUMBER OF VALUES:    1        |
|   MIN LENGTH:   1  MAX LENGTH:  40  DEFAULT LENGTH   9  FUZZ: STD        |
|--------------------------------------------------------------------------|
|START           |END             |LABEL  (VER. 6.12    31MAY05:16:08:00)  |
|----------------+----------------+----------------------------------------|
|**OTHER**       |**OTHER**       |000000009           P   F  M5280        |
----------------------------------------------------------------------------
----------------------------------------------------------------------------
|       FORMAT NAME: MILES    LENGTH:   10   NUMBER OF VALUES:    2        |
|   MIN LENGTH:   1  MAX LENGTH:  40  DEFAULT LENGTH  10  FUZZ: STD        |
|--------------------------------------------------------------------------|
|START           |END             |LABEL  (VER. 6.12    31MAY05:16:08:00)  |
|----------------+----------------+----------------------------------------|
|               1|              99|000000              P   F  M1           |
|             100|HIGH            |>100 MILES        N P   F  M1           |
----------------------------------------------------------------------------
----------------------------------------------------------------------------
|       FORMAT NAME: MNODEC   LENGTH:    7   NUMBER OF VALUES:    1        |
|   MIN LENGTH:   1  MAX LENGTH:  40  DEFAULT LENGTH   7  FUZZ: STD        |
|--------------------------------------------------------------------------|
|START           |END             |LABEL  (VER. 6.12    31MAY05:16:08:00)  |
|----------------+----------------+----------------------------------------|
|LOW             |HIGH            |00000.0             P   F  M10          |
----------------------------------------------------------------------------
PAGE Option with Picture Formats
----------------------------------------------------------------------------
|       FORMAT NAME: MWDEC    LENGTH:    7   NUMBER OF VALUES:    1        |
|   MIN LENGTH:   1  MAX LENGTH:  40  DEFAULT LENGTH   7  FUZZ: STD        |
|--------------------------------------------------------------------------|
|START           |END             |LABEL  (VER. 6.12    31MAY05:16:08:00)  |
|----------------+----------------+----------------------------------------|
|LOW             |HIGH            |00000.0             P   F  M0.1         |
----------------------------------------------------------------------------
----------------------------------------------------------------------------
|       FORMAT NAME: PHONENUM LENGTH:   12   NUMBER OF VALUES:    1        |
|   MIN LENGTH:   1  MAX LENGTH:  40  DEFAULT LENGTH  12  FUZZ: STD        |
|--------------------------------------------------------------------------|
|START           |END             |LABEL  (VER. 6.12    31MAY05:16:07:59)  |
|----------------+----------------+----------------------------------------|
|**OTHER**       |**OTHER**       |000/000-0000        P   F  M1           |
----------------------------------------------------------------------------
----------------------------------------------------------------------------
|       FORMAT NAME: ZIPNE    LENGTH:   13   NUMBER OF VALUES:   10        |
|   MIN LENGTH:   1  MAX LENGTH:  40  DEFAULT LENGTH  13  FUZZ: STD        |
|--------------------------------------------------------------------------|
|START           |END             |LABEL  (VER. 6.12    31MAY05:16:08:01)  |
|----------------+----------------+----------------------------------------|
|            1000|            2799|Massachusetts                           |
|            2800|            2999|Rhode Island                            |
|            3000|            3899|New Hampshire                           |
|            3900|            4999|Maine                                   |
|            5000|            5999|Vermont                                 |
|            6000|            6999|Connecticut                             |
|            7000|            8999|New Jersey                              |
|            9000|           14999|New York                                |
|           15000|           19699|Pennsylvania                            |
|           19700|           19999|Delaware                                |
----------------------------------------------------------------------------
----------------------------------------------------------------------------
|     INFORMAT NAME: @GRADE   LENGTH:    1   NUMBER OF VALUES:    5        |
|   MIN LENGTH:   1  MAX LENGTH:  40  DEFAULT LENGTH   1  FUZZ:        0   |
|--------------------------------------------------------------------------|
|START           |END             |INVALUE(VER. 6.12    31MAY05:16:07:59)  |
|----------------+----------------+----------------------------------------|
|A               |A               |            4                           |
|B               |B               |            3                           |
|C               |C               |            2                           |
|D               |D               |            1                           |
|F               |F               |            0                           |
----------------------------------------------------------------------------
Example 1: PROC FORMAT with the VALUE Statement 
Formatted State Codes and Abbreviations
OBS     ZIP         ZIPST         ABBREV        CSTATE        CENSUSBU        NSTATE
 1     27512    North Carolina      NC      North Carolina       37       North Carolina
Example 2: PROC FORMAT with the PICTURE Statement 
Print the Formats Created with PROC FORMAT
           X          ACCT      PROT             DOL         RSIGN        CREDIT      EUROPE        BLANK      THOU          PHONE
    12345.00    12,345.00    *12,345.00   $12,345.00    12,345.00+   12,345.00CR   12.345,00    12 345.00       12K   919/677-8000
        0.00         0.00    ******0.00        $0.00         0.00+        0.00CR        0,00         0.00        0K       677-8000
   -12345.00   (12,345.00)   -12,345.00   -12,345.00    12,345.00-   12,345.00DR   12.345,00   -12 345.00    -12345              .
     -187.65      (187.65)   ***-187.65     $-187.65       187.65-      187.65DR     -187,65      -187.65   -187.65   919/677-8000
      187.65       187.65    ****187.65      $187.65       187.65+      187.65CR      187,65       187.65        0K              .
        0.23         0.23    ******0.23        $0.23         0.23+        0.23CR        0,23         0.23        0K              .
      101.23       101.23    ****101.23      $101.23       101.23+      101.23CR      101,23       101.23        0K              .
  1100000.00   100,000.00      OVERFLOW   100,000.00   100,000.00+        0.00CR        0,00   100 000.00    1,100K              .
Example 3: PROC FORMAT with the INVALUE Statement 
Students' Names and GPAs
SEX=1
OBS    ID    NAME    _FREQ_     GPA
 1      1    John       4      2.35
 2      4    Rick       4      1.05
SEX=2
OBS    ID    NAME     _FREQ_     GPA
 3      2    Robin       5      1.50
 4      3    Jane        5      2.84
 5      5    Mary        5      3.54
Example 4: PROC FORMAT with the CNTLIN= Option 
Input Control Data Set
OBS     START         LABEL         FMTNAME
 1     5008074    John Smith        account
 2     5008075    Bill Jones        account
 3     5009766    Benjamin Estes    account
 4     7089477    Mary Wilson       account
 Example 5: PROC FORMAT with the CNTLOUT= Option 
Output Control Data Set
OBS   FMTNAME    START       END            LABEL       MIN   MAX   DEFAULT   LENGTH    FUZZ    PREFIX   MULT   FILL   NOEDIT   TYPE   SEXCL   EEXCL   HLO
 1    PHONENUM   **OTHER**   **OTHER**   000/000-0000    1     40      12       12      1E-12              1              0      P       N       N      O
 2    GRADE      a           a                      4    1     40       1        1          0              0              0      I       N       N
 3    GRADE      b           b                      3    1     40       1        1          0              0              0      I       N       N
 4    GRADE      c           c                      2    1     40       1        1          0              0              0      I       N       N
 5    GRADE      d           d                      1    1     40       1        1          0              0              0      I       N       N
 6    GRADE      e           e                      0    1     40       1        1          0              0              0      I       N       N 
These samples are from the "SAS Procedures Guide, Version 6", Chapter 18.
For additional information on the samples refer to this book.
| Type: | Sample | 
| Topic: | SAS Reference  ==>  Procedures  ==>  FORMAT
  | 
| Date Modified: | 2005-06-18 03:00:00 | 
| Date Created: | 2005-05-23 13:44:32 | 
Operating System and Release Information
| SAS System | Base SAS | All | n/a | n/a |