SAS編程-宏:檢查缺失或多余的文件

日常項(xiàng)目中,程序或Outputs一般會(huì)提前確定好剿涮,通過(guò)Excel Tracker或者文件管理系統(tǒng)進(jìn)行管理言津。各個(gè)公司一般也有相關(guān)工具進(jìn)行檢查,以確保Tracker中的程序或Outputs都在輸出文件夾中取试。

不過(guò)悬槽,本公司工具不會(huì)檢查文件夾中多余的程序或Outputs。于是瞬浓,寫(xiě)了個(gè)SAS宏程序用于實(shí)現(xiàn)這個(gè)功能初婆。這個(gè)宏程序也可用于檢查其它所需文件,例如xml

宏程序匯總在文章末尾磅叛,若參考引用屑咳,需要結(jié)合各自項(xiàng)目路徑結(jié)構(gòu),以及Tracker信息弊琴,來(lái)更新程序兆龙。

宏程序的分為3個(gè)部分:

  1. 獲取系統(tǒng)或Tracker中的文件名稱(chēng)
  2. 獲取文件夾中已有的文件名稱(chēng)
  3. 通過(guò)比較以上兩類(lèi)名稱(chēng),獲取缺失或多余的文件

第一部分敲董,可能通過(guò)公司的管理工具獲取紫皇,也可以簡(jiǎn)單導(dǎo)入EXCEL Tracker。

options validvarname = v7;
proc import datafile = "/xxx/xxx/xxx.xlsx"
   out = tfls dbms = xlsx replace;
run;

第二部分腋寨,可以通過(guò)Dopen系列函數(shù)獲取文件名稱(chēng)聪铺,具體可以參考文章SAS編程:如何獲取某路徑下文件的名稱(chēng)?萄窜,完整內(nèi)容可以整合成一個(gè)宏程序铃剔。

%macro get_file(dirpath =, outdt =, type = sas );

%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 %index(%upcase(&type), SAS) %then %do;
        if strip(scan(filename, 2, "."))="sas" then output;
      %end;

      %if %index(%upcase(&type), RTF) %then %do;
        if strip(scan(filename, 2, "."))="rtf" then output;
      %end;

      %if %index(%upcase(&type), XLSX) %then %do;
        if strip(scan(filename, 2, "."))= "xlsx" then output;
      %end;

      end;
  end;

  keep filename filepath;

  proc sort;
    by filename;
  run;
%end;
%mend get_file;

%get_file(
  dirpath = &path_ana./tables
  ,outdt = source_pgm_tables
  ,type = sas 
);

第三部分,可以通過(guò)SQL簡(jiǎn)單的篩選呈現(xiàn)缺失或多余的文件脂倦。

%let out_type = tables;
%let type = T;

proc sql noprint;
  **in tracker not in folder;
  create table source_in_tracker_&out_type. as
    select output_number, base_output_name, program_name, programmer, tester
    from tfls
    where output_type = "&type." and program_name not in
    (
      select filename from source_pgm_&out_type.
    )
  ;

  **in folder not in tracker;
   create table source_in_folder_&out_type. as
      select *
      from source_pgm_&out_type.
      where filename not in
      (
        select program_name from tfls
      )
  ;
quit;

以上三部分番宁,可以整合成一個(gè)完整的宏程序,用于檢查同一個(gè)分析文件夾下的TFLs程序或Output缺失或多余的情況赖阻。

以我當(dāng)前公司的數(shù)據(jù)結(jié)構(gòu)為例蝶押,匯總程序如下。若讀者想要引用火欧,需結(jié)合項(xiàng)目文件結(jié)構(gòu)以及Tracker信息進(jìn)行更新棋电。

***1. Get file names in tracker (Company internal tool);
%global path_ana pi_sas;

%let path_ana = /xxx/xxx;
%let pi_sas = pi_xxx_xxx.sas;

%include "&path_ana./docs/pi/&pi_sas.";

