Re: [程式] 不好意思,想請問有關sas分群加總的問쌠…

看板Statistics作者 (keep)時間14年前 (2011/05/06 21:35), 編輯推噓0(001)
留言1則, 1人參與, 最新討論串4/5 (看更多)
※ 引述《joyce618 (joy)》之銘言: : ------------------------------------------------------------------------ : [軟體程式類別]:SAS : [程式問題]:資料處理 : [軟體熟悉度]: : 低(1~3個月) : [問題敘述]: : 不好意思,目前學了SAS已經有小小一段日子了 : 但是對於資料處理還並不是很熟悉 : 想麻煩各位大人指點一下小妹 : 原始資料為以下形式: : code name year month date return(報酬率) : 1000 甲公司 2010 3 1 0.11 : 1000 甲公司 2010 3 2 0.12 : 1000 甲公司 2010 3 3 0.14 : .... : 1001 乙公司 2010 3 1 0.45 : 1001 乙公司 2010 3 2 0.34 : ... : 1000 甲公司 2011 1 1 0.33 : ... : 1000 甲公司 2011 2 1 0.54 : ... : 原始資料內容有每家公司每年每月每日的報酬率(分別為2010/3/1~2011/2/28) : 我想把資料整理成每家公司各季的報酬率總和 : 比如說2010年第二季各家公司的報酬率總和應該分別為4、5、6月份的加總 : 但是我預期出來的格式是 : code name year month totalAR : 1000 甲公司 2010 6 0.xx : 1000 甲公司 2010 9 0.xx : 1000 甲公司 2010 12 0.xx : 1000 甲公司 2011 2 0.xx : ... : 其中month=6表示的是2010年第二季的總報酬總和(其他各季依此類推) : 小妹爬文之後只有找到分組分年的垂直加總程式 : 因此我目前只有把資料整理到每家公司每月總和 : 接下來就完全不知道要怎麼下手了..... : [程式範例]: : 雖然還沒學過SQL語法 : 但是爬文後,我先將資料整理成每家公司每月的總報酬 : 小妹寫得的程式如下: : PROC SQL; : CREATE table new as : SELECT sum(AR) as AR,code,name,yr,month from olddata : GROUP BY code,name,yr,month; : QUIT; : 接下來就不知道要怎麼下手了 : 麻煩各位大人了 : 真的不好意思^^ : ----------------------------------------------------------------------------- SAS語法如下,較SQL複雜,僅供參考 data temp; set XXX;/*資料名稱*/ if input(month,2.)<=3 then season="1";/*input把文字格式轉成數字格式,月換算成季*/ else if input(month,2.)<=6 then season="2"; else if input(month,2.)<=9 then season="3"; else season="4"; orderID=name||year||month||season;/*將公司名稱 年月 季 合成一個變數,for排序用*/ orderID2=name||year||season;/*將公司名稱年季合併成一變數,for累加報酬率用*/ run; proc sort;by orderID;run;/*將資料依name yr month season依序排列*/ data temp2; set temp; if first.orderID2 then total_AR=0;/*新增一變數total_AR,由0開始*/ total_AR+return; /*指定報酬率依orderID2累加,意即對同一公司名稱 同一年 同一季做累加*/ by orderID2; if last.orderID2;/*若資料中有數筆 同公司 同年 同季 資料,則保留最末筆*/ by orderID; run; temp2長相 : code name year month total_AR : 1000 甲公司 2010 6 0.xx : 1000 甲公司 2010 9 0.xx : 1000 甲公司 2010 12 0.xx : 1000 甲公司 2011 2 0.xx -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 1.161.195.158

05/06 21:38, , 1F
修正倒數第二行 by orderID→by orderID2
05/06 21:38, 1F
※ 編輯: keepingJBJ 來自: 1.161.195.158 (05/06 21:45) ※ 編輯: keepingJBJ 來自: 1.161.195.158 (05/06 21:46)
文章代碼(AID): #1Dm_aZ45 (Statistics)
討論串 (同標題文章)
文章代碼(AID): #1Dm_aZ45 (Statistics)