SAS proc制表,按特定变量排序

时间:2019-10-17 10:14:04

标签: sas proc tabulate

我使用虚构的数字,因为我无法发布原始数据集。我有以下SAS代码:

PROC TABULATE DATA=table;
TITLE 'Offences per country and year;
CLASS year country offence_type;
TABLES country, year ALL, region /BOX="" MISSTEXT='0';
RUN;

这给了我每个国家一张这样的桌子。

SWITZERLAND
year | theft | robbery | assault | …
------------------------------------
ALL  |  1302       329       100   …
1980 |   321       100        24   … 
1981 |   280        24        20   … 
   … |     …         …         …   … 

现在,我想更改犯罪类型/列的顺序。最常出现的犯罪类型(全年)应显示在左侧,不常出现的犯罪类型应显示在左侧 右侧(如上例所示)。

/ ORDER=FREQ声明之后,我已经尝试过TABLES,但是随后的年份顺序也被更改了。

有人可以帮忙吗?谢谢!

1 个答案:

答案 0 :(得分:1)

在使用offence_type选项的单独CLASS语句中指定ORDER=FREQ变量。

示例:

  • 第一个表格显示班级顺序是数据升序,
  • 第二个列表显示了CLASS <var>/ ORDER=FREQ的效果。
  • 第三列表显示了BY <group-vars>的附加效果(以及避免每个BY组上方标题的一些技巧)。

ods listing;

options 
  pagesize=1000
  nodate nonumber nocenter
  formdlim=' '
  formchar="|----|+|---+=|-/\<>*"
;

dm 'listing; clear';

data have(index=(country));
  do country = 'Zeroland   ', 'Switzerland', 'Crimeland';
    do year = 1980 to year(today());
      date = mdy(1,1,year);
      do date = date to intnx('year',date,1);
        crime = scan ('theft robbery assault parking', min(4,rand('geometric', 0.6)));

        /***********************************************\
        |* edit, make robbery predominant in Crimeland *|
        \***********************************************/
        if country = 'Crimeland' then do;
          crime = scan ('robbery theft assault parking', min(4,rand('geometric', 0.6)));
        end;

        output;
      end;
    end;
  end;

  format date date9.;
run;

塔布拉茨

proc tabulate data=have NOSEPS format=8.;
  title1 "Tabulate: default class ordering Alphabetical";
  *where country like 'S%' and year < 1984;
  where year < 1982;
  class country year ;
  class crime;
  tables country, ALL year, crime / condense;
run;

proc tabulate data=have NOSEPS format=8.;
  title1 "Tabulate: CLASS / order=freq - crimes Frequency Descending";
  title2 "freq ordering based on all data";
  * where country like 'S%' and year < 1984;
  where year < 1982;

  class country year ;
  class crime / order=freq;
  tables ALL country, ALL year, crime / condense;
run;

data for_tabulate(index=(country));
  set have;
  title1 = 'NOBYLINE, TITLE in Data. / order=freq by group';
  title2 = 'Country ' || country;
run;

options nobyline;

proc tabulate data=for_tabulate NOSEPS format=8.;
  title;
  * where country like 'S%' and year < 1984;
  where year < 1982;

  by country;

  class title1 title2 year ;
  class crime / order=freq;
  tables 
    title1 * title2,
    ALL year,
    crime 
    / condense
    ;
run;

列出输出

字母

Tabulate: default class ordering Alphabetical

country Crimeland
------------------------------------------------------------
|                      |               crime               |
|                      |-----------------------------------|
|                      |assault |parking |robbery | theft  |
|                      |--------+--------+--------+--------|
|                      |   N    |   N    |   N    |   N    |
|----------------------+--------+--------+--------+--------|
|All                   |      69|      50|     438|     176|
|year                  |        |        |        |        |
|1980                  |      39|      29|     214|      85|
|1981                  |      30|      21|     224|      91|
------------------------------------------------------------

country Switzerland
------------------------------------------------------------
|                      |               crime               |
|                      |-----------------------------------|
|                      |assault |parking |robbery | theft  |
|                      |--------+--------+--------+--------|
|                      |   N    |   N    |   N    |   N    |
|----------------------+--------+--------+--------+--------|
|All                   |      70|      44|     178|     441|
|year                  |        |        |        |        |
|1980                  |      42|      27|      84|     214|
|1981                  |      28|      17|      94|     227|
------------------------------------------------------------

