Search code examples
sasproctabulate

Group rows in PROC TABULATE


I have the following (fake) crime data of offenders:

/* Some fake-data */
DATA offenders;
INPUT id :$12. crime :4. offenderSex :$1. count :3.;
INFORMAT id $12.;
INFILE DATALINES DSD;
DATALINES;
1,110,f,3
2,32,f,1
3,31,m,1
4,113,m,1
5,110,m,1
6,31,m,1
7,31,m,1
8,110,f,2
9,113,m,1
10,31,m,1
11,113,m,1
12,110,f,1
13,32,m,1
14,31,m,1
15,31,m,1
16,31,m,1
17,110,f,2
18,113,m,2
19,31,m,1
20,31,m,1
21,110,m,4
22,32,f,1
23,31,m,1
24,31,m,1
25,110,f,4
26,110,m,1
27,110,m,1
28,110,m,2
29,32,m,1
30,113,f,1
31,32,m,1
32,31,f,1
33,110,m,1
34,32,f,1
35,113,m,2
36,31,m,1
37,113,m,1
38,110,f,1
39,113,u,2
;
RUN;

proc format;
 value crimes 110 = 'Theft'
              113 = 'Robbery'
              32 = 'Assault'
              31 = 'Minor assault';
run;

I want to create a cross table using PROC TABULATE:

proc tabulate;
format crime crimes.;
freq count;
class crime offenderSex;
table crime="Type of crime", offenderSex="Sex of the offender" /misstext="0";
run;

This gives me a table like this:

                      m   f
------------------------------------
Minor assault   |
Assault         |
Theft           |
Robbery         |

Now, I'd like to group the different types of crimes:

'Assault' and 'minor assault' should be in a category "Violent crimes" and 'theft' and 'robbery' should be in a category "Crimes against property":

                             m   f
------------------------------------
Minor assault             |
Assault                   |
*Total violent crimes*    |
Theft                     |
Robbery                   |
*Total property crimes*   |

Can anyone explain me how to do this? I tried to use another format for the 'crime'-variable and use "category * crime" within PROC TABULATE, but then it turned out like this, which is not exactly what I want:

                                               m   f
-------------------------------------------------------
Violent crimes   Minor assault           |
                 Assault                 |
Property crimes  Theft                   |
                 Robbery                 |

Solution

  • Use the all= option within a table dimension :

    table group='Category' * (crime="Type of crime" All='Total'), offenderSex="Sex of the offender" /misstext="0";