See the SAS macro for storing data in WinBUGS' list format and an
example of how to use the macro.
------------------------------------------------------------
SAS macro
------------------------------------------------------------
%macro sas2bug2(data= ,
file= ,
group=,
keep= ,
lowcase=1
);
%local numobs varlist;
/*check the arguments*/
/*-------------------*/
%if %length(&data)=0 or %length(&file)=0 or %length(&group)=0 %then %
do;
%put;
%put ERROR: One or more of the required arguments (DATA=,
GROUP= or FILE=) are not given;
%put ERROR: Macro SAS2BUG2: aborting execution;
%put;
%goto endmac;
%end;
/* create filenames */
/*------------------*/
filename temp "&file";
/* create format for missing values */
/*----------------------------------*/
PROC FORMAT;
VALUE MISSVAL
. = " NA"
other=[best10.];
run;
/* create working data set */
/*-------------------------*/
data _sas2bug;
%if %length(&keep)=0 %then %do;
set &data;
%end;
%else %do;
set &data (keep = &group &keep);
%end;
format _all_ missval.;
run;
/* get the variable names */
/*------------------------*/
proc contents data=_sas2bug (drop=&group) out=_s2bcon (keep=name)
noprint;
run;
/* get number of subjects */
/*------------------------*/
proc sql;
create table _subj as
select distinct &group
from _sas2bug;
quit;
%let numobs=0;
data _null_;
if 0 then set _subj nobs=n;
call symput("numobs",left(put(n, best.)));
stop;
run;
/* create list of variables in macro variabele */
/*---------------------------------------------*/
%let varlist=;
data temp;
set _s2bcon;
length varlist $ 200;
retain varlist;
varlist=trim(left(varlist))||' '||compress(name);
run;
data _null_;
set temp;
call symput("varlist",varlist);
run;
proc sql;
drop table temp;
quit;
/* create heading with number of observations and variables names */
/*----------------------------------------------------------------*/
data _null_;
file temp;
put "list(N=%trim(&numobs),";
run;
/* output all observations for the different variables */
/*-----------------------------------------------------*/
%let i=1;
%let var=%scan(&varlist, &i, %str( ));
%do %while(%length(&var) > 0);
data _null_;
set _sas2bug (keep=&var) end=last;
file temp mod;
if _N_=1 then do;
%if &lowcase=1 %then %do; /* if lowcase */
put "%lowcase(&var)=c(" @;
%end; /*endif lowcase */
%else %do; /* if uppercase */
put "%upcase(&var)=c(" @;
%end; /* endif uppercase */
end;
put &var @;
if last then do;
put '),';
end;
else do;
put "," @;
end;
%let i=%eval(&i+1);
%let var=%scan(&varlist, &i, %str( ));
%end;
data _null_;
set _sas2bug (keep=&group) end=last;
by &group;
file temp mod;
_group_=_N_;
if _N_=1 then do;
put "&group=c(" @;
end;
if first.&group then do;
put _group_ @;
end;
if last then do;
put _group_ @;
put '))';
end;
else do;
if first.&group then put "," @;
end;
run;
/* drop temporary data sets */
/*--------------------------*/
proc sql;
drop table _sas2bug, _s2bcon;
quit;
/* end of macro */
/*--------------*/
%endmac:
%mend sas2bug2;
------------------------------------------------------------
SAS Example
------------------------------------------------------------
data test;
input obs i j x1 x2 x3 y;
cards;
1 1 1 0.35958 0.67927 0.25876 1
2 1 2 0.69103 0.16982 0.60427 0
3 1 3 0.95370 0.13868 0.96908 0
4 1 4 0.15781 0.78085 0.05155 1
5 1 5 0.82808 0.12394 0.97629 0
6 1 6 0.20235 0.56308 0.95577 0
7 1 7 0.31491 0.69448 0.03356 1
8 1 8 0.53636 0.15497 0.58343 0
9 1 9 0.04142 0.22935 0.21585 1
10 1 10 0.35005 0.60405 0.11989 1
11 2 1 0.56238 0.34140 0.78791 0
12 2 2 0.07582 0.29221 0.03031 1
13 2 3 0.39848 0.39655 0.68774 0
14 2 4 0.49622 0.48042 0.38128 1
15 2 5 0.53194 0.21879 0.87443 0
16 2 6 0.85476 0.30305 0.12782 1
17 2 7 0.01700 0.98871 0.72957 1
18 2 8 0.77931 0.56579 0.31921 1
19 2 9 0.82345 0.19629 0.52417 0
20 2 10 0.15123 0.76986 0.94503 0
run;
/* you need to sort by id */
proc sort data=test; by obs; run;
%sas2bug2(data=test,
file='s:\temp\test.txt',
group=obs,
keep=i j x1 x2 x3 y,
lowcase=1);
/* delete 'id', here = 'obs' from "test.txt" */
Regards,
Samuel, K.U.Leuven
Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm
-------------------------------------------------------------------
This list is for discussion of modelling issues and the BUGS software.
For help with crashes and error messages, first mail [log in to unmask]
To mail the BUGS list, mail to [log in to unmask]
Before mailing, please check the archive at www.jiscmail.ac.uk/lists/bugs.html
Please do not mail attachments to the list.
To leave the BUGS list, send LEAVE BUGS to [log in to unmask]
If this fails, mail [log in to unmask], NOT the whole list
|