介绍sas读入数据文件的几种方法:

标签:
杂谈 |
SAS 学习与总结1--初写程序
1. 今天老板逼着要学习SAS,所以硬着头皮来学习,似乎不是很难。首先写下第一个程序:
libname mylib 'D:\source';
data mylib.XYZ;//给定数据集名称
1
1
1
2
2
2
;
run;
//这一步用来计算mean数值。
proc means data=mylib.XYZ;
var z;
run;
这个程序首先要在D盘中建立起来一个source的文件夹,然后执行了之后会在这个文件夹下面自动建立起来一个xyz.sas7bdat 的文件;其次会生成如下的文件形式:
http://hiphotos.baidu.com/kxw102/pic/item/b33f371fed8433ad1ad57692.jpg
以上的方法是手动输入数据的,但是当有很多的数据时候,就必须得采用infile命令读入txt文本。
如下:
data mylib.XYZ2
infile 'D:\source\vbd.txt';
input Sample $ Vbd;
run;
通过扩展,我们现在可以读取以tab为分割的文件:
libname mylib 'D:\source';
data mylib.mydata;
run;
proc import datafile="D:\Buffer_Vbd.txt" out=mylib.mydata dbms=tab
replace;
run;
有了数据后我们就要开始画图了,执行画图的命令采用如下的语句:
libname mylib
'D:\source';//指定载入数据库,如果不指定,则默认为work库。确记者个文件路径一定要存在
data mylib.mydata;//指定数据库中的数据表
run;
proc import datafile="D:\Buffer_Vbd.txt" out=mylib.mydata dbms=tab
replace;//这个文件是没有列名的文件。
run;
proc gcontour data=mylib.mydata;
run;
2.今天终于可以自己读写文件了,而且绘图。
filename VbdFile 'D:\source\Buffer_Vbd_WithHeader.txt';
libname MyLib "D:\source";
proc import datafile=VbdFile out=mylib.BufferVbd DBMS=tab
replace;
run;
proc print data=mylib.BufferVbd;
run;
proc plot data=mylib.BufferVbd;
run;
quit;
2.
以EXCEL表格为例,介绍sas读入数据文件的几种方法:
(1)用import将数据库导入;
(2)通过制定libname库和引擎;
(3)使用access过程;
(4)通过odbc方式。
用导入的方法比较简单,示例代码如下:
proc import out=t1;
datafile="d:\test.xls"
dbms=excel2000 replace;
range=''14#1$";
getnames=yes;
run;
(2)用逻辑库和引擎的方法代码也很简单:
libname tests excel 'D:\tests.xls';
(3)用ACCESS的过程:
proc access dbms=xls2000;
create work.s0001.access;
path='D:\test.xls';
getnames yes;
scantype=yes;
list all;
create work.s001.view;
select data all;
list view;
run;
简单解释一下上面的语句:
用access并不是把数据文件真正的读入到sas中来,而是先建立一个数据访问描述器create work.s0001.access,用来描述sas对数据库的访问,之后建立一个视图create work.s001.view;视图和sas里的数据文件也不一样,相当于一个查询。用access方法访问数据库的好处是不占用硬盘空间,特别是数据文件特别大时,不需要把文件全部读入就可以进行数据访问,同时数据操作的结果也可以写回到数据库中。
(4)odbc的方法:先手工在控制面板里,新建用户dsn,创建相应数据类型的数据源;然后再sas的资源管理器里,手工新建逻辑库odbc。实际应用时,sas系统与大型数据库连接时,这是比较简单可行的方法。
You are receiving this error message for one of the following reasons:
You ran the ODS LISTING CLOSE command in your Program Editor that turns off the default listing output. To fix this: Open SAS.In the SAS Program Editor window, type ODS LISTING; as the first statement of your SAS program.Run your SAS program again.You deactivated the Listing Output option. To fix this: Open SAS.From the Tools menu, selectOptions.SelectPreferences.Place a check in theCreate Listingcheck box.Close thePreferenceswindow.Run your SAS program again.
data XYZ;
1
2
;
run;
SAS will go through each line of data and fill the variables X, Y,
and Z in turn. Without the @@ symbol, it would switch to the next
line as soon as the last variable was filled (which by the way
prevented in the last example the value 'NY'
喜欢
0
赠金笔