country Zeroland
------------------------------------------------------------
|                      |               crime               |
|                      |-----------------------------------|
|                      |assault |parking |robbery | theft  |
|                      |--------+--------+--------+--------|
|                      |   N    |   N    |   N    |   N    |
|----------------------+--------+--------+--------+--------|
|All                   |      74|      38|     189|     432|
|year                  |        |        |        |        |
|1980                  |      36|      25|      89|     217|
|1981                  |      38|      13|     100|     215|
------------------------------------------------------------

频率(全部)下降

Tabulate: CLASS / order=freq - crimes Frequency Descending
freq ordering based on all data

All
------------------------------------------------------------
|                      |               crime               |
|                      |-----------------------------------|
|                      | theft  |robbery |assault |parking |
|                      |--------+--------+--------+--------|
|                      |   N    |   N    |   N    |   N    |
|----------------------+--------+--------+--------+--------|
|All                   |    1049|     805|     213|     132|
|year                  |        |        |        |        |
|1980                  |     516|     387|     117|      81|
|1981                  |     533|     418|      96|      51|
------------------------------------------------------------

country Crimeland
------------------------------------------------------------
|                      |               crime               |
|                      |-----------------------------------|
|                      | theft  |robbery |assault |parking |
|                      |--------+--------+--------+--------|
|                      |   N    |   N    |   N    |   N    |
|----------------------+--------+--------+--------+--------|
|All                   |     176|     438|      69|      50|
|year                  |        |        |        |        |
|1980                  |      85|     214|      39|      29|
|1981                  |      91|     224|      30|      21|
------------------------------------------------------------

country Switzerland
------------------------------------------------------------
|                      |               crime               |
|                      |-----------------------------------|
|                      | theft  |robbery |assault |parking |
|                      |--------+--------+--------+--------|
|                      |   N    |   N    |   N    |   N    |
|----------------------+--------+--------+--------+--------|
|All                   |     441|     178|      70|      44|
|year                  |        |        |        |        |
|1980                  |     214|      84|      42|      27|
|1981                  |     227|      94|      28|      17|
------------------------------------------------------------

country Zeroland
------------------------------------------------------------
|                      |               crime               |
|                      |-----------------------------------|
|                      | theft  |robbery |assault |parking |
|                      |--------+--------+--------+--------|
|                      |   N    |   N    |   N    |   N    |
|----------------------+--------+--------+--------+--------|
|All                   |     432|     189|      74|      38|
|year                  |        |        |        |        |
|1980                  |     217|      89|      36|      25|
|1981                  |     215|     100|      38|      13|
------------------------------------------------------------

频率(按组划分)降序

title1 NOBYLINE, TITLE in Data. / order=freq by group
and title2 Country Crimeland
------------------------------------------------------------
|                      |               crime               |
|                      |-----------------------------------|
|                      |robbery | theft  |assault |parking |
|                      |--------+--------+--------+--------|
|                      |   N    |   N    |   N    |   N    |
|----------------------+--------+--------+--------+--------|
|All                   |     438|     176|      69|      50|
|year                  |        |        |        |        |
|1980                  |     214|      85|      39|      29|
|1981                  |     224|      91|      30|      21|
------------------------------------------------------------

title1 NOBYLINE, TITLE in Data. / order=freq by group
and title2 Country Switzerland
------------------------------------------------------------
|                      |               crime               |
|                      |-----------------------------------|
|                      | theft  |robbery |assault |parking |
|                      |--------+--------+--------+--------|
|                      |   N    |   N    |   N    |   N    |
|----------------------+--------+--------+--------+--------|
|All                   |     441|     178|      70|      44|
|year                  |        |        |        |        |
|1980                  |     214|      84|      42|      27|
|1981                  |     227|      94|      28|      17|
------------------------------------------------------------

title1 NOBYLINE, TITLE in Data. / order=freq by group
and title2 Country Zeroland
------------------------------------------------------------
|                      |               crime               |
|                      |-----------------------------------|
|                      | theft  |robbery |assault |parking |
|                      |--------+--------+--------+--------|
|                      |   N    |   N    |   N    |   N    |
|----------------------+--------+--------+--------+--------|
|All                   |     432|     189|      74|      38|
|year                  |        |        |        |        |
|1980                  |     217|      89|      36|      25|
|1981                  |     215|     100|      38|      13|
------------------------------------------------------------