Re: [程式] 請問同變數sas加總問題

看板Statistics作者 (焦了六年變脆了)時間16年前 (2009/12/25 14:01), 編輯推噓1(100)
留言1則, 1人參與, 最新討論串2/4 (看更多)
※ 引述《supercookie (cookie)》之銘言: : 想請問 如果遇到像是以下的資料 : id x : 1 1 : 1 1 : 2 3 : 3 5 : 3 1 : 3 1 : 4 2 : 4 2 : 要想利用id加總成為 : id x : 1 2 : 2 3 : 3 7 : 4 4 : 在sas資料檔要如何寫這加總程式,且id只重複一次 : 因為有幾千筆資料而且重複次數不一定 所以不知如何加總 : 請版上的高手幫忙 謝謝^^ 兩種方法,第一種的弱點是需要先sort,所以如果資料量足夠龐大的話,sort會 很耗時,用第二種應該比較有效率。不過幾千筆的話兩種方法應該差不多,挑一 個喜歡的吧:) 1. proc sort data=a out=b; by id; run; data c ; retain new; set b; by id; if first.id then new=0; new=new+x; drop x; rename new=x ; if last.id then output; run; 2. proc sql; create table b as select distinct id, sum(x) as x from a group by id; quit; -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.109.40.59

12/25 14:48, , 1F
謝謝^^
12/25 14:48, 1F
文章代碼(AID): #1BD5LJPm (Statistics)
文章代碼(AID): #1BD5LJPm (Statistics)