《SAS金融应用程序举例.doc》由会员分享,可在线阅读,更多相关《SAS金融应用程序举例.doc(172页珍藏版)》请在三一办公上搜索。
1、2.2.1 创建单期收益计算环境data a;set stoindif.a1a0001;year=year(date);qtr=qtr(date);month=month(date);proc sort data=a;by year qtr month;run;data b;set a;last_y=last.year; /*标出某年的最后一个交易日 */last_q=last.qtr; /*标出某季的最后一个交易日 */last_m=last.month; /*标出某月的最后一个交易日 */by year qtr month;run;2.2.2 年收益计算data r_year(keep=d
2、ate r_pct r_log label=年收益);set b;if last_y=1; /* 取各年最后一个交易日的数据 */r_pct=dif(clpr)/lag(clpr_r); /* 计算百分比收益 */r_log=log(clpr_r)-log(lag(clpr_r); /* 计算对数收益 */*函数log(x)是以e为底的自然对数,其它对数函数还有log2(x),log10(x)*/run;2.2.3 季收益计算data r_qtr (keep=date r_pct r_log label=季收益);set b;if last_q=1; /* 取各季最后一个交易日的数据 */r_
3、pct=dif(clpr_r)/lag(clpr_r);r_log=log(clpr_r)-log(lag(clpr_r);run;2.2.4 月收益计算data r_month (keep=date r_pct r_log label=月收益);set b;if last_m=1; /* 取各月最后一个交易日的数据 */r_pct=dif(clpr_r)/lag(clpr_r);r_log=log(clpr_r)-log(lag(clpr_r);run;2.2.5 周收益计算程序一:data a;set stoindif.a1a0001;wd=weekday(date);dif=dif(wd
4、);dif2=dif(date);if (dif=7 then index=1;else index=0;data a(keep=date clpr_r index);set a;date=lag(date);clpr_r=lag(clpr_r);if index=1;data r_week(keep=date r_pct r_log);set a;r_pct=dif(clpr_r)/lag(clpr_r);r_log=log(clpr_r)-log(lag(clpr_r);if r_log=. then delete;run;程序二:data b;set stoindif.a1a0001;
5、wk=int(date-3)/7+2); /* wk为周的标号,设定1960年1月1日为第一周。由于1960年1月1日为周五,所以第一周共有三天。注意该周(1960年1月1日到3日)对应日期按SAS的标准分别为0, 1和2(于是(date-3)/7都等于-1)。由此可以理解为什么这样设定表达式*/proc sort;by date;run;data b;set b;last_wk=last.wk;by wk;run;data b(keep=date r_pct1 r_log1);set b;if last_wk=1;r_pct1=dif(clpr)/lag(clpr);r_log1=log(c
6、lpr)-log(lag(clpr);run;data c;/*检测程序一和程序二的一致性*/merge r_week b;by date;if r_pct=r_pct1 then aa=1;else aa=0;/*最后一个不一样*/run; 2.2.6 日收益计算data r_day (keep=date r_pct r_log label=日收益);set stoindif.a1a0001;r_pct=dif(clpr_r)/lag(clpr_r);r_log=log(clpr_r)-log(lag(clpr_r);run;2.2.7 日复权收益直接计算data return(keep=d
7、ate);set stoindif.a1a0001;where 1997=year(date)=2000;data a(keep=date r_1);set stoindif.a600601;where 1997=year(date)=2000; r_1=log(clpr)-log(lag(clpr);clpr_1=clpr*(1+divrat+rigrat+reisvol/lag(shrout)-rigpr*rigrat reispr* reisvol/lag(shrout)+divamt; /* clpr_1为复权价格 */if clpr_1=. then clpr_1=clpr;r_2=
8、log(clpr_1)-log(lag(clpr_1);if exdt=. then r_2=0;if exdt=. then r_1=0;r_1=r_1+r_2;if r_1=. then r_1=0;else r_1=r_1;data return(rename=(r_1=r600601);merge return a;by date;data return;set return;if r600601=. then r600601=0;else r600601= r600601;run;2.2.8 绘制收益图proc gplot data=r_day ;plot r_pct*date /
9、vref=0;plot r_log*date / vref=0; run;2.2.9 多期平均收益率计算/*建立满足条件的数据集*/data a1;set r_year;where 1995 = year(date) = 2000;proc print;run;/*对数据集转置*/proc transpose data=a1 out=a2;var r_pct;proc print;run;/*计算年平均收益*/data a3(keep=am gm);set a2;c1=col1+1; c2=col2+1;c3=col3+1; c4=col4+1; c5=col5+1;c6=col6+1; gm
10、=(c1*c2*c3*c4*c5*c6)*(1/6)-1;am=mean(of col1-col6);proc print;run;/*变量太多时,用数组的方法处理简单些*/data a4(keep=am gm);set a2;t=6;array col(6) col1-col6;array c(6) c1-c6;do i=1 to 6;c(i)=col(i)+1;end;gm=( c1*c2*c3*c4*c5*c6)*(1/t)-1; am=mean(of col1-col6); proc print;run;data a5 ;set b;if last_y=1 and 1995= year
11、(date) = 2000;run;data a6;retain begin end;set a5 end=lastobs;if _n_=1 then begin=clpr_r; /* 将数据集第一个观测值的复权价赋给变量begin */if lastobs then do;end=clpr_r; /* 将数据集最后一个观测值的复权价赋给变量end */output;end;data a6(keep=gm);set a6;gm=(end/begin)*(1/6)-1;proc print;run; /*该方法主要优点是精确*/2.3.1 由最新股票标识数据集创建宏文本创建全部A股股票宏文本:全
12、部A股票.TXTdata a(drop=hexcd);set compufin.header;if substr(hstocd,3,1)=6 or substr(hstocd,1,5)=21000; /* substr()为取子串函数 */run;data a;length hexcd $6.;set a;hexcd=substr(hstocd,3,6);label hexcd=最新交易所代码;run;data a;set a;a=%a(;b=,;c=); ;file d:基于sas系统的金融计算光盘compufin宏文本全部A股.txt ;put a $ hexcd $ b $ hcomna
13、m $ c $ ;run;创建宏文本:沪市全部A股.txtdata ;set a;if substr(hexcd,1,1)=6;a=%a(;b=,;c=); ;file d:基于sas系统的金融计算光盘compufin宏文本沪市全部A股.txt ;put a $ hexcd $ b $ hcomnam $ c $ ;run;创建宏文本:深市全部A股.txt;data ;set a;if substr(hexcd,1,1)=0;a=%a(;b=,;c=); ;file d:基于sas系统的金融计算光盘compufin宏文本深市全部A股.txt ;put a $ hexcd $ b $ hcomn
14、am $ c $ ;run;2.3.2 由个股数据集目录文件创建宏文本形成文件名数据集:data a;input files $20.;cards;/*上面选择文件名一列构成数据行 */;run;全部A股代码.TXT的形成:data;set a;hexcd=substr(files,2,6);a=%A(;b=,;c=); ;file D:基于SAS系统的金融计算光盘Compufin宏文本全部A股代码.txt;put a $ hexcd $ c $ ;run;2.3.3 多股票收益计算程序沪市A股日对数收益计算程序:options nodate nonotes nosource; /*不输出时间
15、、注释和源程序到日志LOG上 */data stoindiv.lg_shanghai(keep=date);set stoindiv.a1a0001;where 1995=year(date)=2000;%macro a(x,y);data a(keep=date r_1);set stoindiv.a&x;where 1995=year(date)=2000; r_1=log(clpr)-log(lag(clpr);clpr_1=clpr*(1+divrat+Rigrat+reisvol/lag(Shrout)-rigpr*Rigrat reispr* reisvol/lag(Shrout)
16、+Divamt;if clpr_1=. then clpr_1=clpr;r_2=log(clpr_1)-log(lag(clpr_1);if exdt=. then r_2=0;if exdt=. Then r_1=0;r_1=r_1+r_2;if r_1=. Then r_1=0;else r_1=r_1;data stoindiv.lg_shanghai(rename=(r_1=r&x);merge stoindiv.lg_shanghai a;by date;data stoindiv.lg_shanghai;set stoindiv.lg_shanghai;if r&x=. then
17、 r&x=0;else r&x= r&x;%mend a;%include D:基于SAS系统的金融计算光盘Compufin宏文本沪市全部A股.txt;run;沪市A股日百分比收益计算程序:options nodate nonotes nosource;data stoindiv.r_shanghai(keep=date);set stoindiv.a1a0001;where 1995=year(date)=2000;%macro a(x,y);data a(keep=date r_1);set stoindiv.a&x;where 1995=year(date)=2000; r_1=(clp
18、r-lag(clpr)/lag(clpr);clpr_1=clpr*(1+divrat+rigrat+reisvol/lag(shrout)-rigpr*rigrat reispr* reisvol/lag(shrout)+divamt;if clpr_1=. then clpr_1=clpr;r_2=(clpr_1-lag(clpr_1)/lag(clpr_1);if exdt=. then r_2=0;if exdt=. Then r_1=0;r_1=r_1+r_2;if r_1=. Then r_1=0;else r_1=r_1;data stoindiv.r_shanghai(rena
19、me=(r_1=r&x);merge stoindiv.r_shanghai a;by date;data stoindiv.r_shanghai;set stoindiv.r_shanghai;if r&x=. then r&x=0;else r&x= r&x;%mend a;%include D:基于SAS系统的金融计算光盘Compufin宏文本沪市全部A股.txt;run;深市A股日对数收益计算程序:options nodate nonotes nosource;data stoindiv.lg_shenzhen(keep=date);set stoindiv.szcz;where 19
20、95=year(date)=2000;%macro a(x,y);data a(keep=date r_1);set stoindiv.a&x;where 1995=year(date)=2000; r_1=log(clpr)-log(lag(clpr);clpr_1=clpr*(1+divrat+rigrat+reisvol/lag(shrout)-rigpr*rigrat reispr* reisvol/lag(shrout)+divamt;if clpr_1=. then clpr_1=clpr;r_2=log(clpr_1)-log(lag(clpr_1);if exdt=. then
21、 r_2=0;if exdt=. then r_1=0;r_1=r_1+r_2;if r_1=. then r_1=0;else r_1=r_1;data stoindiv.lg_shenzhen(rename=(r_1=r&x);merge stoindiv.lg_shenzhen a;by date;data stoindiv.lg_shenzhen;set stoindiv.lg_shenzhen;if r&x=. then r&x=0;else r&x= r&x;%mend a;%include D:基于SAS系统的金融计算光盘Compufin宏文本深市全部A股.txt;run;深市A
22、股日百分比收益计算程序:data stoindiv.r_shenzhen(keep=date);set stoindiv.szcz;where 1995=year(date)=2000;%macro a(x,y);data a(keep=date r_1);set stoindiv.a&x;where 1995=year(date)=2000; r_1=(clpr-lag(clpr)/lag(clpr);clpr_1=clpr*(1+divrat+rigrat+reisvol/lag(shrout)-rigpr*rigrat reispr* reisvol/lag(shrout)+divamt
23、;if clpr_1=. then clpr_1=clpr;r_2=(clpr_1-lag(clpr_1)/lag(clpr_1);if exdt=. then r_2=0;if exdt=. then r_1=0;r_1=r_1+r_2;if r_1=. Then r_1=0;else r_1=r_1;data stoindiv.r_shenzhen(rename=(r_1=r&x);merge stoindiv.r_shenzhen a;by date;data stoindiv.r_shenzhen;set stoindiv.r_shenzhen;if r&x=. then r&x=0;
24、else r&x= r&x;%mend a;%include D:基于SAS系统的金融计算光盘Compufin宏文本深市全部A股.txt;run;2.3.4 收益SAS数据集转换为EXCEL数据表proc transpose data=stoindiv.lg_shanghai out=stoindiv.lg_shanghai_tr;run;data stoindiv.lg_shanghai_tr_1;set stoindiv.lg_shanghai_tr;if _n_200;run;proc transpose data= stoindiv.lg_shanghai_tr_1 out= stoi
25、ndiv.lg_shanghai_1;data stoindiv.lg_shanghai_1;set stoindiv.lg_shanghai_1;format date yymmdd10.;run;data stoindiv.lg_shanghai_tr_2;set stoindiv.lg_shanghai_tr;if 201=_n_400;run;proc transpose data= stoindiv.lg_shanghai_tr_2 out= stoindiv.lg_shanghai_2;data stoindiv.lg_shanghai_2;set stoindiv.lg_shan
26、ghai_2;format date yymmdd10.;run;data stoindiv.lg_shanghai_tr_3;set stoindiv.lg_shanghai_tr;if 401=_n_;run;proc transpose data= stoindiv.lg_shanghai_tr_3 out= stoindiv.lg_shanghai_3;data stoindiv.lg_shanghai_3;set stoindiv.lg_shanghai_3;format date yymmdd10.;run;proc export data=stoindiv.lg_shanghai
27、_1 outfile=d:基于sas系统的金融计算光盘compufinlg_shanghai_1.xlsdbms=excel2000 replace;run;proc export data=stoindiv.lg_shanghai_2 outfile=d:基于sas系统的金融计算光盘compufinlg_shanghai_2.xls dbms=excel2000 replace;run;proc export data=stoindiv.lg_shanghai_3outfile=d:基于sas系统的金融计算光盘compufinlg_shanghai_3.xlsdbms=excel2000 r
28、eplace;run;同样,可以转换其他收益SAS数据集为相应的EXCEL表。2.4.1 由股本变动历史数据集创建宏文本挑选出1998年前上市的全部A股股票程序:%macro A(x);data a;length hexcd $6.;set compufin.shares;if year(date)&x;if substr(hstocd,3,1)=6 or substr(hstocd,1,5)=21000;hexcd=substr(hstocd,3,6);label hexcd=最新交易所代码;proc sort data=a;by hexcd ;data a;set a;by hexcd;i
29、f first.hexcd=1; /* 以hexcd分组,取每组的第一个观测值 */run;data compufin.y%eval(&x)_list ;set a;a=%a(;c=);file d:基于sas系统的金融计算光盘compufin宏文本AlistedBefore%str(&x).txt;put a $ hexcd $ c $;%mend A;%A(1998);run;2.4.2 随机抽股票%macro a(x);proc sql; /* sql过程创建视图 */ create view _tmp_ as select *, ranuni(5) as _ran_ from comp
30、ufin.y%eval(&x)_list order by calculated _ran_;quit;data random; set _tmp_(obs = 20); drop _ran_;a=%a(;c=);file d:基于sas系统的金融计算光盘compufin宏文本random%str(&x).txt;put a $ hexcd $ c $;%mend a;%a(1998);run;2.4.3 单个股票收益计算对上面随机抽取20支股票,计算19972000年的日百分比收益:data compufin.r_port20(keep=date);set stoindiv.a1a0001;
31、where 1997=year(date)=2000;%macro a(x);data a(keep=date r_1);set stoindiv.a&x;where 1997=year(date)=2000; r_1=(clpr-lag(clpr)/lag(clpr);clpr_1=clpr*(1+divrat+rigrat+reisvol/lag(shrout)-rigpr*rigrat reispr* reisvol/lag(shrout)+divamt;if clpr_1=. then clpr_1=clpr;r_2=(clpr_1-lag(clpr_1)/lag(clpr_1);if
32、 exdt=. then r_2=0;if exdt=. then r_1=0;r_1=r_1+r_2;if r_1=. Then r_1=0;else r_1=r_1;data compufin.r_port20 (rename=(r_1=r&x);merge compufin.r_port20 a;by date;data compufin.r_port20;set compufin.r_port20;if r&x=. then r&x=0;else r&x= r&x;%mend a;%include d:基于sas系统的金融计算光盘compufin宏文本Random1998.txt;ru
33、n;2.4.4 股票组合的随机赋权重方法一:data rv;id=1; retain _seed_ 3; do _i_ = 1 to 20; w= 0 + (1 - 0) * ranuni(_seed_); output; end; drop _seed_ _i_;data a(keep=sumw id);set rv end=obs_last;sumw+w;If obs_last=1;Id=1;data b(keep=w);merge rv a;by id ;w=w/sumw;run;方法二:proc iml;rv=uniform(repeat(0,20,1); /* 0为种子值,20为随机
34、数个数,1为随机数列数 */sum=rv+,;b=rv/sum;sumb=b+,;create b from b; /* 由矩阵b创建SAS数据集b */append from b;print sumb; /* 看权数相加不否为1 */quit;2.4.5 组合收益计算由COMPUFIN.R_PORT20得到数据集A,便于以后形成矩阵:data a(drop=date);set compufin.r_port20;if _n_=1 then delete;proc iml;use a; read all var _num_ into xx; /* 由数据集a的所有数值变量的观测创建矩阵xx *
35、/create xx from xx; /* 由矩阵xx创建数据集xx */append from xx;close xx;use b;read all into w;create w from w;append from w;close w;aaa=xx*w; /*矩阵xx乘向量w */create aaa from aaa; /* 由矩阵aaa创建数据集aaa */append from aaa;close aaa;show names; quit;run;最后得到随机抽取20支股票投资组合的收益数据集R_PORT20:data a;set compufin.r_port20;if _n_
36、=1 then delete; data r_port20;merge a aaa(rename=(col1=r_port20) ;run;3.1.1 内生收益率计算试错法计算内生收益率通用程序:data a;delete;Run;%macro a(r, n, d, par);data a1;p1=0;%do i=1 %to &n ;p1=p1+&d/(1+&r)* &i;output;%end;data a1;set a1 end=lasobs;if lasobs;p2=&par/(1+&r)*&n;p=p1+p2;r=200*&r;r1=100*&r;n=&n;data a;set a a
37、1;%mend a;/* %a(r, n, d, par)内的具体参数值 */proc print data=a ;run;例3.2 利用通用程序,a(r, n, d, par)取值如下:%a(0.00625, 360, 349.6, 0);%a(0.006667, 360, 349.6, 0);%a(0.007083, 360, 349.6, 0);%a(0.0075, 360, 349.6, 0);本例计算程序:%macro a(r);data;p=2000/(1+&r)+2000/(1+&r)*2+2500/(1+&r)*3+4000/(1+&r)*4;r=100*&r;put r= p
38、=;%mend a;%a(0.1);%a(0.14);%a(0.12);run; 3.1.2 有效年利率计算例3.4计算程序:%macro a(r,m);data;i=(1+&r)*&m-1;r=100*&r;put r= i=;%mend a;%a(0.02,4);run;3.1.3 到期收益率计算例3.5 利用通用程序,a(r, n, d, par)取值如下:%a(0.0325, 36, 30, 1000);%a(0.035, 36, 30, 1000);%a(0.0375, 36, 30, 1000);%a(0.04, 36, 30, 1000);%a(0.0425, 36, 30, 1
39、000);%a(0.045, 36, 30, 1000);%a(0.0475, 36, 30, 1000);3.1.5 第一个赎回日收益率计算例3.6 利用通用程序,a(r, n, d, par)取值如下:%a(0.056, 10, 30, 1030);%a(0.0585, 10, 30, 1030);%a(0.061, 10, 30, 1030);%a(0.0635, 10, 30, 1030);%a(0.066, 10, 30, 1030);%a(0.0685, 10, 30, 1030);%a(0.071, 10, 30, 1030);%a(0.0735, 10, 30, 1030);%
40、a(0.076, 10, 30, 1030);3.1.6 清算日处于两个到期日之间的到期收益率计算计算程序:data ;date0=01mar1997d;date1=17jul1997d;date2=01sep1997d;days02=datdif(date0, date2, 30/360); /*美国公司债适合30/360标准 */days12=datdif(date1, date2, 30/360); n=2*(2003-1997)-1;w=days12/days02;put days02/days12/n/w;call symput(n, n); /*创建一个值来自data步的宏变量n*
41、/call symput(w, w); /*创建一个值来自data步的宏变量w*/data a;delete;%macro a(r);data a1;p1=0;do i=1 to &n ;p1=p1+5/(1+&r)*(i-1+&w);output;end;data a1;set a1 end=lasobs;if lasobs;p2=105/(1+&r)*(&n-1+&w);p=p1+p2;r=200*&r;r1=100*&r;data a(drop=i);set a a1;w=&w;n=&n;%mend a;%a(0.0363);%a(0.03735);proc print data=a;r
42、un;例3.8计算程序:data ;date0=01mar1997d;date1=17jul1997d;date2=01sep1997d;days02=datdif(date0, date2, act/act); /*美国政府债适合的标准 */days12=datdif(date1, date2, act/act); n=2*(2003-1997)-1;w=days12/days02;put days02/days12/n/w;call symput(n, n); /*创建一个值来自data步的宏变量n*/call symput(w, w); /*创建一个值来自data步的宏变量w*/data
43、 a;delete;%macro a(r);data a1;p1=0;do i=1 to &n ;p1=p1+5/(1+&r)*(i-1+&w);output;end;data a1;set a1 end=lasobs;if lasobs;p2=105/(1+&r)*(&n-1+&w);p=p1+p2;r=200*&r;r1=100*&r;data a(drop=i);set a a1;w=&w;n=&n;%mend a;%a(0.03627);%a(0.0363);proc print data=a;run;3.1.7 投资组合内生收益率计算例3.9 %macro a(y);data a;ap1=0;do n=1 to 9 ;ap1=ap1+350000/(1+&y)*n;output;end;data a;set a end=lasobs;if lasobs;ap2=10350000/(1+&y)*10;ap=ap1+ap2;data b;bp1=0;do n=1 to 13 ;bp1=bp1+1050000/(1+&y)*n;output;end;data b;set b end=lasobs;if lasobs;bp2=21050000/(