一、 使用DATA步创建SAS数据集
1、 创建临时数据集
程序编辑窗口输入如下命令,即可产生一个名为example1_1的临时数据集
data example1_1;
input time monyy7.price;
format time monyy5.;
cards;
jan2005 101
feb2005 82
mar2005 66
apr2005 35
may2005 31
jun2005 7
;
run;
点击提交,运行结束,临时数据及建立,可以随时调用这个数据集。
2、 创建永久数据集
Datasasuser.example1_1;
Libname命令也可以创建永久数据库:
Libnamedatafile ‘d:\sas\myfile’;
Datadatafile.example1_1;
以后这个数据集将一直以datafile.example1_1形式被引用
3、 查看数据集
Procprint data=数据库名.数据集名;
Procprint data=example1_1;
Run;
运行程序,在结果输出窗口得出结果:
二、 时间序列数据集的处理
1、 间隔函数的使用
间隔函数INTNX可以根据需要自动产生等时间间隔的时间数据:
data example1_2;
input price ;
time=intnx( 'month', '01jan2005'd, _n_-1);
format time monyy.;
cards;
3.41
3.45
3.42
3.53
3.45
;
proc print data=example1_2;
run;
结果
注:intnx函数的三个参数,第一个参数是指定等时间间隔,可以是day week monthquarter year等。第二个参数是指定参照时间。第三个参数是_n_k,用来调整开始观测指针,k为正整数时指针由参照时间向未来拨k期;k取负整数时反之。
2、 序列变换
data example1_3;
input price ;
logprice=log(price);
time=intnx( 'month', '01jan2005'd, _n_-1);
format time monyy.;
cards;
3.41
3.45
3.42
3.53
3.45
;
proc print data=example1_3;
run;
结果
3、 子集
data example1_4;
set example1_3;
keep timelogprice;
where time>='01mar2005'd;
proc print data=example1_4;
run;
结果
4、 缺失值插值
假设上例3月1日price值观察值缺失,运行如下程序插补
data example1_5;
input price;
time=intnx( 'month', '01jan2005'd, _n_-1);
format time date.;
cards;
3.41
3.45
.
3.53
3.45
;
proc expanddata=example1_5 out=example1_6;
id time;
proc print data=example1_5;
proc print data=example1_6;
run;
结果