2013-08-19 230 views
1

中实现这一选择说我有一个SAS表tbl它有一列col。该列col保持不同的值,即{"a","s","d","f",...},但其中一个比另一个更多(比如"d")。我怎样才能做一个选择只有这个值我怎样才能在SAS

它会像

data tbl; 
    set tbl; 
    where col eq "the most present element of col in this case d"; 
run; 

回答

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是数值变量)。

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; 
+0

您可以通过将ORDER = FREQ添加到proc freq语句来保存排序。 – mvherweg

+0

啊,这就是选项。在太急于查找它,谢谢! – Joe

+0

Thnaks但我宁愿在宏变量中得到“d”我该怎么做? – statquant