Select Page

In this post, I will investigate PROC PLAN in SAS. Few programmers know about the Plan Procedure. However, it is nice to have in the back of your head when you want to generate data that must follow a certain pattern. The data can be replicated with Data Step code. However, the Plan Procedure provides short, simple syntax for creating both simple and advanced data. In this post, I will investigate the Plan Procedure by examples. Purposely, I will not go into detail with the procedure statements. The procedure is very well documented: Proc Plan Documentation.

A Simple Example

First, let us look at a simple example. Suppose, I want to generate the numbers 1 to 10 in random order. Most SAS programmers would solve this problem in a data step. A Data Step approach could look like this

```data want (keep=a); array r {10} _temporary_ (1 : 10); h = 10; do _N_ = 1 to 10; i = rand ("integer", h); a = r [i]; output; r [i] = r [h]; h = h-1; end; stop; run;```

The data step above is inspired by the post Random Sampling in SAS Without Replacement. The data step gets the job done. However, it can be done with much less code in PROC PLAN. Let us take a look.

```proc plan; factors a=10 / noprint; output out=want; run;quit;```

In the code above, I use the Factors Statement and specify a=10. This means that the procedure generates the numbers 1 to 10 and returns them in random order. I use the Noprint Option to suppress the HTML Output. Also, I use the Output Statement to generate a SAS Data Set instead. Not only is the code shorter. It is also easily extendable. Suppose that I want to repeat this pattern three times. All I need to is below. Here, I use the ordered keyword to specify that a should be ordered ascending. For each a, I generate the numbers 1 to 10 in random order. Run the code and verify the result.

```proc plan; factors a=3 ordered b=10 / noprint; output out=want; run;quit;```

Generate Permutations

Recently, I blogged about generating Permutations in the SAS Data Step. However, the data step is not the only way to generate permutations. The PROC PLAN below generates all possible permutations of the numbers 1 to 4. Simple combinatorics show that this can be done in 4!=24 ways. Therefore, I specify a=24 and b=4. Finally, I specify the Perm option, which relates to b. Run the code and verify the result. As you can see, the HTML output is not the same as the output SAS data set. To get them in the same format, we have to transpose the data.

```proc plan; factors a = 24 ordered b = 4 perm; output out=want; run;quit;   proc transpose data=want out=want; by a; var b; run;```

Cyclical Design

Finally, let us take a look at generating a cyclical design with PROC PLAN. The procedure below creates the values 1, 2, 3, 4, 5 in the first iteration. Next, it generates 2, 3, 4, 5, 1. This follows in a cyclical manner forward. For demonstration, I generate six cycles below. Obviously, the sixth cycle restores the original order of the values.

```proc plan; factors a = 6 ordered b = 5 cyclic; output out=want; run;quit;```

Summary

In this post, we have seen a few examples of generating data or plans with PROC PLAN. Obviously, we only scratch the surface of how advanced designs the Plan Procedure can handle. However, the syntax stays nice and simple. I highly encourage you to play around with the examples and read the documentation carefully. The key to understand the Plan Procedure is to understand the Factor Statement. When browsing the syntax, that should be your first order of business.

I have used the Plan Procedure to generate example data in the posts An Array Hashing Scheme in SAS and An Array Hashing Scheme in SAS and Bitmapping in the SAS Data Step. For another rarely used procedure in a data science context, see the post, Identify Connected Components with PROC OPTNET in SAS.