【问题标题】:How to filter a dataset according to its quantile如何根据分位数过滤数据集
【发布时间】:2017-10-16 14:59:24
【问题描述】:

在下面的代码中,我怎样才能只保留高于第 95 个分位数的观察值?

data test;
input business_ID $ count;
datalines;
'busi1' 2
'busi1' 10
'busi1' 4
'busi2' 1
'busi3' 2
'busi3' 1
;
run;

proc sort data = test;
    by descending count;
run;

我不知道如何干净地存储四分位数,然后在 if 条件下重新使用它。

谢谢

编辑:我可以用这段代码确定分位数:

proc means data=test noprint; 
var count;
output out=quantile P75= / autoname;
run;

但是我如何在 Test 数据集中关联它,以便我可以选择高于该分位数的每个观察值?

【问题讨论】:

  • 发布您到目前为止所尝试的内容。您可以使用 PROC MEANS、UNIVARIATE 或 RANK 来查找百分位数。

标签: sas quantile


【解决方案1】:

您可以读取宏变量中的分位数值,以在后续ifwhere 条件中使用:

proc means data=test noprint; 
var count;
output out=quantile P75= / autoname;
run;

data _null_;
set quantile;
call symput('quantile',count_p75);
run;

data test;
set test;
where count > &quantile.;
run;

或者您可以使用 SQL 子查询

proc means data=test noprint; 
var count;
output out=quantile P75= / autoname;
run;

proc sql undo_policy=none;
create table test as
select *
from test
where count > (select count_p75 from quantile)
;
quit;

(请注意,您的问题提到了第 95 个分位数,而您的示例代码提到了第 75 个分位数)

【讨论】:

    【解决方案2】:

    User2877959 的解决方案是可靠的。最近我用 Proc Rank 做到了这一点。该解决方案有点“绕开”,但可以节省大量打字时间。

    proc rank data=Input groups=1000 out=rank_out; 
        var var_to_rank; 
        ranks Rank_val; 
    run;
    
    data seventy_five;
        set rank_out;
        if rank_val>750;
    run;
    

    更多排名:http://documentation.sas.com/?docsetId=proc&docsetTarget=p0le3p5ngj1zlbn1mh3tistq9t76.htm&docsetVersion=9.4&locale=en

    【讨论】:

      猜你喜欢
      • 2019-08-14
      • 2020-05-16
      • 2019-06-12
      • 2019-04-30
      • 2019-04-27
      • 2017-12-15
      • 1970-01-01
      • 1970-01-01
      • 2022-07-21
      相关资源
      最近更新 更多