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 ;
Date | Days |
---|---|
201606 | 30 |
201607 | 31 |
201608 | 31 |
/* 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 ;
Date | Days |
---|---|
JUN2016 | 30 |
JUL2016 | 31 |
AUG2016 | 31 |
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 ;
Name | COUNT |
---|---|
A | 7 |
B | 4 |
C | 2 |
etc. |