2016-11-06 250 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; 
相關問題