这篇文章用于宏程序备份。
这个宏程序用于查询给定文件夹中的各类文件名称,相关语法内容可以参考文章SAS编程:Dopen系列函数介绍,宏程序于文末备份。
查询SAS文件名称时,宏程序调用如下:
%get_filename(
dirpath = /userdata/stat/tables
,outdt = res
,type = sas
);
结果数据集如下:
汇总代码如下:
/*=============================================================================================================
Purpose:
Get filenames in the folder.
Version History:
Version Date Programmer Description
------- ---- ---------- -----------
1.0 09Dec2023 Jihai Release the first version
==============================================================================================================*/
**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;
%else %if %index(%upcase(&type.), RTF) %then %do;
if strip(scan(filename, 2, "."))="rtf" and substr(strip(filename), 1, 1) ne "~" then output;
%end;
%else %if %index(%upcase(&type.), XLSX) %then %do;
if strip(scan(filename, 2, "."))="xlsx" and substr(strip(filename), 1, 1) ne "~" then output;
%end;
%else %if %index(%upcase(&type.), XML) %then %do;
if strip(scan(filename, 2, "."))="xml" and substr(strip(filename), 1, 1) ne "~" then output;
%end;
%else %if %index(%upcase(&type.), SAS7BDAT) %then %do;
if strip(scan(filename, 2, "."))="sas7bdat" and substr(strip(filename), 1, 1) ne "~" then output;
%end;
%else %if %index(%upcase(&type.), FOLDER) %then %do;
if index(filename, ".") then output;
%end;
%else %if %upcase(&type.) = %then %do;
output;
%end;
%else %if %length(&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
);
感谢阅读,欢迎关注:SAS茶谈!
若有疑问,欢迎评论交流!
网友评论