2016-11-06 271 views
0

我用下面的代码来计算列中的最大值和最小值之间的差异,但它不喜欢聪明的方式。那么有谁能给我一些建议?SAS-如何计算最大值和最小值之间的差异?

p.s.我需要将区别作为新变量放回数据集,因为我想根据此差异删除数据集。

proc univariate noprint date=test; 
var time_l_; 
output out=result max=max min=min; 
run; 

data test; 
set result test; 
run; 

data test; 
set test; 
gap=max-min; 
run; 

回答

0

实际上,我相当接近我认为的好结果。这不是最快的方法,但它可能是最好的,当你不需要惊人的性能时,因为它比快速方法复杂得多。

创建最大/最小数据集,然后使用if _n_ = 1 then set result;,这会将其带入一次。这些变量会自动保留,因为它们是在SET语句中引入的。然后在相同的数据步骤中计算差距。

proc univariate noprint data=sashelp.class; 
    var age; 
    output out=result max=max min=min; 
run; 

data test; 
    if _n_=1 then set result; 
    set sashelp.class; 
    gap = max-min; 
run; 
0

SQL解决方案非常简单,但在日志中留下了一条关于重新合并的消息。

Proc SQL; 
Create table want as 
Select *, max(age) as max_age, min(age) as min_age, calculated max_age - calculated min_age as age_diff 
From have; 
Quit; 
0

使用range功能更简单的SQL溶液:

proc sql; 
create table want as 
    select *,range(age) as age_range 
    from sashelp.class; 
quit; 
相关问题