%put NOTE: You have called the macro _DECODA, 2007-10-22; %put NOTE: Copyright (c) 2001-2007 Rodney Sparapani; %put; /* Author: Rodney Sparapani Created: 2001-00-00 This file is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2, or (at your option) any later version. This file is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this file; see the file COPYING. If not, write to the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. In short: you may use this file any way you like, as long as you don't charge money for it, remove this notice, or hold anyone liable for its results. */ /* _DECODA Documentation Creates a SAS dataset from CODA files and provides summaries if requested. REQUIRED Parameters INFILE= CODA index file (NAMEIndex.txt, NAME.ind or NAME.ind.txt) FILE=INFILE alias OUT= SAS dataset created Specific OPTIONAL Parameters AUTOCORR=0 auto-correlation statistics are not calculated unless AUTOCORR=1 or NLAG NE 25 (SAS/ETS required) CHAINS= the number of CODA chain files (NAME#.txt, NAME#.out or NAME#.out.txt) CHAIN#= if the CODA chain files do not follow the standard naming convention, then you must manually list them here (up to 10) CONTENTS=1 by default, PROC CONTENTS set to zero to over-ride EXP= list of variables that are logarithms base e from which to create exponentiated variables (EXP_VAR): useful for OR/RR/etc. GRAPHICS=1 if statistics are requested, then produce graphics as well (v. 8 or higher only) GSFMODE=REPLACE default GSFMODE for first graph MU0=0 default location for tests/tables NLAG=25 default of up to 25 lag auto-correlation if AUTOCORR=1 OPTIONS= options to PROC UNIVARIATE PCTLDEF=5 default percentile definition PCTLPTS=2.5 97.5 default percentiles to calcuate and present SYMBOL=i=join v=none r=1 default SYMBOL statement for trace plots STATS=VAR alias TAIL=out default file extension for CODA chain files (NAME.TAIL or NAME#.TAIL) THIN=1 default thinning parameter, set to an integer >1 for only keeping iterations where MOD(ITER, THIN)=0 TRACE=1 default to produce trace plots TYPE= if you set this parameter, then TYPE will be used to name the graphics file (VAR.TYPE) for each summarized variable, currently useful only useful for scalar variables VAR= list of variables to summarize/graph, if any use _all_ for all variables WHERE= restrict the data with a WHERE clause, useful for removing burn-in, i.e. WHERE=iter>4000 see THIN= also Common OPTIONAL Parameters LOG= set to /dev/null to turn off .log */ %macro _decoda(infile=REQUIRED, file=&infile, out=REQUIRED, autocorr=, chains=, contents=1, exp=, graphics=%_version(8), gsfmode=replace, mu0=0, nlag=25, options=, pctldef=5, pctlpts=2.5 97.5, symbol=i=join v=none r=1, stats=, tail=out, thin=1, trace=1, type=, var=&stats, where=, log=, chain1=, chain2=, chain3=, chain4=, chain5=, chain6=, chain7=, chain8=, chain9=, chain10=); %_require(&out &file); %if %length(&log) %then %_printto(log=&log); %local i j head nobs scratch temp; %let file=%scan(&file, 1, ''""); %if %length(&chains)=0 %then %let chains=%_count(&chain1 &chain2 &chain3 &chain4 &chain5 &chain6 &chain7 &chain8 &chain9 &chain10); %if &chains=0 %then %let chains=1; %do i=1 %to &chains; %let chain&i=%scan(&&chain&i, 1, ''""); %if %length(&&chain&i)=0 %then %do; %let j=%index(&file, Index.txt); %if &j %then %let head=%_substr(&file, 1, &j-1); %else %let head=%scan(&file, 1, .); %if &chains=1 %then %do; %let chain1=&head..&tail; %if %_exist(&chain1)=0 %then %do; %let chain1=&head..out; %if %_exist(&chain1)=0 %then %do; %let chain1=&head..out.txt; %if %_exist(&chain1)=0 %then %let chain1=&head..txt; %end; %end; %end; %else %do; %let chain&i=&head.&i..&tail; %if %_exist(&&chain&i)=0 %then %do; %let chain&i=&head.&i..out; %if %_exist(&&chain&i)=0 %then %do; %let chain&i=&head.&i..out.txt; %if %_exist(&&chain&i)=0 %then %do; %let chain&i=&head.&i..txt; %end; %end; %end; %end; %if %_exist(&&chain&i)=0 %then %do; %put ERROR: Chain file(s) not found, specify manually by CHAIN1=, etc.; %_abend; %end; %end; %local data&i; %if &chains=1 %then %let data1=&out; %else %let data&i=%_scratch; data &&data&i; infile "&&chain&i" dlm='090D20'x; input iter value; run; %let nobs=%_nobs(data=&&data&i); data &&data&i; length var label index1 index2 $ 32; drop start stop index1 index2 prev; infile "&file" dlm='090D20'x missover; input var start stop; retain chain &i; obs=index(var, "["); if obs then do; index1=scan(var, 2, "[,]"); index2=scan(var, 3, "[,]"); var=scan(var, 1, "[,]"); obs=index1; if index2>'' then do; label=trim(var)||'[obs,'||trim(index2)||']'; var=trim(var)||index2; end; else label=trim(var)||'[obs]'; end; var=lowcase(translate(var, '_', '.')); if stop>&nobs then goto error; else do row=start to stop; prev=value; set &&data&i point=row; /* if row=start then weight=1; else weight=not(fuzz(value-prev)=0); */ %if &thin>1 %then if mod(iter, &thin)=0 then; output; end; return; error: put 'ERROR: POINT > NOBS ' start= stop=; stop; run; proc sort data=&&data&i out=&&data&i; where &where; by obs iter; run; %end; %if &chains>1 %then %do; data &out; set %do i=1 %to &chains; &&data&i %end;; by obs chain iter; * weight; run; %end; proc transpose data=&out out=&out(drop=_name_ sortedby=obs chain iter %if %length(&exp)=0 %then index=(ci=(chain iter)); ); by obs chain iter; * weight; var value; id var; idlabel label; run; %if %length(&exp) %then %do; data &out(sortedby=obs chain iter index=(ci=(chain iter))); set &out; %do i=1 %to %_count(&exp); %let temp=%scan(&exp, &i, %str( )); if n(&temp) then exp_&temp=exp(&temp); %end; run; %end; %if &contents %then %do; proc contents; run; %end; %if %length(&var) %then %do; %_debugs(data=&out, var=&var, autocorr=&autocorr, graphics=&graphics, gsfmode=&gsfmode, mu0=&mu0, nlag=&nlag, options=&options, pctldef=&pctldef, pctlpts=&pctlpts, symbol=&symbol, trace=&trace, type=&type, log=&log); %let syslast=&out; %end; %if %length(&log) %then %_printto; %mend _decoda; %*VALIDATION TEST STREAM; /* un-comment to re-validate %_decoda(out=mydata, chains=2, infile=head.txt); */