我有以下示例数据:
DATA offenders;
INPUT id :$12. age :3. sex :$1. residenceStatus :$1.;
INFORMAT id $12.;
INFILE DATALINES DSD;
DATALINES;
1,30,m,A
2,20,f,B
3,16,u,X
4,55,m,X
5,60,f,Y
6,20,f,A
7,34,m,B
8,63,m,X
9,23,m,X
10,19,f,A
11,56,f,A
12,25,u,X
13,31,u,A
14,24,f,B
15,27,m,A
16,20,m,B
17,21,u,A
18,43,f,A
19,44,u,A
20,41,m,B
;
RUN;
proc format;
value agegrp 1-20 = '1-20'
21-30 = '21-30'
31-40 = '31-40'
41-50 = '41-50'
51-60 = '51-60'
61-70 = '61-70';
value $status 'A' = 'Status A'
'B' = 'Status B'
'X' = 'Status X'
'Y' = 'Status Y';
run;
现在,我正在使用PROC TABULATE创建一个交叉表:
proc tabulate;
class age sex residenceStatus;
table sex="", (ALL residenceStatus="") * age="" /misstext="0";
format age agegrp. residenceStatus $status.;
run;
但是如何更改交叉表中特定变量的顺序?
例如,在上面的示例中,列为:
ALL Status A Status B Status X Status Y
1-20 21-30 ... 1-20 21-30 ... 1-20 21-30 ... 1-20 21-30 ... 1-20 21-30 ...
f
m
u
如果我想要ALL Status X Status B Status A Status Y
怎么办?感谢您的帮助!
答案 0 :(得分:2)
选项PRELOADFMT
用于强制对变量CLASS
的值进行特定的排序。
更改自定义格式$status
的定义以指定(NOTSORTED)
选项,并按所需的输出顺序列出映射。 NOTSORTED告诉过程不要按起始值排序-这会影响格式的内部以及它与其他过程的交互方式。
然后在TABULATE
中,为需要定义的特定顺序的CLASS
变量,指定选项ORDER=DATA PRELOADFMT
。 注意:默认顺序为格式化值升序。
proc format;
value $status (notsorted)
'X' = 'Status X'
'B' = 'Status B'
'A' = 'Status A'
'Y' = 'Status Y'
;
run;
proc tabulate;
class age sex; /* <---- default ordering */
class residenceStatus / preloadfmt order=data; /* <---- custom ordering */
table sex="", (ALL residenceStatus="") * age="" /misstext="0";
format
age agegrp.
residenceStatus $status. /* <---- defined with (NOTSORTED) */
;
run;