Create Custom Numeric Formats with the Proc Format

advertisement
Create Custom Numeric Formats with the Proc Format Picture Statement
Vince Timbers, Penn State University, University Park, PA
ABSTRACT
SAS® provides a variety of numeric formats for displaying
numeric variables. However, there may be times when the
formats provided do not meet your needs. In many of these
situations the PROC FORMAT picture statement may be used
to create a format which specifies a template for displaying
numeric variables as required. This paper provides several
examples of creating custom numeric formats with the PROC
FORMAT picture statement.
Prefix is an option of the value-range-set which specifies
characters to be displayed before the numeric values. Digit
selectors of zero must be used for the prefix to be included in
the picture format.
EXAMPLE 1
This picture statement example creates a format that will output
a social security number with leading zeroes and hyphens.
INTRODUCTION
proc format;
picture soc low-high='999-99-9999';
run;
While the PROC FORMAT picture statement is very useful it is
an often overlooked feature which allows users to create
custom templates for displaying numeric variables. Examples of
situations which require custom numeric formats include the
output of social security numbers with hyphens, phone numbers
with hyphens and parentheses, negative numeric percentages
with a minus sign and numbers with leading zeros. These
examples will be used below to illustrate the use of the PROC
FORMAT picture statement.
The picture statement includes soc as the required name
argument. The value-range-set of low-high indicates that all
values from the lowest to the highest will use the picture
specified. The picture specified, 999-99-9999, includes 9s for
digit selectors and hyphens for message characters. The
resulting format will output a social security number with
leading zeroes when needed and hyphens in the correct places.
Using this format, a value of 12345678 would be formatted as
012-34-5678.
SYNTAX
Picture statement arguments used in the following examples
include name, value-range-sets and options.
EXAMPLE 2
Name specifies the name of the format to be created and can
be up to eight characters long, not ending in a number.
The second example creates a format that outputs a phone
number with the area code in parentheses and the hyphen in
the proper place.
Value-range-set specifies variable values and the picture (or
template) for outputting the specified values. The picture can be
up to 40 characters including three types of characters: digit
selectors, message characters and directives.
proc format;
picture phone
low-high='0000) 000-0000'
(prefix='(');
run;
Digit selectors define the positions of numeric values and
can range from 0 through 9. Nonzero digit selectors print
leading zeros in variable values while digit selectors of
zero do not print leading zeroes. Picture formats that
include a digit selector must have a digit selector as the
first character in the picture.
Message characters are nonnumeric characters which can
be included in the picture.
Directives are characters that can be used in the picture to
format date, time and datetime values. Directives will not
be covered in this paper.
In this example the name of the format is phone. The valuerange-set of low-high indicates that all values from the lowest
to the highest will use the picture specifies. The picture
specified, 0000) 000-0000, includes zeros for digit selectors to
allow the prefix options to be used. A closing parenthesis
followed by a space and a hyphen are message characters that
are put in the appropriate place. Note that the picture starts with
an extra zero in place where the opening parenthesis will be.
This is because the picture must begin with a digit selector. The
opening parenthesis is placed in the format with the prefix
option, (prefix='(').
Using this format, the value 8141112222 would be formatted as
(814) 111-2222.
EXAMPLE 3
The third example creates a format to output numeric values
ending with the percent sign and beginning with a minus sign if
the value is less than zero. This is different from the percent
format provided by SAS which puts negative numbers in
parentheses.
proc format;
picture per low-<0='00000%'
(prefix='-')
0='9%'
0<-high='00000%';
run;
In this example the name of the format being created is per.
This example differs from the others in that it has three valuerange-sets. The first set, low-<0, includes all negative numbers
up to but not including zero. The prefix option was used on this
set to include a minus sign before the number. The second set
includes 0 while the third set includes 0<-high which indicates
all values greater than zero. Notice that the pictures for the first
and last value-range-sets use zeros for the digit indicators while
the second value-rage-set uses nine as the digit indicator. This
is because, as mentioned above, nonzero digit indicators allow
leading zeroes to be output while zero digit indicators do not. If
zero were used as the digit indicator in the picture for the
second value-range-set the format would not produce any
output.
CONCLUSION
The above examples demonstrate that the PROC FORMAT
picture is a powerful and flexible tool for creating custom
numeric formats. While these examples used the most basic
picture statement options, there are others that provide
additional functionality. For more information see the PROC
FORMAT documentation.
SAS is a registered trademark of SAS Institute Inc. in the USA
and other countries. ® indicates USA registration.
CONTACT INFORMATION
Vince Timbers
201 Shields Building
University Park, PA 16802
814-865-4253
vlt@psu.edu
Download