在 SAS 中用最小值旋转 table

Pivot table with minimum in SAS

我有一个 table 看起来像这样 :

data work.Hotels;
Length Hotel_Name  Class  ;
INPUT Hotel_Name $ Class $ Country $ PRICE ;
CARDS ;
Appolpon A Greece 390
Caravel B Greece 468
Christina A Greece 427
Economy B Greece 369
EdenBeach A Greece 499
HanikianBeach C Greece 526
MarinaBeach C Greece 587
Xenia C Greece 534
Agdal B Maroc 447
Almohades B Maroc 482
Atlas A Maroc 511
AtlasArnadi C Maroc 532
Chems C Maroc 450
Dunes A Maroc 569
AlfaMa B Portu 646
AppDo B Portu 652
DELagos C Portu 802
Madeira A Portu 761
Reid's A Portu 1101
;
run;

我正在尝试生成一个 table,其中国家/地区为行,Class 为列,对于每一对,价格最低的酒店名称。像这样:

        A           B           C
Greece  Appolpon    Caravel     Marinabeach
Portu   Madeira     Appdo       Delagos
Maroc   Atlas       Agdal       Chems

我尝试了很多东西,比如 Proc tabulate,但我没有得到任何好的结果。

谢谢

您需要对数据进行预处理,以便每个单元格只有一个值(即每个国家/地区+class 的组合)。那时 PROC REPORT 和跨列堆叠规范将在单元格中显示名称。为了强制 REPORT 进入这种布局模式,还需要一个偷偷摸摸的技巧。

示例:

* order data for selecting row of lowest priced county+class;

proc sort data=have out=ordered;
  by country class price name;
run;

* if two lowest prices in a cell are the same select the (alphabetically) first name ;

data lowlowprices;
  set ordered;
  by country class;
  if first.class;  * save first lowest price within the by group;
run;

proc report data=lowlowprices;
  columns country class,name sneaky;  * tip <varname>,<varname> specifies a stacked column ;
  define country / group;
  define class / across;
  define sneaky / noprint;
run;

生产

Excel 数据透视表可能会对数据进行预处理,因为 Excel 中有您想要完成的技巧。