我想遍历特定文件夹中的文件,并提取它们文件名的子字符串。是否有捷径可寻?如何遍历SAS中的文件?
lib dir '.../folder';
#iterate through all files of dir and extract first five letters of file name;
#open files and do some processesing, aka data steps proc steps;
我想遍历特定文件夹中的文件,并提取它们文件名的子字符串。是否有捷径可寻?如何遍历SAS中的文件?
lib dir '.../folder';
#iterate through all files of dir and extract first five letters of file name;
#open files and do some processesing, aka data steps proc steps;
首先,我会指出“dir”似乎是拼写错误的libref,而不是文件夹。如果你的文件夹中寻找文件,你可以使用:
%macro get_filenames(location);
filename _dir_ "%bquote(&location.)";
data filenames(keep=fname);
handle=dopen('_dir_');
if handle > 0 then do;
count=dnum(handle);
do i=1 to count;
fname=subpad(dread(handle,i),1,5);/* extract first five letters */
output filenames;
end;
end;
rc=dclose(handle);
run;
filename _dir_ clear;
%mend;
%get_filenames("c:\temp\");
如果您在库寻找数据集,你可以使用:
proc sql;
create table datasets as
select substr(memname,1,5) as dataset
from dictionary.tables
where libname='LIB'; /* must be uppercase */
两种方法会产生一个'档案'的数据集,可以随后'步入'..
有没有一种方法可以在特定分隔符(如_)之前读取文件的子字符串? – lord12
fname = subpad(scan(“some_file”,1,'_'),1,5); –
是否可以遍历由proc sql创建的SQL记录并对其执行一些命令为lib中的每个SAS文件运行proc导出? –
你到目前为止尝试过什么?这是一个非常常见的问题,对SO或Google的搜索会产生很多结果。 –