This sort of nested definition is one of the places that MULTILABEL formats can be useful. The limitation of these is that one a few procedures will use them but since Proc Summary/Means, Report and Tabulate will then counting is quite possible.
Strangely enough the example below also uses a Vital Statistics example though the data is artificial and you generate it.
This example was designed more to show ways that options affect appearance of reports in Proc Tabulate but the approach should be similar. Part of this exercise is to show that the way the Multilabel format is defined affects appearance as in order of rows in this case for same values.
/* To demonstrate how the order of definition affects appearance in
multilabel formats. Also appearance options to show the spaces to
get the indent as desired and fix column widths.
And investigate whether class level format based style overrides work
with MLF in proc tabulate. Result: NO.
*/
proc format library=work;
value accidentl (multilabel notsorted)
1-5 = 'Accidents'
1-3 = ' Transport accidents'
1 = ' Motor vehicle accidents'
2 = ' Water, air, and space'
3 = ' Other land transport accidents'
4-5 = ' Nontransport accidents'
5 = ' Fishing'
;
value accidentr (multilabel notsorted)
1-5 = 'Accidents'
1-3 = ' Transport accidents'
4-5 = ' Nontransport accidents'
1 = ' Motor vehicle accidents'
2 = ' Water, air, and space'
3 = ' Other land transport accidents'
5 = ' Fishing'
;
value accidents (multilabel notsorted)
1 = ' Motor vehicle accidents'
2 = ' Water, air, and space'
3 = ' Other land transport accidents'
5 = ' Fishing'
1-5 = 'Accidents'
1-3 = ' Transport accidents'
4-5 = ' Nontransport accidents'
;
value mf
1 = "Male"
2 = "Female"
;
value mycolor
1='white'
2='red'
3='green'
4='blue'
5='orange'
6='purple'
;
value accsimple
1 = 'Motor vehicle accidents'
2 = 'Water, air, and space'
3 = 'Other land transport accidents'
4 = 'Nontransport accidents'
5 = 'Fishing'
;
value MyColorl (multilabel notsorted)
1-5 = 'white'
1-3 = 'red'
1 = 'blue'
2 = 'orange'
3 = 'pink'
4-5 = 'purple'
5 = 'black'
;
run;
/* populate a dataset to display */
/* This specifically does NOT generate any data for FISHING above*/
/* to display the behavior of the options below in those cases. */
data junk;
do i=1 to 50;
type = round(4*ranuni(1234)+.5);
sex = round(2*ranuni(3455)+.5);
output;
end;
run;
/* Notice that before we get here the data is NOT sorted */
/* in any manner!!!! */
title 'Option Order=Data Preloadfmt Printmiss Misstext=0 format accidentl';
proc tabulate data=junk order=data ;
class type / mlf PRELOADFMT;
/*ASIS preserves the leading spaces in the format, Cellwidth here is optional
for this demo. These will ONLY affect ODS output such as HTML, RTF or PDF
not the OUTPUT window
*/
classlev type/ style=[asis=on cellwidth=1.5in];
class sex;
/* the formatted values of SEX normally take up different lengths, this fixes
the column widths to be the same for both headers. May cause interesting
appearances with large numbers of displayed digits if requested in formats*/
classlev sex/ style=[cellwidth=.5in];
/* to get the ALL to have the same width as SEX need to specify this way*/
table type=' ', all='Total'*{style=[cellwidth=.5in]}*n=' '*f=comma9. sex=' '*n=' '*f=comma8.
/ printmiss misstext='0';
format type accidentl. sex mf.;
run;title;
title 'Option simple format and classlev background color';
proc tabulate data=junk order=data ;
class type / ;
/*ASIS preserves the leading spaces in the format, Cellwidth here is optional
for this demo. These will ONLY affect ODS output such as HTML, RTF or PDF
not the OUTPUT window
*/
classlev type/ style=[asis=on cellwidth=1.5in background=mycolor.];
class sex;
/* the formatted values of SEX normally take up different lengths, this fixes
the column widths to be the same for both headers. May cause interesting
appearances with large numbers of displayed digits if requested in formats*/
classlev sex/ style=[cellwidth=.5in];
/* to get the ALL to have the same width as SEX need to specify this way*/
table type=' ', all='Total'*{style=[cellwidth=.5in]}*n=' '*f=comma9. sex=' '*n=' '*f=comma8.
/ row=float misstext='0';
format type accsimple. sex mf.;
run;title;
title 'Option Order=Data Preloadfmt Printmiss Misstext=0 format accidentl classlev background';
proc tabulate data=junk order=data;
class type / mlf PRELOADFMT;
classlev type/ style=[asis=on cellwidth=1.5in background=mycolor.];
class sex;
classlev sex/ style=[cellwidth=.5in];
table type=' ', all='Total'*{style=[cellwidth=.5in]}*n=' '*f=comma9. sex=' '*n=' '*f=comma8.
/ printmiss misstext='0';
format type accidentl. sex mf.;
run;title;
title 'Option Order=Data Preloadfmt Printmiss Misstext=0 format accidents';
proc tabulate data=junk order=data;
class type / mlf PRELOADFMT;
classlev type/ style=[asis=on cellwidth=1.5in];
class sex;
classlev sex/ style=[cellwidth=.5in];
table type=' ', all='Total'*{style=[cellwidth=.5in]}*n=' '*f=comma9. sex=' '*n=' '*f=comma8.
/ printmiss misstext='0';
format type accidents. sex mf.;
run;title;
... View more