2018年5月9日水曜日

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

カタログにプログラムを格納するサンプルです。探したら良い例が見つからなかったのでメモしておきます。

/* ディレクトリにあるMM_MISC.sasをカタログに格納する */
filename there '/tmp' ;
libname mylib '/tmp/sashelp' ;

%let trialcode = MM_MISC;
%let catname = modelmgr;

data _null_ ;
    filename mycat catalog "mylib.&catname" lrecl= 256 ;
    file   mycat("&trialcode..source") ;
    infile   there("&trialcode..sas") ;
    input ;
    put _infile_ ;
run;

proc catalog c=mylib.&catname;
    contents;
run;
quit;

/* 格納したプログラムを読み込む */
filename chkinit catalog "mylib.&catname..&trialcode..source";
%include chkinit;


2018年5月2日水曜日

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

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

/* ソースコードをファイルに出力するマクロ */
%macro _myfile(catalog=, memname=, dir=);
 data _null_;
  infile "&catalog..&memname..source" catalog;
  file "&dir./&memname..sas";
  input;
  put _infile_;
 run;
%mend;

/* カタログに含まれているソースコードをディレクトリに出力するマクロ */
%macro extract_source(in=, dir=);
 %local out memname;

 data;
  stop;
 run;

 %let out=&SYSLAST;
 %let memname=%substr(&out, 6); 

 proc catalog catalog=∈
    contents out=&out;
 run;

 data _null_;
  set &out(keep=name type);
  where type eq 'SOURCE';
  attrib catalog length=$32;
  attrib dir length=$128;
  catalog = "&in";
  dir = "&dir";
  call execute('%_myfile(catalog='||catalog||", memname="||name||", dir="||dir||")");
 run;

 proc datasets lib=work nolist;
  delete &memname;
 quit;
%mend;

%extract_source(in=sashelp.modelmgr, dir=/var/tmp);