我想遍歷特定文件夾中的文件,並提取它們文件名的子字符串。是否有捷徑可尋?如何遍歷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的搜索會產生很多結果。 –