说我有一个SAS表tbl
,其中有col
列。此列col
包含不同的值{"a","s","d","f",...}
,但其中一个比另一个更多(比如"d"
)。如何只选择此值
这就像是
data tbl;
set tbl;
where col eq "the most present element of col in this case d";
run;
答案 0 :(得分:3)
实现这一目标的众多方法之一...
data test;
n+1;
input col $;
datalines;
a
b
c
d
d
d
d
e
f
g
d
d
a
b
d
d
;
run;
proc freq data=test order=freq; *order=freq automatically puts the most frequent on top;
tables col/out=test_count;
run;
data want;
set test;
if _n_ = 1 then set test_count(keep=col rename=col=col_keep);
if col = col_keep;
run;
将它放入一个宏变量(见注释):
data _null_;
set test_count;
call symput("mvar",col); *put it to a macro variable;
stop; *only want the first row;
run;
答案 1 :(得分:1)
我会使用PROC SQL。
这是一个将“d”变为宏变量,然后按照问题中的要求过滤原始数据集的示例。
即使最频繁的观察存在多向联系,这也会有效。
data tbl;
input col: $1.;
datalines;
a
a
b
b
b
b
c
c
c
c
d
d
d
;run;
proc sql noprint;
create table tbl_freq as
select col, count(*) as freq
from tbl
group by col;
select quote(col) into: mode_values separated by ', '
from tbl_freq
where freq = (select max(freq) from tbl_freq);
quit;
%put mode_values = &mode_values.;
data tbl_filtered;
set tbl;
where col in (&mode_values.);
run;
请注意使用QUOTE(),将col的值包装在引号中是必需的(如果col是数字变量,则省略它)。