0

我使用的是虚构数字,因为我无法发布原始数据集。我有以下 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=FREQTABLES-statement 之后尝试过,但是年份的顺序也发生了变化。

任何人都可以帮忙吗?谢谢!

4

1 回答 1

1

在使用该选项offence_type的单独语句中指定变量。CLASSORDER=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|
------------------------------------------------------------
于 2019-10-17T12:43:24.737 回答