Friday, 23 November 2012

Proc Format


                                 PROC FORMAT STATEMENT

 Sometimes when we enter data in a computer, we use codes to enter the data so it saves time.    In the student data, class is entered with 1, 2, 3, or 4.  Sex was entered with >M= or >F= to make it easier to enter.   This saves us time and space when entering the data.  However, we may want to print out more meaningful information in a report by giving the names to the numbers or the character abbreviations.  This can be done by using a PROC FORMAT statement.

Proc format;
value name range = label;

NAME names the format being created. The name can be 1 to 8 characters long.  It cannot start or end with a number, and cannot contain any special characters except the underscore.  The name cannot be the name of an existing format statement.  If the format is for character variables, the first character of the name must be a $ (dollar sign).  Always try to use meaningful names to help you remember what the format will be used for.  

You can use the same name for a format statement as you used for a column name.  However this is probably not a good idea.  The format values can be up to 40 characters in length but in some cases only 16 characters will print out. This usually happens with charts.    Note that if you entered the data in the data set  in a character form, you must put single quotes around it. 

These are examples of different Format statements.

The RANGE can be
                                                                                  
Value   (single values)

PROC FORMAT;
value $abc
'A' = 'Adult'
'C' = 'Child'
'T' = 'Teenager';
                                                                                  

Value - value  ( a range of values)

PROC FORMAT;
value agefmt
low-12 =  'Child'
13-19 =    'Teen'                                 
20-high = 'Adult';
                                                                                           


NOTE: LOW and HIGH are special words that can be used when you don't know the entire range of ages or other numbers.

                                                                                   
Value, Value (list of ranges or values)
 Ex. 1-4, 7-9   
PROC FORMAT
Value sexfmt
1 = 'Female'
2 = 'Male'
                                                0,3-9 = Miscoded';
                                                                                   

An example of a program with a format statement follows:

Data all;
infile 'a:student.dat';
input name $ 1-17 sex $ 18 class 19 ovgpa 21-24;
proc format;
value cfmt
1 = 'Freshman'
2 = 'Sophomore'
3 = 'Junior'
4 = 'Senior';
Proc chart;
vbar class/discrete;
format class cfmt.;

NOTE: When the name that is given to the format statement is listed in the format statement, it appears with a PERIOD AFTER IT.  This is to let SAS know that it is a FORMAT name and NOT a COLUMN name.  Also the discrete was used here because the column values for class are numeric. 

If the FORMAT statement is used in the DATA statement, the formats are permanently associated with the columns throughout the program.  It can be used with a PROC statement also.  It will only be assigned in that procedure. 









USE OF A FORMAT STATEMENT


                  Frequency                                                                    
                                                                                                
                  20 ˆ                                           *****                         
                     ‚                                           *****                         
                  19 ˆ                                           *****                         
                     ‚                                           *****                         
                  18 ˆ                                           *****                         
                     ‚                                           *****                         
                  17 ˆ                                           *****                         
                     ‚                                           *****                         
                  16 ˆ                                           *****                         
                     ‚                                           *****                         
                  15 ˆ                                           *****                         
                     ‚                                           *****                         
                  14 ˆ                               *****       *****                          
                     ‚                               *****       *****                         
                  13 ˆ                               *****       *****                         
                     ‚                               *****       *****                         
                  12 ˆ                               *****       *****                         
                     ‚                               *****       *****                         
                  11 ˆ                   *****       *****       *****                         
                     ‚                   *****       *****       *****                         
                  10 ˆ                   *****       *****       *****                          
                     ‚                   *****       *****       *****                         
                   9 ˆ                   *****       *****       *****                         
                     ‚                   *****       *****       *****                         
                   8 ˆ                   *****       *****       *****                         
                     ‚                   *****       *****       *****                          
                   7 ˆ                   *****       *****       *****                         
                     ‚                   *****       *****       *****                         
                   6 ˆ       *****       *****       *****       *****                         
                     ‚       *****       *****       *****       *****                         
                   5 ˆ       *****       *****       *****       *****                         
                     ‚       *****       *****       *****       *****                         
                   4 ˆ       *****       *****       *****       *****                         
                     ‚       *****       *****       *****       *****                          
                   3 ˆ       *****       *****       *****       *****                         
                     ‚       *****       *****       *****       *****                         
                   2 ˆ       *****       *****       *****       *****                         
                     ‚       *****       *****       *****       *****                         
                   1 ˆ       *****       *****       *****       *****                         
                     ‚       *****       *****       *****       *****                         
                     Šƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒ                  
                           Freshman    Sophomore    Junior      Senior                         
                                                                                               
                                               class                                           

