※ 引述《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;
: 接下來就不知道要怎麼下手了
: 麻煩各位大人了
: 真的不好意思^^
: -----------------------------------------------------------------------------
如果上面的sql可以消化的話,其實在用之前稍微轉個彎就行了
先把原始資料按季分類再跑sql
data temp;
set old;
if month='1' or month='2' then month='3';
else if month='4' or month='5' then month='6';
else if month='7' or month='8' then month='9';
else if month='10' or month='11' then month='12';
run;
proc sql;
create table new as
select distinct *, sum(return) as totalAR
from temp
group by code, name, year, month
order by code, name, year, month;
quit;
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 111.243.0.65