Proc Format lets us write user defined formats and informats in SAS. The Format Procedure is very flexible. Today, I will demonstrate a trick, that lets us embed one format within another. This is quite easy to do. However, not many SAS programmers are aware of it.

Embed One Format Within Another in SAS

Let us see how to use the Value Statement to created nested formats. In the Proc Format below, I create two numeric formats. Inner and Outer. Not surprisingly, the goal is to embed Inner within Outer. First, we create the inner format. This is no different from the the standard approach to creating a numeric format. Next, I create the outer format. Here, I create a simple mapping for the numbers 1 and 2. However, for the range 500 –< 1000, I do not specify values explicitly. Instead, I use squared brackets and specify the inner format within it.

proc format;
   value inner
      500 -< 600 = 'Over 500'
      600 -< 700 = 'Over 600'
      700 -< 800 = 'Over 700'
      800 -< 900 = 'Over 800'
      other      = 'Over 900';
   value outer
      1           = 'One'
      2           = 'Two'
      500 -< 1000 = [inner.]
      other       = [8.];
run;

Next, let us put the nested format to work. In the data step below, I simply put in some numbers to the variable x. Then, I put the formated value of x in the log.

data have;
input x;
put x outer.;
datalines;
1    
2    
3    
200  
300  
400  
500  
600  
700  
800  
900  
1000 
;

The data step above prints the following in the log. The format works as expected. Notice that when the values are between 500 and 1000, the Proc Format applies the logic from the inner format. Not the outer.

One
Two
    3.00
  200.00
  300.00
  400.00
Over 500
Over 600
Over 700
Over 800
Over 900
 1000.00

Summary

In this short post, we learn how to nest a format within another in SAS. This technique makes it possible to write even more flexible formats in SAS. One of the many reasons to love the procedure. If you want to learn more about Proc format, see one of my related posts below. Furthermore, my usual reference in the great book The Power of PROC FORMAT by Jonas Bilenas.

Related Posts:

Write User Defined Formats in SAS with PROC FORMAT
Proc Format Lookup Example
5 SAS Picture Format Options You Should Know
Pass Function Logic to PROC FORMAT
Picture Formats For Character Variables in SAS

You can download the entire code from this post here.