%pi_amg757_bla_iss_90d_2023(
  lib = work
  ,txtpath = &path_ana./docs/pi/
  ,outds = all
);


***2. Macro to get  file names in folder;
%macro get_file(dirpath =, outdt =, type = sas );

%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 %index(%upcase(&type), SAS) %then %do;
        if strip(scan(filename, 2, "."))="sas" then output;
      %end;

      %if %index(%upcase(&type), RTF) %then %do;
        if strip(scan(filename, 2, "."))="rtf" then output;
      %end;

      %if %index(%upcase(&type), XLSX) %then %do;
        if strip(scan(filename, 2, "."))= "xlsx" then output;
      %end;

      %if %index(%upcase(&type), XML) %then %do;
        if strip(scan(filename, 2, "."))= "xml" then output;
      %end;

      end;
  end;

  keep filename filepath;

  proc sort;
    by filename;
  run;
%end;
%mend get_file;


***3. Macro to check folder file;
%macro check_folder_file(out_type = tables);

%local type;
%if %upcase(&out_type) = TABLES %then %let type = T;
%else %if %upcase(&out_type) = LISTINGS %then %let type = L;
%else %if %upcase(&out_type) = FIGURES %then %let type = F;

**3.1 Source folder;
%get_file(
  dirpath = &path_ana./&out_type.
  ,outdt = source_pgm_&out_type.
);

proc sql noprint;
  **in tracker not in folder;
  create table source_in_tracker_&out_type. as
    select output_number, base_output_name, program_name, programmer, tester
    from tfls
    where output_type = "&type." and program_name not in
    (
      select filename from source_pgm_&out_type.
    )
  ;

  **in folder not in tracker;
   create table source_in_folder_&out_type. as
      select *
      from source_pgm_&out_type.
      where filename not in
      (
        select program_name from tfls
      )
  ;
quit;


**3.2 QC folder;
%get_file(
  dirpath = &path_ana./&out_type./validation
  ,outdt = qc_pgm_&out_type.
);

proc sql noprint;
  **in tracker not in folder;
  create table qc_in_tracker_&out_type. as
    select output_number, testing_program_name, tester
    from tfls
    where output_type = "&type." and testing_program_name not in
    (
      select filename from qc_pgm_&out_type.
    )
  ;

  **in folder not in tracker;
   create table qc_in_folder_&out_type. as
      select *
      from qc_pgm_&out_type.
      where filename not in
      (
        select testing_program_name from tfls
      )
  ;
quit;


**3.3 Output folder;
%get_file(
  dirpath = &path_ana./&out_type./output
  ,outdt = output_&out_type.
  ,type = rtf-xlsx
);

proc sql noprint;

  **in tracker not in folder;
  create table output_in_tracker_&out_type. as
    select *
    from tfls
    where output_type = "&type." and output_name not in
    (
      select filename from output_&out_type.
    )
  ;

  **in folder not in tracker;
   create table output_in_folder_&out_type. as
      select *
      from output_&out_type.
      where filename not in
      (
        select output_name from tfls
        where output_type = "&type."
      )
  ;

**3.4 xml template folder (internal use);
%get_file(
  dirpath = &path_ana./docs/templates
  ,outdt = tflgen_template
  ,type = xml
);

proc sql noprint;

  **in tracker not in folder;
  create table tflgen_template_in_tracker as
    select *
    from tfls
    where tflgen_allow_code_generation = "Yes" and strip(tflgen_template_name)||".xml" not in
    (
      select filename from tflgen_template
    )
  ;

  **in folder not in tracker;
   create table tflgen_template_in_folder  as
      select *
      from tflgen_template
      where filename not in
      (
        select strip(tflgen_template_name)||".xml" from tfls
        where tflgen_allow_code_generation = "Yes" 
      )
  ;

quit;


%mend check_folder_file;

%check_folder_file(out_type = tables);

%check_folder_file(out_type = listings);

