Creating Custom Formats Using the PICTURE Statement

Overview

You have learned that the VALUE statement can associate a text label with a discrete numeric or character value. Suppose you want to insert text characters into a numeric value. For example, you might have stored phone numbers as numeric values like 1111231234 that you want to display as (111) 123-1234. You can use the PICTURE statement to create a template for printing numbers.
General form, PROC FORMAT with the PICTURE statement:
PROC FORMAT;
PICTURE format-name
value-or-range='picture';
RUN;
Here is an explanation of the syntax:
format-name
is the name of the format that you are creating.
value-or-range
is the individual value or range of values that you want to label.
picture
specifies a template for formatting values of numeric variables. The template is a sequence of characters enclosed in quotation marks. The maximum length for a picture is 40 characters.

Ways to Specify Pictures

Pictures are specified with three types of characters:
  • digit selectors
  • message characters
  • directives.
Consider using digit selectors and message characters first. You learn about directives in a later topic.
Digit selectors are numerals (0 through 9) that define positions for numbers. If you use nonzero digit selectors, zeros are added to the formatted value as needed. If you use zeros as digit selectors, no zeros are added to the formatted value.
In the picture definitions below, you can see the difference between using nonzero digit selectors (99) and zero digit selectors (00) on the formatted values.
Picture Definition
Data Values
Formatted Values
picture month 1-12='99';
1
12
01
12
picture month 1-12='00';
1
12
1
12
Message characters are nonnumeric characters that are printed as specified in the picture. They are inserted into the picture after the numeric digits are formatted. Digit selectors must come before message characters in the picture definition. The prefix option can be used to append text in front of any digits. In the picture definition below, the text string JAN consists of message characters.
Picture Definition
Data Value
Formatted Value
picture month 1='99 JAN';
1
01 JAN

Example

The following PICTURE statement contains both digit selectors and message characters. Because the RAINAMT. format has nonzero digit selectors, values are printed with leading zeros. The keyword OTHER is used to print values and message characters for any values that do not fall into a specified range.
proc format;
   picture rainamt
           0-2='9.99 slight'
           2<-4='9.99 moderate'
           4<-<10='9.99 heavy'
           other='999 check value';
run;
data rain;
   input Amount;
   datalines;
   4
   3.9
   20
   .5
   6
   ;
proc print data=rain;
   format amount rainamt.;
run;
The following output shows the values with the RAINAMT. format applied.
RAINAMT. format
The final way to specify a picture is with a directive. Directives are special characters that you can use in the picture to format date, time, or datetime values. If you use a directive, you must specify the DATATYPE= option in the PICTURE statement. This option specifies that the picture applies to a SAS date, SAS time, or SAS datetime value.
General form, PICTURE statement with the DATATYPE= option:
PICTURE format-name
value-or-range, 'picture' (DATATYPE=SAS-date-value-type);
Here is an explanation of the syntax:
format-name
is the name of the format that you are creating.
value-or-range
is the individual value or range of values that you want to label.
picture
specifies a template with directives for formatting numeric values.
SAS-date-value-type
is either DATE, TIME, or DATETIME.

Guidelines for Specifying Directives

The percent sign (%) followed by a letter indicates a directive. Directives that you can use to create a picture format are listed in the table below.
Directive
Result
%a
abbreviated weekday name
%A
full weekday name
%b
abbreviated month name
%B
full month name
%d
day of the month as a number 1-31, with no leading zero
%H
24-hour clock as a number 0-23, with no leading zero
%I
12-hour clock as a number 1-12, with no leading zero
%j
day of the year as a number 1-366, with no leading zero
%m
month as a number 1-12, with no leading zero
%M
minute as a decimal number 0-59, with no leading zero
%p
AM or PM
%S
second as a number 0-59, with no leading zero
%U
week number of the year (Sunday is the first day of the week) as a number 0-53, with no leading zero
%w
weekday as a number (1=Sunday, to 7)
%y
year without century as a number 0-99, with no leading zero
%Y
year with century as a number
Although directives generally return numbers with no leading zeros, you can add 0 in the directive so that if a one-digit numeric value is returned, it is preceded by a 0.
As shown below, when you create a picture with directives, the number of characters inside quotation marks is the maximum length of the formatted value. You must add trailing blanks to the directive if your values contain more characters than the picture. The formatted value is truncated if you do not.
directives

Example

Suppose you want to display values for employee hire dates in the format dd-mmmyyyy (such as 25-JAN2000). This format requires spaces for 10 characters.
The following code creates this format. There are a few things that you should notice about the picture definition:
  • The keywords LOW and HIGH are used to include all values.
  • The 0 in the %d directive indicates that if the day of the month is one digit, it should be preceded by a 0.
  • Because there are only eight characters inside the single quotation marks, you must add two blank spaces to set the length to 10.
proc format;
   picture mydate
           low-high='%0d-%b%Y  ' (datatype=date);
run;

proc print data=sasuser.empdata 
     (keep=division hireDate lastName obs=5);
   format hiredate mydate.;
run;
The output below shows the values for HireDate formatted with the MYDATE. picture format.
MYDATE. picture format
Tip
For more information about using the PICTURE statement, see the documentation for the FORMAT procedure.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.117.186.153