Formats
Formats
Please note: 1) Familiarity with SAS is assumed in this document. 2) This document will not attempt to
cover all that is possible with proc formats. Please refer to “The FORMAT Procedure” chapter in the SAS
Procedures Guide for more information. (Documentation is available on-line at this web-site for the UW
community).
It is possible to create formats for values for variables in SAS by using PROC FORMATS. “Proc formats”
is similar to using value labels in SPSS, but there are important differences. And it is a lot more complicated.
I will explain the use of proc format in programs below. These programs were written in Version 8.1 of
SAS PC. Comments are in italics and green and have explanations that are important.
EXAMPLE 1 - Simplest - Formats are not permanently assigned to variables, and are not saved.
* format1.sas ;
title1 'Format1.sas, 1st program to explain formats in SAS' ;
* Using "proc format" creates formats that are not associated with variables. ;
proc format ;
* Character variable formats begin with a dollar sign. ;
* Both sides of the equal sign must be in single or double ;
* quotes for character variable formats. ;
VALUE $STUSABF
'AL' = "Alabama" "AK" = "Alaska" "AZ" = "Arizona"
"AR" = "Arkansas" "CA" = "California" "CO" = "Colorado"
"CT" = "Connecticut" "DE" = "Delaware" ;
* Either single quotes or double quotes can be used. ;
value statefip
1= 'Alabama' 2= "Alaska" 4= "Arizona" 5= "Arkansas"
6= "California" 8= "Colorado" 9= "Connecticut" 10= "Delaware" ;
* It is sometimes helpful to include the number in the value - in case you need to know.;
Value Region 1='1 North East' 2='2 MidWest' 3='3 South' 4='4 West' ;
value yn 0="no" 1='yes' ; run ;
1
Prepared by Patty Glynn, University of Washington, February 15, 2001
EXAMPLE 2 - Formats are permanently assigned to variables, and are saved. This program creates a
file for formats (always called formats.sas7bcat) in the directory specified in the libname - in this case:
LIBNAME LIBRARY 'C:\ALL\HELP\HELPNEW\SASLIB8' ;
proc format library = LIBRARY ;
* format2.sas ;
title1 'Format2.sas, 2nd program to explain formats in SAS' ;
There are tricks to using saved formats. Following is a program that causes problems, and the error
message that you will get if you have this problem. SAS doesn’t know where to find the formats.
Following is a program that provides one way of solving the error in the program above. You can tell SAS
where to look for formats in the options card.
* format4.sas ;
title1 'Format4.sas, to explain formats in SAS - accessing formats from library' ;
* Information on formats, Patty Glynn, 2/15/2001;
LIBNAME test 'C:\ALL\HELP\HELPNEW\SASLIB8' ;
* You can tell SAS where to look for formats in the options card. ;
options fmtsearch= (test.formats) ;
data ONE; set test.FORMAT2; proc freq ; run ;
USING SAVED FORMATS: Example 3 - another solution - use “library” as the libname.
Following is another program that provides one way of solving the error in the program above. Using a
libname of “library” has special meaning to SAS. It lets SAS know that it should look in that directory for
formats.
* format5.sas ;
title1 'Format5.sas, to explain formats in SAS - accessing formats from library' ;
USING SAVED FORMATS: Example 4 - another solution - telling SAS to ignore format errors - you
won’t not get formats, but at least your program will run.
If you do not have the formats associated with a data set (this happens sometimes) or if you do not want to use
them, it is possible to use the data set anyway by adding “nofmterr” to the options statement.
* format6.sas ;
title1 'Format6.sas, to explain formats in SAS - accessing formats from library' ;
It is also possible to use formats to categorize variables in different ways. See the following sample
program and output.
* format7.sas ;
title1 'Format7.sas, Different Categories with Proc Format' ;
proc format ;
value south 1 = 'Not South' 2='Not South' 3='South' 4='Not South' ;
Value Region 1='1 North East' 2='2 MidWest' 3='3 South' 4='4 West' ;
1 AL South
2 AK Not South
3 AZ Not South
4 AR South
5 CA Not South
6 CO Not South
7 CT Not South
8 DE South
Cumulative Cumulative
region Frequency Percent Frequency Percent
ƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒ
Not South 5 62.50 5 62.50
South 3 37.50 8 100.00
1 AL 3 South
2 AK 4 West
3 AZ 4 West
4 AR 3 South
5 CA 4 West
6 CO 4 West
7 CT 1 North East
8 DE 3 South
Cumulative Cumulative
region Frequency Percent Frequency Percent
ƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒ
1 North East 1 12.50 1 12.50
3 South 3 37.50 4 50.00
4 West 4 50.00 8 100.00