Monday, 10 September 2012

NOTE: New Ways in Means

There aren't many SAS programmers who haven't used PROC MEANS (or its alter ego PROC SUMMARY) to aggregate (summarise) their data. PROC MEANS is a familiar tool for most SAS programmers, and the process of subsequently selecting specific values of _TYPE_is an equally familiar if slightly more painful activity. If you've ever struggled to select the correct _TYPE_ value(s) then I wonder if you know of the CHARTYPE parameter.

Traditionally, SAS software created _TYPE_ as a numeric variable, and we had to select values based upon a calculation or by use of bit strings. However, version 8 introduced the CHARTYPE parameter to PROC MEANS. CHARTYPE tells PROC MEANS to create _TYPE_ as a character variable. This makes the select process much easier. Compare the following snippets of code.

/* Old Style */
proc means ...
  class continent country state town;
  output out=old ...
[snip]
data olddemo;
  set old;
  select (_type_);
    when (5) /* country & town */
    [snip]
    when ('1010'b) /* continent & state */
[snip]

/* New Style */

proc means chartype ...
  class continent country state town;
  output out=old ...
[snip]
data olddemo;
  set old;
  select (_type_);
    when ('0101') /* country & town */
    [snip]
    when ('1010') /* continent & state */
[snip]

As you can see, you can select rows which contain your desired class column combination by simply specifying 1's for the columns you want and 0's for those you don't. The length of the _TYPE_ variable is determined by the number of CLASS variables.

If you'd like to learn more about PROC MEANS, I recommend Andrew Karp's excellent SAS User Group International (SUGI) paper titled "Advanced Tips and Techniques with PROC MEANS" from SUGI 27. Andrew covers CHARTYPE and much, much more - all with excellent examples.