2018年5月9日水曜日

カタログにSASプログラムを格納するサンプル

カタログにプログラムを格納するサンプルです。探したら良い例が見つからなかったのでメモしておきます。
  1. /* ディレクトリにあるMM_MISC.sasをカタログに格納する */  
  2. filename there '/tmp' ;  
  3. libname mylib '/tmp/sashelp' ;  
  4.   
  5. %let trialcode = MM_MISC;  
  6. %let catname = modelmgr;  
  7.   
  8. data _null_ ;  
  9.     filename mycat catalog "mylib.&catname" lrecl= 256 ;  
  10.     file   mycat("&trialcode..source") ;  
  11.     infile   there("&trialcode..sas") ;  
  12.     input ;  
  13.     put _infile_ ;  
  14. run;  
  15.   
  16. proc catalog c=mylib.&catname;  
  17.     contents;  
  18. run;  
  19. quit;  
  20.   
  21. /* 格納したプログラムを読み込む */  
  22. filename chkinit catalog "mylib.&catname..&trialcode..source";  
  23. %include chkinit;  

2018年5月2日水曜日

カタログに含まれているソースコードを1ずつ取り出してファイルに保存

カタログの中に含まれているソースコードを、1つずつ取り出してディレクトリに出力するマクロです。Model ManagerのAPIの中身を見るために、書きました。

  1. /* ソースコードをファイルに出力するマクロ */  
  2. %macro _myfile(catalog=, memname=, dir=);  
  3.  data _null_;  
  4.   infile "&catalog..&memname..source" catalog;  
  5.   file "&dir./&memname..sas";  
  6.   input;  
  7.   put _infile_;  
  8.  run;  
  9. %mend;  
  10.   
  11. /* カタログに含まれているソースコードをディレクトリに出力するマクロ */  
  12. %macro extract_source(in=, dir=);  
  13.  %local out memname;  
  14.   
  15.  data;  
  16.   stop;  
  17.  run;  
  18.   
  19.  %let out=&SYSLAST;  
  20.  %let memname=%substr(&out, 6);   
  21.   
  22.  proc catalog catalog=∈  
  23.     contents out=&out;  
  24.  run;  
  25.   
  26.  data _null_;  
  27.   set &out(keep=name type);  
  28.   where type eq 'SOURCE';  
  29.   attrib catalog length=$32;  
  30.   attrib dir length=$128;  
  31.   catalog = "&in";  
  32.   dir = "&dir";  
  33.   call execute('%_myfile(catalog='||catalog||", memname="||name||", dir="||dir||")");  
  34.  run;  
  35.   
  36.  proc datasets lib=work nolist;  
  37.   delete &memname;  
  38.  quit;  
  39. %mend;  
  40.   
  41. %extract_source(in=sashelp.modelmgr, dir=/var/tmp);