SAS中文论坛

标题: sas pipe [打印本页]

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



此处强调两点:
1、这里现实的是相对路径,如果需要绝对路径,则代码换成:
filename temp pipe "dir F:\南昌项目\全省气象资料\ETO-1981-2012\ETO*.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:\Temp\crackman”;/*建立管道,获取文件夹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:\sas\xml /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:\sas\xml\&file." firstobs=2;
                input x y z;
        run;
        proc datasets;
                append base=want data=tmp;
        quit;
%end;
%mend;
%want


参考:利用filename and pipe读取文件
跟crackman读sas程序(121)–如何获得一个文件夹下所有文件的基本信息  http://crackman.net/?p=1212







欢迎光临 SAS中文论坛 (http://www.mysas.net/forum/) Powered by Discuz! X3.2