sas SAS Formats Using the format statement to group data


Example

You can apply formats within a procedure, e.g. to change the groupings within a proc summary or proc freq.


Grouping SAS dates

data example2 ;
  do Date = '01JUN2016'dt to '31AUG2016'dt ;
    Days = 1 ;
    output ;
  end ;
run ;

/* Summarise by year & month */
proc summary data=example2 nway ;
  class Date ;
  var Days ;
  output out=example2_sum (drop=_TYPE_ _FREQ_) sum= ;
  format Date yymmn6. ; /* e.g. 201606 */
run ;
DateDays
20160630
20160731
20160831
/* Summarise by month & year */
proc summary data=example2 nway ;
  class Date ;
  var Days ;
  output out=example2_sum2 (drop=_TYPE_ _FREQ_) sum= ;
  format Date monyy7. ; /* e.g. JUN2016 */
run ;
DateDays
JUN201630
JUL201631
AUG201631

The benefit of using a format is that the natural sort order is retained.


Using sashelp.class as an example, say you wanted to compare the frequency of the first letter of each name. You could use the substr() function to find the first letter, and run a proc freq on the new variable. Alternatively, you can apply the $1. format to the Name variable :

proc freq data=sashelp.class ;
  table Name ;
  format Name $1. ;
run ;
NameCOUNT
A7
B4
C2
etc.