作者keepingJBJ (keep)
看板Statistics
标题Re: [程式] 不好意思,想请问有关sas分群加总的问쌠…
时间Fri May 6 21:35:28 2011
※ 引述《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
1F:→ keepingJBJ:修正倒数第二行 by orderID→by orderID2 05/06 21:38
※ 编辑: keepingJBJ 来自: 1.161.195.158 (05/06 21:45)
※ 编辑: keepingJBJ 来自: 1.161.195.158 (05/06 21:46)