我有一个目录,每周都会在其中创建一个新文件。名称如下:
file_w1.csv
file_w2.csv
file_w3.csv
我需要做的是拾取最新文件(基于修改日期),然后解析文件扩展名之前的2个字符。 因此,在这种情况下,我要使用“ w3”,因为我想用它来知道我要报告哪一周。
如何在SAS中执行此操作?
答案 0 :(得分:1)
与操作系统无关的技术将使用诸如dopen
,fopen
和finfo
之类的SAS外部文件功能来获取有关文件夹及其项目的信息。
考虑以下示例代码,该示例代码在Windows计算机上解析C:\Temp
时会进行可用信息的“完全转储”:
data _null_;
length dfileref fileref $8 folder $200;
rc = filename (dfileref, 'C:\Temp');
did = dopen(dfileref);
if did then do;
do index = 1 to doptnum(did);
featurename = doptname(did,index);
featurevalue = dinfo(did,featurename);
put index= featurename= featurevalue=;
if featurename = 'Directory' then folder = featurevalue;
end;
do dindex = 1 to dnum(did);
entryname = dread(did,dindex);
put dindex= entryname=;
rc = filename(fileref, cats(folder, '/', entryname));
fid = fopen (fileref); * if entry is another folder fid will be 0;
if fid then do;
do findex = 1 to foptnum(fid);
featurename = foptname(fid, findex);
featurevalue = finfo(fid, featurename);
put +2 findex= featurename= featurevalue=;
end;
fid = fclose(fid);
end;
rc = filename(fileref);
end;
did = dclose(did);
end;
rc = filename (dfileref);
run;
检查日志后,您可以缩减将特定的所需信息收集到数据集中所需的代码。然后,您可以使用SQL查询来进一步处理数据:
data csv_files(keep=fullname lastmod where=(fullname like '%.csv'));
length dfileref fileref $8 folder $200;
folder = 'C:\Temp';
rc = filename (dfileref, folder);
did = dopen(dfileref);
if did then do;
do dindex = 1 to dnum(did);
entryname = dread(did,dindex);
rc = filename(fileref, cats(folder, '/', entryname));
fid = fopen (fileref);
if fid then do;
fullname = finfo(fid,'Filename');
lastmod = input(finfo(fid,'Last Modified'), datetime18.); format lastmod datetime18.;
output;
fid = fclose(fid);
end;
rc = filename(fileref);
end;
did = dclose(did);
end;
rc = filename (dfileref);
run;
proc sql;
create table csv_newest as
select *, scan(scan(fullname,-1,'_'),1,'.') as tag
from csv_files
where prxmatch ('/_.+\.csv$/', fullname)
having lastmod = max(lastmod)
;