In an earlier lesson, you looked at charts.  You had an example of a chart using size where people were Short, Average, and Tall.  There were some problems with this.  One of the problems was that SAS sorts everything in alphabetical order.  In the example, Average came before Short. You saw that there was one way to change the order by using the MIDPOINT with character data. Here is another example of how you can define the variable "size" using numbers and then use the Proc Format statement to get the size variable to come out in the order you want. 




data all;
infile 'a:member.dat';
input id 1‑4 LAST $ 6‑14 FIRST $ 16‑25 STWGT 33‑35 ENDWGT 37‑39 SEX $ 41
AGE 43‑44     HEIGHT 46‑47;
if height < 62 then SIZE = 1;
if height >= 62 and height < 67 then SIZE = 2;
if height >= 67 then SIZE = 3;
proc format;
   value tallest
     1 = 'SHORT'
     2 = 'AVERAGE'
     3 = 'TALL';
proc print;
  var last first size;
   format size tallest.;
title 'Print out of the Size of the Members in the Health Study';
run;

proc chart;
    vbar size/discrete group=sex;
 format size tallest.;
title 'Bar chart of Members broken down by Size';
run;

The print out follows:


    Print out of the Size of the Members in the Health Study 
                                 

            OBS    LAST         FIRST       SIZE

              1    BROOKS       JENNIFER    TALL
              2    MARTIM       JOHN        TALL
              3    ARMSTRONG    CHARLENE    AVERAGE
              4    REDDING      BECKY       AVERAGE
              5    SERRANO      AMELIA      AVERAGE
              6    COLLINS      JOAN        AVERAGE

              7    VANHOY       MARGIE      TALL
              8    NAGASAKA     KANOKO      AVERAGE
              9    MCKNIGHT     ASHLEY      AVERAGE
             10    BLACKBURN    KIM         AVERAGE
             11    STEWART      SUSAN       AVERAGE
             12    GARG         ASHA        AVERAGE
             13    FINK         ADRIENNE    TALL
             14    VANMETER     JOHN        TALL
             15    COLLINS      ROSE        TALL
             16    CHOATE       HOLLY       TALL
             17    SHAW         DAVID       TALL
             18    OVERBY       LYNNE       AVERAGE
             19    NANCE        ANN         TALL
             20    BROWN        JIM         TALL
             21    SMITH        ALLISON     SHORT
             22    HICKS        DEANNA      TALL
             23    JONES        TIM         TALL
             24    SINHA        RAVI        TALL
             25    ROSE         RICHARD     TALL
             26    SANCHEZ      RAOUL       TALL
             27    CHEN         YAO         TALL
             28    SIMS         DAVID       TALL
             29    SCHOCK      JASON        TALL
             30    ITO         HISASHI      AVERAGE
             31    GOSS        LARRY        AVERAGE
             32    LEE         LI‑HWA       SHORT
             33    SIMS        ELIZABETH    AVERAGE
             34    LONG        BETTE        TALL
             35    RICHARDS    ANN          AVERAGE

BAR CHART OF MEMBERS BROKEN DOWN BY SIZE

 Frequency                                                                                     
                                                                                               
 12 ˆ                   *****                                                                   
    ‚                   *****                                                                  
 11 ˆ                   *****                                                *****              
    ‚                   *****                                                *****             
 10 ˆ                   *****                                                *****             
    ‚                   *****                                                *****             
  9 ˆ                   *****                                                *****             
    ‚                   *****                                                *****             
  8 ˆ                   *****       *****                                    *****             
    ‚                   *****       *****                                    *****             
  7 ˆ                   *****       *****                                    *****             
    ‚                   *****       *****                                    *****             
  6 ˆ                   *****       *****                                    *****             
    ‚                   *****       *****                                    *****             
  5 ˆ                   *****       *****                                    *****             
    ‚                   *****       *****                                    *****             
  4 ˆ                   *****       *****                                    *****             
    ‚                   *****       *****                                    *****             
  3 ˆ                   *****       *****                                    *****             
    ‚                   *****       *****                                    *****             
  2 ˆ       *****       *****       *****                        *****       *****             
    ‚       *****       *****       *****                        *****       *****             
  1 ˆ       *****       *****       *****                        *****       *****             
    ‚       *****       *****       *****                        *****       *****              
    Šƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒ 
            SHORT      AVERAGE       TALL            SHORT      AVERAGE       TALL        SIZE 
                                                                                                
            ‚ƒƒƒƒƒƒƒƒƒƒƒƒ F ƒƒƒƒƒƒƒƒƒƒƒƒ‚            ‚ƒƒƒƒƒƒƒƒƒƒƒƒ M ƒƒƒƒƒƒƒƒƒƒƒƒ‚        sex  

No comments:

Post a Comment