据集就建立了,在本次SAS运行中,我们可以随时调用这个数据集,该数据集在本次SAS系统运行中始终存在,而一旦退出本次操作,该库中的数据集将被全部删除。SAS会自动的把所有缺省数据库名的数据集存在work数据库中。1.2创建永久数据集data sasuser examplel_l;input time monyy7.price;formattime monyy5.;cards;jan200510182feb200566mar2005apr200535may200531jun20057;run;proc print data=examplel_l;run;实验结果:timeprice1011JANO52FEBOS8266MAR05354APROSSMAY053176JUNOS实验分析:该程序的到了一个名为sasuser.examplel1的永久数据集。所谓的永久数据库就是指在该库建立的数据集不会因为我们退出SAS系统而丢失,它会永久的保存在该数据库中,我们以后进入SAS系统还可以从该库中调用该数据集。1.3查看数据集data examplel l;input time monyy7. price;format time monyy5.;cards;jan2005 101feb2005822
2 据集就建立了,在本次 SAS 运行中,我们可以随时调用这个数据集,该数据集在 本次 SAS 系统运行中始终存在,而一旦退出本次操作,该库中的数据集将被全部 删除。SAS 会自动的把所有缺省数据库名的数据集存在 work 数据库中。 1.2 创建永久数据集 data sasuser example1_1; input time monyy7. price; format time monyy5. ; cards; jan2005 101 feb2005 82 mar2005 66 apr2005 35 may2005 31 jun2005 7 ; run; proc print data=example1_1; run; 实验结果: 实验分析:该程序的到了一个名为 sasuser.example1_1 的永久数据集。所谓 的永久数据库就是指在该库建立的数据集不会因为我们退出 SAS 系统而丢失,它 会永久的保存在该数据库中,我们以后进入 SAS 系统还可以从该库中调用该数据 集。 1.3 查看数据集 data example1_1; input time monyy7. price; format time monyy5. ; cards; jan2005 101 feb2005 82
mar200566apr2005351may2005311jun20057"run;proc printdata=examplel_l;run;实验结果:Obstimeprice12345101JAN0588597FEB05MAR05APR05MAY05coJUN05实验分析:使用print程序查看数据集。交建立好一个数据集后,我们可以使用print程序查看这个数据集的结果。命令格式为:procprintdata=数据库名.数据集名;在原程序中添加如下命令可以查看临时数据集examplel1的内容。2时间序列数据集的处理2.1时间间隔函数的使用data examplel_2;input price;time=intnx('month',"01jan2005'd,_n_-1);format time monyy.;cards;3.413.453.423.533.45;procprintdata=examplel2;run;实验结果:3
3 mar2005 66 apr2005 35 may2005 31 jun2005 7 ; run; proc print data=example1_1; run; 实验结果: 实验分析:使用 print 程序查看数据集。建立好一个数据集后,我们可以使用 print 程序查看这个数据集的结果。命令格式为:proc print data=数据库名.数据集 名;在原程序中添加如下命令可以查看临时数据集 example1_1 的内容。 2 时间序列数据集的处理 2.1 时间间隔函数的使用 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; 实验结果:
Obstimeprice1283:4JAN05FEB053.42MAR0543.53APR0553.45MAY05实验分析:time=intnx(‘month,‘01jan2005d,_n_-1);指定用intnx函数给时间变量time赋值。具体操作是以2005年1月1日为起始时间,以月为时间间隔,从起始时刻2005年1月1日开始每读入一个price的数据,就自动产生一个time的时间数据。Intnx函数包括三个参数:第一个参数是指定等时间间隔,本例中指定等时间间隔为月‘month;第二个参数是指定参照时间,本例的参照时间是01jan2005'd;第三个参数是n-k,这个参数主要是调整开始观测指针。2.2序列变换dataexamplel_3;input price;logprice=log(price);time=intnx('month',"01jan2005'd,_n_-1);formattimemonyy.;cards;3.413.453.423.533.45;proc print data=examplel 3;run;实验结果:Obspricelogpricetime29:1JAN051.22671:41.23837FEB058.42MAR051.22964I3.531.26130APR05L3.451.23837MAY05实验分析:在时间序列分析中,我们得到的是观测值序列xt,但是需要分析的可能是这个观察值序列的某个函数变换,例如对数序列Inxt。在建立数据集时,我们可以通过简单的赋值命令实现这个变换。再该程序中,logprice=log(price);是一个简单的赋值语句,将price的对数函数值赋值给一个新的变量logprice,即建立了一个新的对数序列。4
4 实验分析:time=intnx(‘month’,‘01jan2005’d,_n_-1);指定用 intnx 函数 给时间变量 time 赋值。具体操作是以 2005 年 1 月 1 日为起始时间,以月为时间 间隔,从起始时刻 2005 年 1 月 1 日开始每读入一个 price 的数据,就自动产生一 个 time 的时间数据。Intnx 函数包括三个参数:第一个参数是指定等时间间隔, 本例中指定等时间间隔为月‘month’;第二个参数是指定参照时间,本例的参照 时间是‘01jan2005’d;第三个参数是_n_-k,这个参数主要是调整开始观测指针。 2.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; 实验结果: 实验分析:在时间序列分析中,我们得到的是观测值序列 xt,但是需要分析 的可能是这个观察值序列的某个函数变换,例如对数序列 lnxt。在建立数据集时, 我们可以通过简单的赋值命令实现这个变换。再该程序中,logprice=log(price); 是一个简单的赋值语句,将 price 的对数函数值赋值给一个新的变量 logprice,即 建立了一个新的对数序列
2.3子集data examplel_4;set examplel_3;keep time logprice;wheretime>='0lmar2005'd;procprintdata=examplel_4;run;实验结果:Obstimelogprice-201.22964MAR051.26130APR05MAY051.23837实验分析:当我们只需要分析一个时间序列中的部分序列值时,这时可以在data步中建立一个子集。例如在本例中,我们只需要分析数据集examplel_3中2005年3-5月的对数价格序列,建立相应的子集examplel_4。“dataexample1_4”告诉系统要建立一个名为examplel_4的临时数据集;“setexamplel_3”是说数据集examplel_4是数据集examplel_3的子集。注意example1_3数据集要在本次SAS运行中已经存在;“keeptimelogprice”告诉系统数据集examplel_4中只需要保留两个变量:一个是time,一个是logprice。“wheretime>=01mar2005”d”指令系统只将数据集example1_3中的时间大于等于2005年3月1日的那些数据输入数据集examplel_4。2.4缺失值插值data examplel_5;input price;time=intnx('month',01jan2005'd,_n_-1);format time date.icards;3.413.45.....3.533.45;proc expandddata=examplel_5out=examplel_6;id time;5
5 2.3 子集 data example1_4; set example1_3; keep time logprice; where time>='01mar2005'd; proc print data=example1_4; run; 实验结果: 实验分析:当我们只需要分析一个时间序列中的部分序列值时,这时可以在 data 步中建立一个子集。例如在本例中,我们只需要分析数据集 example1_3 中 2005 年 3-5 月的对数价格序列,建立相应的子集 example1_4。“data example1_4” 告诉系统要建立一个名为 example1_4 的临时数据集;“set example1_3”是说数 据集 example1_4 是数据集 example1_3 的子集。注意 example1_3 数据集要在本 次 SAS 运行中已经存在;“keep time logprice”告诉系统数据集 example1_4 中只 需要保留两个变量:一个是 time,一个是 logprice。“where time>=‘01mar2005’ d”指令系统只将数据集 example1_3 中的时间大于等于 2005 年 3 月 1 日的那些 数据输入数据集 example1_4。 2.4 缺失值插值 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 expand data=example1_5 out=example1_6; id time;
procprint data=examplel_5;proc print data=examplel_6;run;实验结果:ObstimepriceObstimeprice123.4101JAN05128401JAN053.410003.4501FEB0501FEB053.45000100401MAR0501MAR053.506933.5301APR0501APR053.5300053.4501MAY0501MAY053.45000实验分析:有时观察值序列会有缺失值,这会影响我们的分析。这时可以使用expand过程,使用插值方法补全缺失值。在本例子中2005年3月1日price的观察值缺失,故我们使用以上程序对缺失值进行补插,在该程序中“procexpanddata=examplel5out=examplel6”指令系统将数据集example15中的所有缺失值用插值的方法补全,并将补全后的数据集另存为数据集examplel_6。实验二,时间序列的预处理一、实验目的:掌握时间序列预处理方法。二、实验内容:绘制时序图,平稳性和纯随机性检验。三、实验要求:熟悉时间序列分析预处理的基本语句。四、实验时间:1小时。五、实验软件:SAS系统。六、实验步骤1绘制时序图data example2 l;input pricel price2;time=intnx('month',01jul2004d,_n_-1);format time date.;cards;12.8515.2113.2914.2312.41 14.6915.21 13.2714.2316.756
6 proc print data=example1_5; proc print data=example1_6; run; 实验结果: 实验分析:有时观察值序列会有缺失值,这会影响我们的分析。这时可以使 用 expand 过程,使用插值方法补全缺失值。在本例子中 2005 年 3 月 1 日 price 的观察值缺失,故我们使用以上程序对缺失值进行补插,在该程序中“proc expand data=example1_5 out=example1_6”指令系统将数据集 example1_5 中的所有缺失 值用插值的方法补全,并将补全后的数据集另存为数据集 example1_6。 实验二 时间序列的预处理 一、实验目的:掌握时间序列预处理方法。 二、实验内容:绘制时序图,平稳性和纯随机性检验。 三、实验要求:熟悉时间序列分析预处理的基本语句。 四、实验时间:1 小时。 五、实验软件:SAS 系统。 六、实验步骤 1 绘制时序图 data example2_1; input price1 price2; time=intnx('month','01jul2004'd,_n_-1); format time date.; cards; 12.85 15.21 13.29 14.23 12.41 14.69 15.21 13.27 14.23 16.75