I am hoping this is a better example. I'd like to call OpenBUGS into SAS and then run the Bayesian analysis. Right now each simulation is being completed sequentially. I would like to take advantage of parallel processing, generate the coda files simultaneously for each simulation and cut back on processing time. Please note that this is only an example. I've included comments so that each step is clear. Thank you.
dm 'log;clear;output;clear; '; /*Clear the log file, results window, working directory*/;
dm 'odsresults; clear';;
PROC DATASETS LIB=work NOlist MEMTYPE=data kill;
options nosymbolgen nomprint nomlogic; /*system options set to default*/
%let path = /panfs/pfs.local/work/example2; /*Define path as a macro variable that can be referenced throughout program*/
%LET J=100; *obs in each simulation;
%LET NumSimul=2; *number of simulations;
DATA Sim_Reg; /*Generate data*/
b0=1; b1=2; b2=3; sig_e=2; seed=20060118;
DO NumSimul = 1 TO &NumSimul;
DO j = 1 to &J;
x1=RANNOR(seed+&NumSimul);
x2=RANNOR(seed+&NumSimul);
e=RANNOR(seed+&NumSimul);
y = b0+b1*x1+b2*x2+sig_e*e;
KEEP NumSimul J y x1 x2;
OUTPUT;
END;
END;
RUN;
DATA model; /*Define Model*/
INPUT model $80.;
CARDS;/*start the model*/
model{
#Model specification
for (i in 1:100) {
y[i]~dnorm(muy[i], Inv_sig2_e)
muy[i]<-b0+b1*x1[i]+b2*x2[i]
}
#priors
b0~dnorm(0, 1.0E-6)
b1~dnorm(0, 1.0E-6)
b2~dnorm(0, 1.0E-6)
Inv_sig2_e~dgamma(1.0E-3, 1.0E-3)
#parameter transformation
Sig2_e<-1/Inv_sig2_e
}
;
RUN;
DATA _NULL_; /*Store model as text file in this location*/
SET model;
FILE "&path/RegModel.txt";
PUT model;
RUN;
DATA INIT; /*Create the initial values*/
INPUT initials $80.;
CARDS;
list(b0=0, b1=0, b2=0, Inv_sig2_e=1)
;
RUN;
DATA _NULL_; /*Export them*/
SET INIT;
FILE "&path/RegInit.txt";
PUT initials;
RUN;
%MACRO main(n);
data RegData(rename=(cx1=x1 cx2=x2 cy=y)); /*Prepare data to be exported as a text file*/
set Sim_Reg;
where NumSimul=&n;
cx1=put(x1,7.3); /*convert variables to character format*/
cx2=put(x2,7.3);
cy=put(y,7.3);
drop x1 x2 y j NumSimul;
run;
data RegData;
length x1 $7 x2 $7 y $7;
if _n_=1 then do; /*insert variable names for OpenBUGS*/
x1 = "x1[]";
x2 = "x2[]";
y = "y[] ";
output;
end;
set RegData;
output;
run;
proc sql;
insert into RegData (x1) values ('END'); /*insert END statement at eof*/
run;
PROC EXPORT DATA=RegData /*Export data as text file to this location*/
OUTFILE="&path/RegData&n..txt"
DBMS=TAB REPLACE;
PUTNAMES=NO;
RUN;
DATA SCRIPT; /*Script file so OpenBUGS knows what to do*/
LENGTH script $80.;
SCRIPT = "modelOutput('log')" ;OUTPUT;
SCRIPT = "modelCheck('&path/RegModel.txt')" ;OUTPUT;
SCRIPT = "modelData('&path/RegData&n..txt')" ;OUTPUT;
SCRIPT = "modelCompile(1)" ;OUTPUT;
SCRIPT = "modelInits('&path/RegInit.txt',1)" ;OUTPUT;
SCRIPT = "modelGenInits()"; OUTPUT;
SCRIPT = "modelUpdate(3000)" ;OUTPUT;
SCRIPT = "samplesSet(b0)" ;OUTPUT;
SCRIPT = "samplesSet(b1)" ;OUTPUT;
SCRIPT = "samplesSet(b2)" ;OUTPUT;
SCRIPT = "samplesSet(Sig2_e)" ;OUTPUT;
SCRIPT = "modelUpdate(5000)" ;OUTPUT;
SCRIPT = "samplesStats('*')" ;OUTPUT;
SCRIPT = "modelSaveLog('&path/bugslog.txt')" ;OUTPUT;
SCRIPT = "samplesCoda('*','&path/output&n')" ;OUTPUT;
SCRIPT = "modelQuit('yes')" ;OUTPUT;
RUN;
DATA _NULL_; /*Export script file*/
SET SCRIPT;
FILE "Batch.txt";
PUT script;
RUN;
DATA _NULL_;
X "OpenBUGS /panfs/pfs.local/work/example2/Batch.txt"; /*Execute*/
RUN;
%mend;
%MACRO runmain; /*Call multiple simulations sequentially*/
%LET n=1;
%DO %WHILE(&n <=&NumSimul);
%main(&n);
%LET n=%EVAL(&n+1);
%END;
%MEND runmain;
%runmain;
... View more