加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

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

(2012-12-23 00:45:18)
标签:

杂谈

SAS 学习与总结1--初写程序

1. 今天老板逼着要学习SAS,所以硬着头皮来学习,似乎不是很难。首先写下第一个程序:

 

 

libname mylib 'D:\source';

data mylib.XYZ;//给定数据集名称

  input x y z;//数据集具有3个变量. 分别是x,y,z.他们都是数值型,如果是字符型,则需要加上$符号.字符型数据不可超过8个.

  datalines;//SAS 把每一列叫做observation,把每一行就做variable。

1  1  12.4 

1  2  11.3 

1  3   1.4 

2  1   2.1 

2  2  19.4 

2  3  10.0 

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;
   getnames=no;
run;

有了数据后我们就要开始画图了,执行画图的命令采用如下的语句:

libname mylib 'D:\source';//指定载入数据库,如果不指定,则默认为work库。确记者个文件路径一定要存在
data mylib.mydata;//指定数据库中的数据表
run;

proc import datafile="D:\Buffer_Vbd.txt" out=mylib.mydata dbms=tab replace;//这个文件是没有列名的文件。
   getnames=no;
run;

proc gcontour data=mylib.mydata;
   plot VAR2*VAR3=VAR12;
run;


2.今天终于可以自己读写文件了,而且绘图。

filename VbdFile 'D:\source\Buffer_Vbd_WithHeader.txt';

libname MyLib "D:\source";
proc import datafile=VbdFile out=mylib.BufferVbd DBMS=tab replace;
    getnames=yes;
run;

proc print data=mylib.BufferVbd;
run;

proc plot data=mylib.BufferVbd;
   plot Vbd*Wafer_Name;
   label Vbd='Buffer Breakdwon Voltate(V)'
         Wafer_Name='Wafer Name';   
   title 'Buffer Breakdown Voltage Vs. Wafer';
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系统与大型数据库连接时,这是比较简单可行的方法。

 

 

Question:I just ran a SAS program and no output was displayed. When I checked the SAS log, I received the following message: "WARNING: No output destinations active." What should I do?Answer:

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.

 

 

 You can also read multiple observations in each line of data. Simply add the @@ symbol at the end of the input statement:

data XYZ;
  input x y z @@;
  datalines;
12.4 1  11.3 1   1.4
  2.1 2  19.4 1  10.0
;
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'  from ever being read). With the @@ symbol, SAS will continue filling the variables until it reaches the end of the line.


0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有