/*********************************************************************** ** SAS code for running WinBUGS and handling I/O with SAS ** Written by Mike K Smith, PGRD Sandwich ** Contact Mike.K.Smith@Pfizer.Com for more details ** ** Writes batchfile to Winbugs14 directory by default ** "dir" macro variable directs data, model, logfile and SAS output ** Assumes that a valid model file is available ** - it's a good idea to check this using scripts within WinBUGS first ** Need to ensure that variable names are appropriate in the data1 step ** Need to supply valid constants in the data2 step if required. ***********************************************************************/ %macro WINBUGSIO( dir=c:\program files\winbugs14\, datafile1=data1, datafile2=data2, initsfile=init, modelfile=model, logfile=log, batchfile=batch, dsout=WBoutSAS); ***********************************; ** Print out data in column format; ***********************************; options pagesize=32767 nodate nocenter nonumber formdlim=' ' mprint symbolgen mlogic; title1 ' '; filename data1 "&dir.&datafile1..txt"; proc printto file=data1 new; run; proc print data=foo noobs label; label Dose='DOSE[]' obs='OBS[]' outcome='DV[]'; var Dose Obs outcome; run; proc printto; run; data _null_ ; file data1 mod; put@1 "END"; put @1 " "; run; proc printto; run; ***********************************; ** Print out constants in list format; ***********************************; filename data2 "&dir.&datafile2..txt"; data _null_; file data2 new; put@1 "list(N=32,T=4,K=3,dose=c(0,25,50,100,150))"; run; proc printto; run; ***********************************; ** Write batch file; ***********************************; options pagesize=32767 nodate nocenter nonumber formdlim=' ' mprint symbolgen mlogic; title1 ' '; filename fileout2 "c:\program files\winbugs14\&batchfile..txt"; data _null_; file fileout2; put@1 'display("log")'; put@1 "check('&dir.&modelfile..txt')"; put@1 "data('&dir.&datafile1..txt')"; put@1 "data('&dir.&datafile2..txt')"; put@1 "compile(1)"; put@1 "inits(1,'&dir.&initsfile..txt')"; put@1 "gen.inits()"; put@1 "update(1000)"; put@1 "set(alpha)"; put@1 "set(sigma)"; put@1 "update(5000)"; put@1 "stats(*)"; put@1 "save('&dir.&logfile..txt')"; put@1 'display("window")'; run; proc printto; run; ************************************************; ** Execute the WinBUGS run in batch mode ; ************************************************; options xmin noxwait; x cd c:\program files\winbugs14\; x winbugs14.exe /PAR &batchfile; ************************************************; ** Read in log file of batch run which includes ; ** stats for Gibbs Sampler ; ** Calculate new priors for the mean parameters ; ** mu[1] and mu[2]. Using tau=1/variance ; ************************************************; data _null_; retain i j 0; infile "&dir&logfile..txt" expandtabs truncover; length text $200; input text $ 1-200; if scan(text,1)="node" then i=_n_; call symput("i",i+1); if upcase(scan(text,1))="SAVE" then j=_n_; call symput("j",j-1); run; data results; infile "&dir&logfile..txt" firstobs=&i obs=&j expandtabs truncover; length var $20; input var $ mean se MCMCer lowCI median uppCI start samp; rep=&count; run; ***********************************; ** Write stats to SAS dataset; ***********************************; proc append base=&dsout data=results; run; %mend; %winbugsio(dir=c:\foo\, datafile1=data1, datafile2=data2, initsfile=init, modelfile=model, logfile=log, batchfile=batch, dsout=WBoutSAS);