如何遍历 SAS 中的文件? [英] How to iterate through files in SAS?
本文介绍了如何遍历 SAS 中的文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想遍历特定文件夹中的文件,并提取文件名的子字符串.有没有简单的方法可以做到这一点?
I would like to iterate through files in a particular folder, and extract substrings of their files names. Is there an easy way to do this?
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,而不是文件夹.如果您正在查找文件夹中的文件,您可以使用:
Firstly, I'd point out that "dir" appears to be a misspelt libref, not a folder. If you are looking for files in a folder, you can use:
%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: emp");
如果您在库中寻找数据集,您可以使用:
If you are looking for datasets in a library, you can use:
proc sql;
create table datasets as
select substr(memname,1,5) as dataset
from dictionary.tables
where libname='LIB'; /* must be uppercase */
任何一种方法都会产生一个文件"数据集,随后可以逐步执行"..
either approach will produce a dataset of 'files' which can be subsequently 'stepped through'..
这篇关于如何遍历 SAS 中的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文