SAS picture format creates templates in which you’ll be able to define how the numbers are displayed. With the use of PICTURE FORMAT, you will get over several display problems with numbers like:-
- Decimal and Comma Placement
- Embedding Characters with Numbers
PICTURE statement in PROC FORMAT allows you to leverage the facility of PROC FORMAT. You could make labels look the way you would like them to look.
A picture format works identically the same as a primary format on the left-hand side (e.g. the specification of ranges and values); the difference falls on the right-hand side (e.g. the label display).
For the label display, there are three things to know,
- Using a ‘9’ (or any non-zero number) as a digit selector will drive the number to appear (i.e. will include leading zeros),
- Using a 0 as a digit selector will solely display the number if that digit is within the number (i.e. no leading zeros),
- A character string will display precisely as included within the format.
One core rule for Picture Formats is that for those who use digit selectors as the template for displaying the data, the first position of the template has to be a digit selector.
That means you can make the right parentheses part of the template since it’s not a digit.
Instead, if you instruct PROC FORMAT to make the left parentheses the prefix to the template, which will begin with the (required) digit selector.
proc format; picture phone_n (default=16) low-high="999) 999-9999" (prefix='('); run; data Domestic_numbers; format phone phone_n.; input phone; datalines; 5417543010 ; run;
First, make the default length of the Picture Format 16 characters, which is broad enough to accommodate BOTH the template AND the required prefix.
Give the prefix you like in the PREFIX option, which is just like the DEFAULT option enclosed in parentheses.
Be cautious: The
PREFIX option is within the parentheses, and the value of the option is a left parentheses symbol enclosed in single quotes.
Creating a Picture Format
1. Creating a Picture Format (Appending Text)
A textual content string might be added AFTER (however not BEFORE) the numbers by merely including the text within the format.
proc format; picture phone Low-high='9999999999 - Mobile'; run; data test; format mobile phone.; input Mobile $30.; datalines; 9900001000 8888888888 ; run;
2. Creating a Picture Format (Character Display)
A character string might be displayed for certain values while numbers for the other values. A format like this can be used:
proc format; picture temp low-36="Invalid Temperature" 36.1-<37.2='99.00' 37.2-high='99.00 - High Body Temperature'; run; data testtemp; format temp temp.; input temp $30.; datalines; 34.56 13.45 36.4 36.8 37.3 38.0 ; run;
3. Creating a Picture Format (Round Option)
When a picture format is applied, it truncates the data. (As a side note, the data is rounded by default when an existing SAS format is applied like 5.1 or 7.3.) In general, rounding is preferred to truncating.
To make a picture format round as an alternative to truncating, we merely add the
proc format; picture testA (round) Low-high='000.00'; run; data _null_; format val testA.; input val; put val=; datalines; 3.1452342 2.96543 ; run;
4. Creating a Picture Format (Prefix Option)
With a picture format, character strings will be displayed after the digits, as is shown in the above example.
Character strings can’t be displayed before the digits.
proc format; picture testB Low-<1000='099.00' 1000-high='Value of 0000.00 too high'; run; data _null_; format val testb.; input val; put val=; datalines; 3.1452342 2899.96543 ; run;
val=03.14 val=2899.96 too high
Notice that the “Value of ” portion of the string is eliminated, and the number seems to be formatted as indicated by the “0000.00” with the string “too high” appended to the end.
PREFIX option will allow you to put a character string before the numbers. Replacing the code above with the following will fix the error and display the intended output:
proc format; picture testc Low-<1000='099.00' 1000-high='0000.00 is too high' (prefix='Value of '); run; data _null_; format val testc.; input val; put val=; datalines; 3.1452342 2899.96543 ; run;
val=03.14 val=Value of 2899.96 is too high
5. Creating a Picture Format (NOEDIT Option)
NOEDIT option can be used to avoid collisions with the data in the PICTURE format.
In the example below, TESTed has, suppose as an alternative to “Value of 0000.00 is too high”, you want to show the text “Value >1000 is too high.”
proc format; picture testd Low-<1000='099.00' 1000-high='Value >1000 too high' (noedit); run; data _null_; format val tested.; input val; put val=; datalines; 3.1452342 2899.96543 ; run;
val=03.14 val=Value >1000 too high
6. Picture formats in SAS dates
If you wish to specify a date, time, or DateTime in a fashion not conforming to one of many built-in formats, a picture format stands out as the resolution.
The PICTURE statement expects a quoted string defining a template for formatting a numeric value.
When that numeric value happens to be a date, time, or DateTime value, a set of special character codes known as directives are available to specify the various date and time components as a part of the picture definition.
To use these directives, the
DATATYPE= option has to be included in the PICTURE statement with a value of DATE, TIME, or DATETIME to indicate the specific type of value being formatted.
|%a||Short weekday name||Mon|
|%A||Full weekday name||Monday|
|%b||Short month name||Jan|
|%B||Full month name||January|
|%d||Day of the month (1-31)*||14|
|%H||Hour, 24-hour clock (0-23)*||22|
|%I||Hour, 12-hour clock (1-12)*||8|
|%j||Day of the year, number (1-366)*||22|
|%m||Month as a number (1-12)*||1|
|%p||AM or PM||PM|
|%q||A quarter of the year, numbers (1-4)||1|
|%Q||A quarter of the year (e.g. “Quarter4”)||Quarter1|
|%u||Day of week (1-7, Sunday=7)*||1|
|%U||Week number (0-53, by Mondays)*||1|
|%w||Day of week (0-6, Sunday=0)*||1|
|%W||Week number (0-53, by Sundays)*||3|
|%%||Escape code for % character|
proc format; picture mydt low - high='%Y-%b-%0d (W%W:D%w)' (datatype=date); run; data _null_; dt="16MAY2020"d; format dt mydt20.; put dt; run;
Using the put statement proven below to apply this format to the date value of May 17, 2020, results in the output shown below.