%check_folder_file(out_type = figures);

感謝閱讀, 歡迎關(guān)注:SAS茶談苇侵!
若有疑問(wèn)赶盔,歡迎評(píng)論交流!

梳理不易榆浓,轉(zhuǎn)載請(qǐng)注明出處 (by Jihai / SAS茶談)

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末于未,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子陡鹃,更是在濱河造成了極大的恐慌烘浦,老刑警劉巖,帶你破解...
    沈念sama閱讀 221,406評(píng)論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件萍鲸,死亡現(xiàn)場(chǎng)離奇詭異闷叉,居然都是意外死亡,警方通過(guò)查閱死者的電腦和手機(jī)脊阴,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,395評(píng)論 3 398
  • 文/潘曉璐 我一進(jìn)店門(mén)握侧,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)蚯瞧,“玉大人,你說(shuō)我怎么就攤上這事品擎÷窈希” “怎么了?”我有些...
    開(kāi)封第一講書(shū)人閱讀 167,815評(píng)論 0 360
  • 文/不壞的土叔 我叫張陵孽查,是天一觀(guān)的道長(zhǎng)饥悴。 經(jīng)常有香客問(wèn)我坦喘,道長(zhǎng)盲再,這世上最難降的妖魔是什么? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 59,537評(píng)論 1 296
  • 正文 為了忘掉前任瓣铣,我火速辦了婚禮答朋,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘棠笑。我一直安慰自己梦碗,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,536評(píng)論 6 397
  • 文/花漫 我一把揭開(kāi)白布蓖救。 她就那樣靜靜地躺著洪规,像睡著了一般。 火紅的嫁衣襯著肌膚如雪循捺。 梳的紋絲不亂的頭發(fā)上斩例,一...
    開(kāi)封第一講書(shū)人閱讀 52,184評(píng)論 1 308
  • 那天,我揣著相機(jī)與錄音从橘,去河邊找鬼念赶。 笑死,一個(gè)胖子當(dāng)著我的面吹牛恰力,可吹牛的內(nèi)容都是我干的叉谜。 我是一名探鬼主播,決...
    沈念sama閱讀 40,776評(píng)論 3 421
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼踩萎,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼停局!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起香府,我...
    開(kāi)封第一講書(shū)人閱讀 39,668評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤董栽,失蹤者是張志新(化名)和其女友劉穎,沒(méi)想到半個(gè)月后回还,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體裆泳,經(jīng)...
    沈念sama閱讀 46,212評(píng)論 1 319
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,299評(píng)論 3 340
  • 正文 我和宋清朗相戀三年柠硕,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了工禾。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片运提。...
    茶點(diǎn)故事閱讀 40,438評(píng)論 1 352
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖闻葵,靈堂內(nèi)的尸體忽然破棺而出民泵,到底是詐尸還是另有隱情,我是刑警寧澤槽畔,帶...
    沈念sama閱讀 36,128評(píng)論 5 349
  • 正文 年R本政府宣布栈妆,位于F島的核電站,受9級(jí)特大地震影響厢钧,放射性物質(zhì)發(fā)生泄漏鳞尔。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,807評(píng)論 3 333
  • 文/蒙蒙 一早直、第九天 我趴在偏房一處隱蔽的房頂上張望寥假。 院中可真熱鬧,春花似錦霞扬、人聲如沸糕韧。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 32,279評(píng)論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)萤彩。三九已至,卻和暖如春斧拍,著一層夾襖步出監(jiān)牢的瞬間雀扶,已是汗流浹背。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 33,395評(píng)論 1 272
  • 我被黑心中介騙來(lái)泰國(guó)打工饮焦, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留怕吴,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 48,827評(píng)論 3 376
  • 正文 我出身青樓县踢,卻偏偏與公主長(zhǎng)得像转绷,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子硼啤,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,446評(píng)論 2 359

推薦閱讀更多精彩內(nèi)容