In the blog post Writing User Defined Formats in SAS with PROC FORMAT, I briefly introduce Picture Formats. Picture formats are templates for printing numbers. They are extremely flexible if you use them correctly. To do so, you should familiarize yourself with the options that form them. I am going to assume that you have basic knowledge of picture formats and know about the 0’s and 9’s (digit placeholders). If not, I encourage you to browse the PROC FORMAT Picture Statement Documentation.
There are two kinds of picture format options. The kind that controls the attributes of each picture in the format and the kind that controls the overall attributes. This post presents two of the first kind and three of the last kind. In my opinion, these are the five most important options in the world of picture formats.
In the examples to come, I will introduce the picture format options with the data set below. I use a simple PROC PRINT to demonstrate how the picture format options affect the printing of numbers.
data MyData; input num; datalines; 10.999 -100.603 0 100 1000.333 ;
When we create a picture format and the number of decimal placeholders is smaller than the actual number of decimals, SAS truncates the number by default. For example, take a look at the first number in MyData. This is the number 10.999. If you want to print this number like 11.0, i.e. round the number to the nearest decimal, you have to use the Round Option. Otherwise, SAS truncates the number. This is a feature that has confused many programmers over time.
proc format; picture MyFmt (round) low-high = '000,009.9'; run; proc print data=MyData; format num MyFmt.; run;
Default Length Option
Next, let us consider the length of a picture format. By default, SAS sets the length to be the number of placeholders in the format. Consequently, MyFmt has a default length of 9. We can easily increase the default length of the picture format with the Default= Option. If you run the code below, you will see that the default length is now 20. Always specify a suitable default length of your picture format. Otherwise, you may not get the desired result.
proc format; picture MyFmt (round default=20) low-high = '000,009.9'; run; proc print data=MyData; format num MyFmt.; run;
The Round and Default Options relate themselves to the entire format. Next, let us turn to the option, that relates to the individual picture.
In the picture format below, I create two pictures. One for values less than zero and one for zero and above. I use the Prefix Option to set a prefix in each case. Here, I use a minus and a dollar sign for the negative values and just a dollar sign for positive values. Remember, that the default length of the format should allow for such prefixes.
proc format; picture MyFmt (round default=20) low-<0 = '000,009.9' (prefix='-$') 0-high = '000,009.9' (prefix='$'); run; proc print data=MyData; format num MyFmt.; run;
The Multiplier Option multiplies the number by the number specified in the option. The Multiplier Option is ideal for reporting derivatives of numbers without creating a new variable. Let us assume that the numbers in MyData are dollar values, and we want them reported in Danish Crones. I now change the prefix to ‘DKK’ and specify 6.5 in the Multiplier Option. This is roughly the conversion rate between the two currencies.
proc format; picture MyFmt (round default=20) low-<0 = '000,009.9' (prefix='DKK -' multiplier=6.5) 0-high = '000,009.9' (prefix='DKK ' multiplier=6.5); run; proc print data=MyData; format num MyFmt.; run;
The NoEdit Option lets you escape the rules set up by the other picture format options. You can use the NoEdit Option when you simply want to display some constant value for specific ranges. For example, if you want to simply display a ‘-‘ for the exact value of zero. No prefixes and no multipliers. This is where the NoEdit Option has its power.
proc format; picture MyFmt (round default=20) low-<0 = '000,009.9' (prefix='DKK -' multiplier=6.5) 0 = '-' (noedit) 0<-high = '000,009.9' (prefix='DKK ' multiplier=6.5); run; proc print data=MyData; format num MyFmt.; run;
In this post, we have explored the Picture Format Options Round, Default, Prefix, Multiplier, and NoEdit. We have learned that using the options with care can vastly improve our picture formats and their flexibility. However, it should be mentioned, that the good SAS programmer always browses the existing formats before rolling out their own. Literally thousands of display formats already exist and the few minutes searching for a suiting format is well spend.
I devote an entire post to the Multilabel Option in the blog post Creating Multilabel Format in SAS with PROC FORMAT. Also, see the post Write Custom Date and Datetime Formats in SAS.
If you are interested in learning more about the Format Procedure in SAS, I recommend the book The Power of PROC FORMAT by Jonas V. Bilenas.
You can download the entire code from this post here.