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

[转载]sas pipe

(2015-11-14 09:01:07)
标签:

转载

分类: SAS编程及应用
原文地址:sas pipe作者:bxfly
    一、介绍pipe
    首先pipe是什么,它是SAS中两中处理间的信息传输管道,其中存在两种Pipe,即:Unnamed pipe和Named pipe。后者主要处理SAS和其他软件间的信息交流,前者则可以用来调用外部代码或在不创建中间文件情况下更改输入、输出、报错等信息。这是利用主程序和子程序间的关系,其中filename 语句下面的是子程序。Unnamed Pipe 与filename一起使用可以用来解决涉及外部文件的很多问题,姑且算作Filename语句的一种延伸吧,语法如下:
      filename pipe “”;
例如:
filename temp pipe "dir F:南昌项目全省气象资料ETO-1981-2012ETO*.txt /b"; 
 data name;
  infile temp truncover;
  input name $2000.;
run;
proc print data=name;
run;
http://s3/mw690/002DdZ3Hgy6LDajkFJU52&690pipe" TITLE="[转载]sas pipe" />

此处强调两点:
1、这里现实的是相对路径,如果需要绝对路径,则代码换成:
filename temp pipe "dir F:南昌项目全省气象资料ETO-1981-2012ETO*.txt /b/s";
相关的dos中dir命令见链接:
http://www.360doc.com/content/09/1213/13/5029_11012595.shtml

2、需要在infile后面添加truncover。(不太明白为什么)

二、实现批量导入
(1)利用两个input全部导入,对于命名没规律的文件比写append方法效率高
filename dir
pipe pipe “dir c:Tempcrackman”;/*建立管道,获取文件夹crackman下文件的信息并放在dirpipe中,dirpipe是一个
虚拟的文件,随着pipe的建立而建立,pipe的断开而消失/
data ReadPipe(drop=DateString);
infile dirpipe firstobs=8 truncover;
input DateString $ 1-10 @;
if DateString=” ” then stop;
input @1 Date : yymmdd10. Time & : time.
Bytes : comma. FileName : $64.;
if Bytes ge 0;
format Date mmddyy10. Time timeampm8. Bytes comma18.;
run;
proc print data=ReadPipe;
title1 “Files identified through unnamed pipe”;
run;
filename dirpipe clear;
(2)、利用宏将文件导入
filename indata pipe "dir d:sasxml /b";
data vname;
length fname $20.;
infile indata truncover;
input fname $20.;
call symput ('nvars',_n_);
run;
%macro want;
%do i=1 %to &nvars.;
        data _null_;
                set vname;
                if _n_=&i;
                call symput ('file',fname);
        run;
        data tmp;
                infile "d:sasxml&file." firstobs=2;
                input x y z;
        run;
        proc datasets;
                append base=want data=tmp;
        quit;
%end;
%mend;
%want

0

  

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

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

新浪公司 版权所有