在SAS中搜索是否存在部分文件名的文件
我需要确定目录中是否有部分名称(不是全名,而是文件名%.txt)的文件 如何使用通配符(%and)搜索文件的可用性在SAS中搜索是否存在部分文件名的文件,sas,Sas,我需要确定目录中是否有部分名称(不是全名,而是文件名%.txt)的文件 如何使用通配符(%and)搜索文件的可用性 我们可以在sas的FILEEXIST函数中使用通配符吗?此代码将返回文件名,但它将读取整个文件,因此如果文件太大,可能不是一个好主意: filename search pipe "dir /B c:\temp\bet*.txt"; data _Null_; infile search; input; put _infile_; run; filename search
我们可以在sas的FILEEXIST函数中使用通配符吗?此代码将返回文件名,但它将读取整个文件,因此如果文件太大,可能不是一个好主意:
filename search pipe "dir /B c:\temp\bet*.txt";
data _Null_;
infile search;
input;
put _infile_;
run;
filename search "c:\temp\bet*.txt";
data _Null_;
attrib filevar length=$1024;
retain filevar "";
infile search filename=filevar;
input;
if filevar ne lag(filevar) then put filevar;
run;
我尝试使用firstobs和obs从每个文件中只读取一行,但没有成功
如果您对仅获取第一个匹配的文件名感到满意,则此代码应执行以下操作:
filename search "c:\temp\bet*.txt";
data _Null_;
attrib filevar length=$1024;
infile search filename=filevar;
put filevar;
run;
如果没有匹配项,将出现以下错误:
ERROR: Physical file does not exist, c:\temp\bet*.txt.
斯蒂格·艾德的方法简单有效。 在SAS中处理目录和文件的另一种方法是使用函数dopen、dnum、dread和dclose。 下面是一个扫描目录中所有文件的示例:
%let dir= your_path;
data _null_;
rc=filename("filrf","&dir.");
did=dopen("filrf");
nfile=dnum(did);
do j = 1 to nfile;
filename= dread(did,j);
str= 'File n. '||strip(j)||' has name: '||filename;
put str;
end;
rc=dclose(did);
run;
要调查某个特定文件的存在性,只需添加某种检查,如Stig的示例所示。
也可以使用相同的函数在宏语言中处理此问题
下面是一个示例,其中包括对文件名的检查:
%macro search(dir=,str=);
%global file_exists;
/* Assigns a fileref to the directory and opens the directory */
%let rc=%sysfunc(filename(filrf,&dir.));
%let did=%sysfunc(dopen(&filrf.));
/* Returns the number of members in the directory */
%let nfile=%sysfunc(dnum(&did.));
/* Loops through entire directory */
%let file_exists=0;
%do j = 1 %to &nfile.;
/* Checks if the j-th member name matches the wildcard */
%put Analyzing file %qsysfunc(dread(&did.,&j.));
%if %index(%qupcase(%qsysfunc(dread(&did.,&j.))),%qupcase(&str.)) > 0
%then %do;
%let file_exists=1;
%put Match found!;
%end;
%end;
%if (&file_exists.=0) %then %put No match found!;
/* Closes the directory */
%let rc=%sysfunc(dclose(&did.));
%mend search;
%search(dir=path_dir, str=wildcard)
%put file_exists= &file_exists.;
%qupcase和%qsysfunc函数的q版本仅用于处理%等特殊字符
希望这有帮助!:) 否,路径中的通配符将使FILEEXIST函数返回0此解决方案适用于Unix和Windows,还是仅适用于一种环境?运行管道命令是否需要管理员权限?是的,它将在UNIX上工作,但您当然需要更改为ls命令。为了通过通过objectspawner启动的SAS会话使用管道文件名设备(如您在EG中所做的),workspaceserver需要管理员设置ALLOWXCMD选项。感谢您的澄清!