您的当前位置:首页正文

SAS代码汇总

2020-04-17 来源:好走旅游网


建立逻辑库

libname xlb \"D:\\my document\\PKU\\econometrics\\hw1\";

从excel中导入数据

PROC IMPORT OUT=hw1.one DATAFILE= \"数据存放的地址\" DBMS=EXCEL REPLACE;

SHEET=“FS_Combas”; /* 指明哪个sheet*/ GETNAMES=YES; /*是否用excel表中的变量名*/ MIXED=YES; /* 用一列中是否可以有不同结构的数据*/

USEDATE=YES; /*是否直接把excel中日期转出SAS的日期变量*/

RUN;

黏贴表格

proc append data=hw2.dalyr7 base=hw2.dalyr;

run;

合并匹配/*先排序再合并,支持一对多,但不支持多对多*/

proc sort data=hw2.dalyr;

by trddt;

run;

proc sort data=hw2.index nodupkey;

by trddt;

run;

data hw2.four;

merge hw2.dalyr hw2.index;

by trddt;

run;

编辑数据

data one;

set wang.exer2_capm;

beta2=beta*beta;

run;

data hw2.four;

set hw2.four;

format dretwd1 retindex1 best32.;

dretwd1=input(dretwd,best32.);

retindex1=input(retindex,best32.);

keep stkcd trddt dretwd1 retindex1;

rename dretwd1=dretwd;

rename retindex1=retindex;

run;

显示N、均值、标准差、最大最小值/*先看看数据特征是一个好习惯 */

proc means data=one;

var eret beta beta2;

run;

/* 多元回归 */

proc reg data=one;

model eret=beta beta2;

run;

定义宏变量

%let nsimu=2;

/* 定义宏变量nsimu,它将作为常量2,可以用在整个程序中 .这样做的好处是当需要改变取值时,只要改一处,不用在程序中到处改*/

%let n=200; %let alpha=0.5; %let beta=0.02; %let sigma=0.02;

缩尾处理

libname hw5 \"D:\\my document\\PKU\\econometrics\\hw5\";

data hw5.one;

set hw2.all;

rename beta1=sysrisk;

rename error=idiorisk;

rename ret_yr=ret;

run;

*%let datafile=hw5.one;

*%let var=ret;

%macro winsor1(datafile, var);

proc univariate data=&datafile. noprint;

var &var.;

output out=extreme pctlpre=p pctlpts=1 99; proc sql;

select p1 into: myp1 from extreme;

run;

select p99 into: myp99 from extreme; quit;

data &datafile.;

set &datafile.;

if &var.>%sysevalf(&myp99) then &var.=%sysevalf(&myp99);

if (&var.>-1000000 and &var.<%sysevalf(&myp1)) then &var.=%sysevalf(&myp1);

run;

%mend;

%winsor1(hw5.one,ret) /* 引用宏函数, 没有分号结束*/

%winsor1(hw5.one,lev)

%winsor1(hw5.one,sysrisk)

%winsor1(hw5.one,idiorisk)

proc reg data=hw5.one;

model ret=sysrisk idiorisk lev;

run;

BP检验

proc reg data=hw5.one noprint;

model ret=sysrisk idiorisk lev;

output out=out1 residual=res predicted=yhat;

run;

data out1;

set out1;

varr=(res*res);

run;

proc reg data=out1;

model varr=sysrisk idiorisk lev;

run;

因篇幅问题不能全部显示,请点此查看更多更全内容