6 Sas Picture Format Tips You Need To Learn Now

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 a number of display problems with numbers like:-

  1. Decimal and Comma Placement
  2. Embedding Characters with Numbers
  3. Prefixes

PICTURE statement in <a href="https://www.9to5sas.com/proc-format-in-sas/">PROC FORMAT</a> allows you to leverage the facility of PROC FORMAT. You could make labels look precisely the way you would like them to look.

A picture format works identically the same as a primary format on the left-hand side (eg. the specification of ranges and values), the difference falls on the right-hand side (eg. the label display).

For the label display, there are three things to know,

  1. Using a ‘9’ (or any non-zero number) as a digit selector will drive the number to appear (i.e. will include leading zeros),
  2. Using a 0 as a digit selector will solely display the number if that digit is within the number (i.e. no leading zeros),
  3. Using a character string will display that character string 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 will be able to make the right parentheses as a part of the template itself since it’s clearly 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;

Picture-format

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 is 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;

6 Sas Picture Format Tips You Need To Learn Now2. Creating a Picture Format (Character Display)

A character string might be displayed for certain values while displaying numbers for the other values. A format like this can be used:

proc format;
 picture temp
 low-36 = "Invalid Temperature"
 36.1-

Picture format
3. Creating a Picture Format (Round Option)

By default, when a picture format is applied, it truncates the data. (As a side note, by default when an existing SAS format is applied like 5.1 or 7.3, the data is rounded.) In general, rounding is preferred to truncating.

To make a picture format round as an alternative to truncating, we merely add the ROUND option:

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;

Output:

 val=3.15
val=2.97

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-

Output:

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.

The 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-

Output:

val=03.14
val=Value of 2899.96 is too high

5. Creating a Picture Format (NOEDIT Option)

The NOEDIT option can be used to assist avoid some collisions with the data in the PICTURE format.

In the example below TESTd 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 too high' (noedit)
	;
run;

data _null_; 
format val tested.;
input val; 
put val=;
datalines; 
3.1452342 
2899.96543
;
run;

Output:

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.

Directive Description Example
%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
%M Minute (0-59)* 45
%p AM or PM PM
%q A quarter of the year, number (1-4) 1
%Q A quarter of the year (e.g. “Quarter4”) Quarter1
%s Fractional seconds 223
%S Seconds* 23
%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
%y Year, two-digit* 20
%Y Year, four-digit 2020
%% 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.

Output:

2020-May-16 (W19:D7)

Every week we'll send you SAS tips and in-depth tutorials

JOIN OUR COMMUNITY OF SAS Programmers!

Subhro Kar is an Analyst with over five years of experience. As a programmer specializing in SAS (Statistical Analysis System), Subhro also offers tutorials and guides on how to approach the coding language. His website, 9to5sas, offers students and new programmers useful easy-to-grasp resources to help them understand the fundamentals of SAS. Through this website, he shares his passion for programming while giving back to up-and-coming programmers in the field. Subhro’s mission is to offer quality tips, tricks, and lessons that give SAS beginners the skills they need to succeed.

1 thought on “6 Sas Picture Format Tips You Need To Learn Now”

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Share via
Copy link
Powered by Social Snap
%d bloggers like this: