DATA Step, Macro, Functions and more

Group and subgroups coding

Accepted Solution Solved
Reply
Contributor
Posts: 21
Accepted Solution

Group and subgroups coding

Hi,

 

I am trying to write a code whose output has the follwing format:

 

Group1                   Count     %

    Subgroup1         Count     %

    Subgroup2         Count     %

 

Group2                  Count     %

    Subgroup3         Count     %

    Subgroup4         Count     %

where subgroups are essentially a part of the groups.

 

Is there an easy way to do this?

 

Thanks,

Neha.


Accepted Solutions
Solution
‎06-07-2016 01:28 PM
Super User
Posts: 9,662

Re: Group and subgroups coding

You didn't give us some data to test yet ?

 


proc report data=sashelp.class nowd ;
columns sex age new_age pctn;
define sex/group noprint;
define age/group noprint;
define new_age/computed style={asis=on};
define pctn/format=percent8.2;
compute new_age/character length=20;
 new_age=cats('09'x,put(age,best8.));
endcomp;
compute before sex;
 new_age=sex;
endcomp;
break before sex/summarize;
run;

x.png

View solution in original post


All Replies
Super User
Posts: 10,466

Re: Group and subgroups coding

Some applications support what is referred to as a MULTILABEL format to do such things. Only a few procedures such as Means, Summary and Tabulate support them.

 

Here are some examples with proc tabulate. Note that different options and how the format is defined affect the output.

/* To demonstrate that 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;
 

Solution
‎06-07-2016 01:28 PM
Super User
Posts: 9,662

Re: Group and subgroups coding

You didn't give us some data to test yet ?

 


proc report data=sashelp.class nowd ;
columns sex age new_age pctn;
define sex/group noprint;
define age/group noprint;
define new_age/computed style={asis=on};
define pctn/format=percent8.2;
compute new_age/character length=20;
 new_age=cats('09'x,put(age,best8.));
endcomp;
compute before sex;
 new_age=sex;
endcomp;
break before sex/summarize;
run;

x.png

PROC Star
Posts: 1,558

Re: Group and subgroups coding

Like this?

 

proc tabulate data=SASHELP.CLASS;
  class SEX AGE;
  table SEX *(all AGE=' '),pctn;
run;

 

 

 


  PctN
Sex   47.37
F All
11 5.26
12 10.53
13 10.53
14 10.53
15 10.53
M All 52.63
11 5.26
12 15.79
13 5.26
14 10.53
15 10.53
16 5.26
☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 3 replies
  • 256 views
  • 2 likes
  • 4 in conversation