2016-02-17 51 views
-2

附加是一個數據集示例,其中ID#列的某些值相同,但Dollar Amount列對於相同的ID#具有不同的值。我怎樣才能在ID#相同的行上添加美元金額,並且實質上爲該ID#創建一個彙總行?該列表比這更長,因此指定每個ID#不是一個選項。SAS跨行添加

enter image description here

回答

1

有SQL選項爲好,但這是一個會怎麼做一個運行總計/總和在數據的一步。

data have; 
input id $1. dollar_amount 8.; 
format dollar_amount dollar25.; 
cards; 
1 24 
2 53 
2 67 
3 35 
4 86 
5 245 
6 353 
6 56 
7 21 
; 
run; 

proc sort data=have noequals; /*Only run if not already sorted*/ 
by id; 
run; 

data want(drop=dollar_amount); 
set have; 
by id; 
if first.id then dollar_total = .; 
format dollar_total dollar25.; 
dollar_total + dollar_amount; 
if last.id then output; 
run; 
0

如果你不想使用PROC SQL,並且希望將輸入數據中的實際摘要行(我認爲這是你問的是什麼),這裏有一個使用標誌來識別簡單的數據步驟和行:

data have; 
input id $1. dollar_amount 8.; 
format dollar_amount dollar25.; 
cards; 
1 24 
2 53 
2 67 
3 35 
4 86 
5 245 
6 353 
6 56 
7 21 
; 
run; 

proc means data = have noprint; 
    by id; 
    var dollar_amount; 
    output out=sum_data (drop=_type_ _freq_) sum=dollar_amount ; 
run; 

data sum_data; set sum_data; 
    sum_row=1; 
run; 

data final; 
    set have sum_data; 
run; 

proc sort data = final; 
    by id sum_row; 
run; 

如果你只是想通過唯一的ID產生資金的輸出數據集,那麼就使用類似:

proc means data = have noprint; 
    by id; 
    var dollar_amount; 
    output out = want (drop=_type_ _freq_) sum=dollar_amount_sum; 
run; 
0

和PROC SQL相當於...

data have; 
input id $1. dollar_amount 8.; 
format dollar_amount dollar25.; 
cards; 
1 24 
2 53 
2 67 
3 35 
4 86 
5 245 
6 353 
6 56 
7 21 
; 
run; 


proc Sql; 
create table want as 
    Select 
ID, 
sum(dollar_amount) as dollar_amount format dollar25. 

    from have 
group by ID 
    ; 
quit; 

enter image description here