作者luenchang (luen)
看板Statistics
标题[问题] Check existence of paths in SAS
时间Fri Sep 17 13:33:55 2021
Dear SAS users,
请先进们是如何在SAS里检查资料夹的路径是否正确? 我download了别人的SAS macro
, 在以下程式例子称为 check_existence_single_directory, 一次只可以检查1个资料夹
,程式没有问题,我用这个macro检查了3个已经存在的folders, 全部都
有找到。接着我试着把macro改成可以检查多个资料夹,在以下程式例子称为check_existence_directory
,我再把那3个路径放进去检查,前2个okay,第3个路径被SAS在句点的地方断掉。请教该如何
改进我的macro? 照理说如果路径内的special characters是问题所在,那第一个macro应
该也会有问题。
我在SAS community上的发文,有syntax highlighting, 程式较容易看。
https://communities.sas.com/t5/SAS-Programming/Check-existence-of-multiple-directories-and-mask-special/td-p/768211
我再把程式贴如下:
以下是3个资料夹路径
/*Set up local directory*/
%let drive_E= E:\Lab_MarkS\lunC_work ;
%let folder_path_SAS_macro=
&drive_E.\library_genetics_epidemiology\SAS_macros_chang ;
%let folder_path_Exp108=
&drive_E.\Immunohistochemistry_images\data_output\AP_Exp108.1_PeterMac-lungCancer-CD8-PD1\analysis-results
;
以下是一次检查1个路径的SAS macro (working)
/*SAS macro to check existence of a single directory (working)*/
%macro check_existence_single_directory(dir=) ;
*options noxwait;
%local rc fileref ;
%let rc = %sysfunc(filename(fileref,&dir)) ;
%if %sysfunc(fexist(&fileref)) %then
%put NOTE: The directory "&dir" exists ;
%else
%do ;
%sysexec md &dir ;
%put %sysfunc(sysmsg()) The directory has been created. ;
%end ;
%let rc=%sysfunc(filename(fileref)) ;
%mend check_existence_single_directory ;
用以上macro检查路径 (working)
/*Check existence of folders*/
%check_existence_single_directory(dir=&drive_E.);
%check_existence_single_directory(dir=&folder_path_SAS_macro.);
%check_existence_single_directory(dir=&folder_path_Exp108.);
修改以上的macro (not working on paths containing special characters)
/*SAS macro to check existence of multiple directories (working?)*/
%macro check_existence_directory(paths=) ;
*options noxwait;
%local rc fileref ;
/*Loop thru each macro parameter value using do loop*/
%do i=1 %to %sysfunc(countw(&paths.));
/*Assign current directory to a macro variable path*/
%let path=%scan(&paths.,&i.);
/*Assign the external path to a file reference then to a new macro
variable rc(?)*/
%let rc = %sysfunc(filename(fileref,&path.)) ;
%if %sysfunc(fexist(&fileref)) %then
%put NOTE: The directory "&path." exists ;
%else
%do ;
/*If the path is not found, create that directory*/
%sysexec md &path. ;
%put %sysfunc(sysmsg()) The directory has been created. ;
%end;
%let rc=%sysfunc(filename(fileref)) ;
%end; /*Close the do loop*/
%mend check_existence_directory ;
用以上macro检查多个路径
%check_existence_directory(paths=&drive_E. &folder_path_SAS_macro.
&folder_path_Exp108.) ;
%check_existence_directory(paths=%str(&drive_E. &folder_path_SAS_macro.
&folder_path_Exp108.)) ;
因为第3个路径没有正确被解读,Windows cmd.exe跳了出来,并且新增了资料夹
SAS log里也出现了些warning. 不知为何SAS在C:\Users\lunC底下做事,这并不是我
所指定的路径
NOTE: The directory "E:\Lab_MarkS\lunC_work" exists
NOTE: The directory
"E:\Lab_MarkS\lunC_work\library_genetics_epidemiology\SAS_macros_chang"
exists
WARNING: Physical file does not exist,
E:\Lab_MarkS\lunC_work\Immunohistochemistry_images\data_output\AP_Exp108. The
directory has
been created.
WARNING: Physical file does not exist, C:\Users\lunC\1_PeterMac. The
directory has been
created.
WARNING: Physical file does not exist, C:\Users\lunC\lungCancer. The
directory has been
created.
WARNING: Physical file does not exist, C:\Users\lunC\CD8. The directory has
been created.
WARNING: Physical file does not exist, C:\Users\lunC\PD1\analysis. The
directory has been
created.
WARNING: Physical file does not exist, C:\Users\lunC\results. The directory
has been created.
--
※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 110.174.219.126 (澳大利亚)
※ 文章网址: https://webptt.com/cn.aspx?n=bbs/Statistics/M.1631856838.A.0FC.html