![]() Chapter Contents |
![]() Previous |
![]() Next |
| The FORMAT Procedure |
| Featured in: | Creating a Picture Format and Filling a Picture Format |
| See also: | The section on formats in SAS Language Reference: Dictionary for documentation on formats supplied by SAS. |
|
PICTURE name
<(format-option(s))>
<value-range-set-1 <(picture-1-option(s) )> <...value-range-set-n <(picture-n-option(s))>>>; |
| To do this | Use this option | |
|---|---|---|
| Control the attributes of the format | ||
| Specify a fuzz factor for matching values to a range | DEFAULT= | |
| Specify a fuzz factor for matching values to a range | FUZZ= | |
| Specify a maximum length for the format | MAX= | |
| Specify a minimum length for the format | MIN= | |
| Specify multiple pictures for a given value or range and for overlapping ranges | MULTILABEL | |
| Store values or ranges in the order that you define them | NOTSORTED | |
| Round the value to the nearest integer before formatting | ROUND | |
| Control the attributes of each picture in the format | ||
| Specify a character that completes the formatted value | FILL= | |
| Specify a number to multiply the variable's value by before it is formatted | MULTIPLIER= | |
| Specify that numbers are message characters rather than digit selectors | NOEDIT | |
| Specify a character prefix for the formatted value | PREFIX= | |
| Required Arguments |
| Options |
The following options are common to the INVALUE, PICTURE, and VALUE statements and are described in Informat and Format Options:
| DEFAULT= length | |
| FUZZ=fuzz-factor | |
| MAX=length | |
| MIN=length | |
| NOTSORTED |
In addition, you can use the following arguments:
| Default: | . (a decimal point) |
| Default: | , (a comma) |
If the picture includes other characters, such as a comma, which appear to the left of the digit selector that maps to the last significant digit placed, the characters are replaced by the fill character or leading zeros.
| Default: | ' ' (a blank) |
| Interaction: | If you use the FILL= and PREFIX= options in the same picture, the format places the prefix and then the fill characters. |
| Featured in: | Filling a Picture Format |
picture abc (multilabel) 1000='9,999' 1000='9999'; picture overlap (multilabel) /* without decimals */ 0-999='999' 1000-9999='9,999' /* with decimals */ 0-9='9.999' 10-99='99.99' 100-999='999.9';In the first PICTURE statement, the primary format for the value of 1000 makes it appear as 1,000, but its secondary format can make it appear as 1000. In the second PICTURE statement, the primary format for the value 5 makes it appear as 5, but its secondary format can make it appear as 5.000.
$1.6M:
picture million low-high='00.0M'
(prefix='$' mult=.00001);
| Alias: | MULT= |
| Default: | 10n ,
where n is the number of digits after the first decimal point
in the picture. For example, suppose your data contain a value 123.456 and
you want to print it using a picture of '999.999'. The format multiplies
123.456 by 103 to obtain a value of 123456, which
results in a formatted value of 123.456. |
| Example: | Creating a Picture Format |
>1000 miles:
picture miles 1-1000='0000'
1000<-high='>1000 miles'(noedit);The picture must be wide enough to contain both the value and the prefix.
If the picture is not wide enough to contain both the value and the prefix,
the format truncates or omits the prefix. Typical uses for PREFIX= are printing
leading dollar signs and minus signs. For example, the PAY. format prints
the variable value 25500 as $25,500.00:
picture pay low-high='000,009.99'
(prefix='$');
| Default: | no prefix |
| Interaction: | If you use the FILL= and PREFIX= options in the same picture, the format places the prefix and then the fill characters. |
| Featured in: | Creating a Picture Format and Filling a Picture Format |
| Tip: | Note that the ROUND option rounds a value of .5 to the next highest integer. |
| value-or-range-1 <..., value-or-range-n>='picture' |
Digit selectors are numeric characters (0 through 9) that define positions for numeric values. A picture format with nonzero digit selectors prints any leading zeros in variable values; picture digit selectors of 0 do not print leading zeros in variable values. If the picture format contains digit selectors, a digit selector must be the first character in the picture.
Note: This chapter uses 9's as nonzero digit selectors.
Message characters are nonnumeric characters that
print as specified in the picture. The following PICTURE statement contains
both digit selectors (99) and message characters (illegal day
value). Because the DAYS. format has nonzero digit selectors,
values are printed with leading zeros. The special range OTHER prints the
message characters for any values that do not fall into the specified range
(1 through 31).
picture days 01-31='99'
other='99-illegal day value';
For example, the values 02 and 67 print as
02
67-illegal day value
Directives are special characters that you can use in the picture to format date, time, or datetime values.
| Restriction: | You can only use directives when you specify the DATATYPE= option in the PICTURE statement. |
The permitted directives are
| %a | Locale's abbreviated weekday name |
| %A | Locale's full weekday name |
| %b | Locale's abbreviated month name |
| %B | Locale's full month name |
| %d | Day of the month as a decimal number (1-31), with no leading zero |
| %H | Hour (24-hour clock) as a decimal number (0-23), with no leading zero |
| %I | Hour (12-hour clock) as a decimal number (1-12), with no leading zero |
| %j | Day of the year as a decimal number (1-366), with no leading zero |
| %m | Month as a decimal number (1-12), with no leading zero |
| %M | Minute as a decimal number (0-59), with no leading zero |
| %p | Locale's equivalent of either AM or PM |
| %S | Second as a decimal number (0-59), with no leading zero |
| %U | Week number of the year (Sunday as the first day of the week) as a decimal number (0,53), with no leading zero |
| %w | Weekday as a decimal number (1= Sunday, 7) |
| %y | Year without century as a decimal number (0-99), with no leading zero |
| %Y | Year with century as a decimal number |
| %% | % |
Any directive that generates numbers can produce a leading zero, if desired, by adding a 0 before the directive. This applies to %d, %H, %I, %j, %m, %M, %S, %U, and %y. For example, if you specify %y in the picture, then 2001 would be formatted as '1', but if you specify %0y, then 2001 would be formatted as '01'.
| Building a Picture Format: Step by Step |
This section shows how to write a picture format for formatting numbers with leading zeros. In the SAMPLE data set, the default printing of the variable Amount has leading zeros on numbers between 1 and -1:
options nodate pageno=1 linesize=64
pagesize=60;
data sample;
input Amount;
datalines;
-2.05
-.05
-.01
0
.09
.54
.55
6.6
14.63
;
The following PROC FORMAT step creates the NOZEROS. format, which eliminates leading zeros in the formatted values:
libname library 'SAS-data-library';
proc format library=library;
picture nozeros
low - -1 = '00.00'
(prefix='-')
-1 <-< 0 = '99'
(prefix='-.' mult=100)
0 -< 1 = '99'
(prefix='.' mult=100)
1 - high = '00.00';
run;
Building a Picture Format explains how one value from each range is formatted. Formatting One Value in Each Range provides an illustration of each step. The circled numbers in the figure correspond to the step numbers in the table.
| Step | Rule | In this example | |
|---|---|---|---|
| 1 | Determine into which range the value falls and use that picture. | In the second range, the exclusion operator < appears on both
sides of the hyphen and excludes -1 and 0 from the range.
|
|
| 2 | Take the absolute value of the numeric value. | Because the absolute value is used, you need a separate range and
picture for the negative numbers in order to prefix the minus sign.
|
|
| 3 | Multiply the number by the MULT= value. If you do not specify the
MULT= option, the PICTURE statement uses the default. The default is 10n , where n is the number of digit selectors
to the right of the decimal (table note 1)
in the picture. (Step 6 discusses digit selectors further.) |
Specifying a MULT= value is necessary for numbers between 0 and
1 and numbers between 0 and -1 because no decimal appears in the
pictures for those ranges. Because MULT= defaults to 1, truncation of
the significant digits results without a MULT= value specified. (Truncation
is explained in the next step.) For the two ranges that do not have MULT=
values specified, the MULT= value defaults to 100 because the corresponding
picture has two digit selectors to the right of the decimal. After the
MULT= value is applied, all significant digits are moved to the left of
the decimal.
|
|
| 4 | Truncate the number after the decimal. If the ROUND option is in effect, the format rounds the number after the decimal to the next highest integer if the number after the decimal is greater than or equal to .5. | Because the example uses MULT= values that ensured that all of the
significant digits were moved to the left of the decimal, no significant
digits are lost. The zeros are truncated.
|
|
| 5 | Turn the number into a character string. If the number is shorter than the picture, the length of the character string is equal to the number of digit selectors in the picture. Pad the character string with leading zeros. (The results are equivalent to using the Zw. format. Zw. is explained in the section on SAS formats in SAS Language Reference: Dictionary. | The numbers 205, 5, and 660 become the character strings 0205, 05, and
0660, respectively. Because each picture is longer than
the numbers, the format adds a leading zero to each value. The format
does not add leading zeros to the number 55 because the corresponding picture
only has two digit selectors.
|
|
| 6 | Apply the character string to the picture. The format only maps the rightmost n characters in the character string, where n is the number of digit selectors in the picture. Thus, it is important to make sure that the picture has enough digit selectors to accommodate the characters in the string. After the format takes the rightmost n characters, it then maps those characters to the picture from left to right. Choosing a zero or nonzero digit selector is important if the character string contains leading zeros. If one of the leading zeros in the character string maps to a nonzero digit selector, it and all subsequent leading zeros become part of the formatted value. If all of the leading zeros map to zero digit selectors, none of the leading zeros become part of the formatted value; the format replaces the leading zeros in the character string with blanks. (table note 2) | The leading zero is dropped from each of the character strings 0205 and 0660 because
the leading zero maps to a zero digit selector in the picture.
|
|
| 7 | Prefix any characters that are specified in the PREFIX= option. You need the PREFIX= option because when a picture contains any digit selectors, the picture must begin with a digit selector. Thus, you cannot begin your picture with a decimal point, minus sign, or any other character that is not a digit selector. | The PREFIX= option reclaims the decimal point and the negative sign,
as shown with the formatted values -.05 and .55. |
|
A decimal in a PREFIX=
option is not part of the picture.
You can use the
FILL= option to specify a character other than a blank to become part of
the formatted value.
Formatting One Value in Each Range
The following PROC PRINT step associates the NOZEROS. format with the AMOUNT variable in SAMPLE:
proc print data=sample noobs; format amount nozeros.; title 'Formatting the Variable Amount'; title2 'with the NOZEROS. Format'; run;
![[cautend]](../common/images/cautend.gif)
| Specifying No Picture |
This PICTURE statement creates a picture-name format that has no picture:
picture picture-name;
![]() Chapter Contents |
![]() Previous |
![]() Next |
![]() Top of Page |
Copyright © 1999 by SAS Institute Inc., Cary, NC, USA. All rights reserved.