作者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/m.aspx?n=bbs/Statistics/M.1631856838.A.0FC.html