对前面文章SAS编程-宏:查询文件夹中的各类文件名称,做一次勘误和更新,更新的汇总程序附于文末。
1. 勘误
当只输出给定路径中的文件夹名称时,输出条件应该是不包含文件类型后缀的点".
",感谢@星罗浩瀚的指正。
对应程序更新如下:
%if %index(%upcase(&type.), FOLDER) %then %do;
if index(filename, ".") = 0 then output;
%end;
可以结合输出路径下所有文件名称的结果,理解这一处更新。
%get_filename(
dirpath = /userdata/stat/tables
,outdt = res
,type =
);
输出结果中,文件夹名称是不带后缀点号".
"的。
2. 更新
之前的文章中,宏循环的条件判断使用的是%else %if
语句,这个确实可以筛选出特定类型的文件。
但项目中Listing的输出,可能既有RTF
文件,又有XLSX
文件。为了同时筛选出,这两类文件,将%else %if
语句更新为%if
语句。这样, 通过控制文件类型参数值 (type = rtf xlsx
),就可以同时输出这两类文件。
%if %index(%upcase(&type.), RTF) %then %do;
if strip(scan(filename, 2, "."))="rtf" and substr(strip(filename), 1, 1) ne "~" then output;
%end;
%if %index(%upcase(&type.), XLSX) %then %do;
if strip(scan(filename, 2, "."))="xlsx" and substr(strip(filename), 1, 1) ne "~" then output;
%end;
测试文件夹如下:
文件类型参数设置为rtf xlsx
:
%get_filename(
dirpath = /userdata/stat/listings/output
,outdt = res
,type = rtf xlsx
);
输出结果如下:
3. 汇总代码如下:
/*=============================================================================================================
Purpose:
Get filenames in the folder.
Version History:
Version Date Programmer Description
------- ---- ---------- -----------
1.0 09Dec2023 Jihai Release the first version
1.1 10Dec2023 Jihai 1) Update the condition for FOLDER selection;
2) Update %else %if --> %if.
==============================================================================================================*/
**Macro to get file name in the folder;
%macro get_filename(
dirpath = /*folder path*/
,outdt = res /*Output data set*/
,type = /*File type: sas,rtf, xlsx, xml, sas7bdat, folder, Missing */
);
%if "&dirpath." ne "" %then %do;
%local dirpath_tmp slash;
%let slash = %substr(%sysfunc(compress(&dirpath., :_ , a d)), 1, 1);
*Remove trailing slash;
%if "%substr(&dirpath., %length(dirpath.), 1)" = "&slash." %then %let dirpath_tmp = %substr(&dirpath., 1, %length(dirpath.)-1);
%else %let dirpath_tmp = &dirpath.;
**Dopen--Get filepath;
data &outdt.;
fileres = filename("dirpath", "&dirpath_tmp");
dirid = dopen("dirpath");
num = dnum(dirid);
length direct filename filepath $200;
if dirid > 0 and num > 0 then do;
do i = 1 to num;
direct = "&dirpath_tmp";
filename = dread(dirid, i);
filepath = catx("&slash.", direct, filename);
%if %upcase(&type.) = SAS %then %do;
if strip(scan(filename, 2, "."))="sas" and substr(strip(filename), 1, 1) ne "~" then output;
%end;
%if %index(%upcase(&type.), RTF) %then %do;
if strip(scan(filename, 2, "."))="rtf" and substr(strip(filename), 1, 1) ne "~" then output;
%end;
%if %index(%upcase(&type.), XLSX) %then %do;
if strip(scan(filename, 2, "."))="xlsx" and substr(strip(filename), 1, 1) ne "~" then output;
%end;
%if %index(%upcase(&type.), XML) %then %do;
if strip(scan(filename, 2, "."))="xml" and substr(strip(filename), 1, 1) ne "~" then output;
%end;
%if %index(%upcase(&type.), SAS7BDAT) %then %do;
if strip(scan(filename, 2, "."))="sas7bdat" and substr(strip(filename), 1, 1) ne "~" then output;
%end;
%if %index(%upcase(&type.), FOLDER) %then %do;
if index(filename, ".") = 0 then output;
%end;
%if %upcase(&type.) = %then %do;
output;
%end;
%if %length(&type.) > 0 and %index(SAS RTF XLSX XML SAS7BDAT FOLDER, %upcase(&type.)) = 0 %then %do;
filename = "There is no this type file (.&type.) !";
filepath = " ";
output;
stop;
%end;
end;
end;
keep filename filepath;
proc sort;
by filename;
run;
%end;
%else %do;
%put %str(Warn)ing: The dirpath is missing! ;
%end;
%mend get_filename;
%*get_filename(
dirpath = /userdata/stat/tables
,outdt = res
,type =
);
感谢阅读,欢迎关注:SAS茶谈!
若有疑问,欢迎评论交流!