**Create dataset of indicator and amount variables;
%macro process_variables(pre_mcmc_data=,
												 episodic_variables=,
												 episodic_indicators=,
												 episodic_amounts=,
												 daily_variables=,
												 daily_amounts=,
												 never_consumer_variable=,
												 never_consumer_indicator=,
												 never_consumer_amount=,
												 outname=);
															
	%local is_nci_multivar 
	       num_episodic num_daily
	       i variable
				 has_never_consumers;

	data _NULL_;
		_dsid = open("&pre_mcmc_data._mcmc_in");
		_check = (varnum(_dsid, "_nci_multivar") ^= 0);
		
		call symputx("is_nci_multivar", _check);
	run;
	
	%if &is_nci_multivar. = 1 %then %do;
	
		**if the input data is from the NCI multivar preprocessor, fill in indicators and amounts using appropriate prefixes;
		%if &episodic_indicators. = %str() and &episodic_variables. ^= %str() %then %do;
		
			%let num_episodic = %sysfunc(countw(&episodic_variables., %str( )));
			%do i = 1 %to &num_episodic.;
			
				%let episodic_indicators = &episodic_indicators. ind_%sysfunc(scan(&episodic_variables., &i., %str( )));
			%end;
		%end;
		
		%if &episodic_amounts. = %str() and &episodic_variables. ^= %str() %then %do;
		
			%let num_episodic = %sysfunc(countw(&episodic_variables., %str( )));
			%do i = 1 %to &num_episodic.;
			
				%let episodic_amounts = &episodic_amounts. amt_%sysfunc(scan(&episodic_variables., &i., %str( )));
			%end;
		%end;
		
		%if &daily_amounts. = %str() and &daily_variables. ^= %str() %then %do;
		
			%let num_daily = %sysfunc(countw(&daily_variables., %str( )));
			%do i = 1 %to &num_daily.;
			
				%let daily_amounts = &daily_amounts. amt_%sysfunc(scan(&daily_variables, &i., %str( )));
			%end;
		%end;
		
		%if &never_consumer_indicator. = %str() and &never_consumer_variable. ^= %str() %then %do;
		
			%let never_consumer_indicator = ind_&never_consumer_variable.;
		%end;
		
		%if &never_consumer_amount. = %str() and &never_consumer_variable. ^= %str() %then %do;
		
			%let never_consumer_amount = amt_&never_consumer_variable.;
		%end;
	%end;
	
	**if raw variables are not provided, match amounts to the backtransformation variables;
	%if &episodic_amounts. ^= %str() and &episodic_variables. = %str() %then %do;
	
		%let num_episodic = %sysfunc(countw(&episodic_amounts., %str( )));
		%do i = 1 %to &num_episodic.;
		
			%let amount = %sysfunc(scan(&episodic_amounts., &i., %str( )));
		
			data _NULL_;
				set &pre_mcmc_data._backtran;
				
				if find("&amount.", variable, "i") ^= 0 then call symputx("variable", variable);
			run;
			
			%let episodic_variables = &episodic_variables. &variable.;
		%end;
	%end;
	
	%if &daily_amounts. ^= %str() and &daily_variables. = %str() %then %do;
	
		%let num_daily = %sysfunc(countw(&daily_amounts., %str( )));
		%do i = 1 %to &num_daily.;
		
			%let amount = %sysfunc(scan(&daily_amounts., &i., %str( )));
			
			data _NULL_;
				set &pre_mcmc_data._backtran;
				
				if find("&amount.", variable, "i") ^= 0 then call symputx("variable", variable);
			run;
			
			%let daily_variable = &daily_variables. &variable.;
		%end;
	%end;
	
	%if &never_consumer_amount. ^= %str() and &never_consumer_variable. = %str() %then %do;
	
		data _NULL_;
			set &pre_mcmc_data._backtran;
				
			if find("&never_consumer_amount.", variable, "i") ^= 0 then call symputx("never_consumer_variable", variable);
		run;
	%end;
	
	%if &never_consumer_variable. ^= %str() %then %do;
	
		%let has_never_consumers = 1;
	%end;
	%else %do;
	
		%let has_never_consumers = 0;
	%end;
	
	**Convert variable lists to uppercase;
	%let episodic_variables = %upcase(&episodic_variables.);
	%let episodic_indicators = %upcase(&episodic_indicators.);
	%let episodic_amounts = %upcase(&episodic_amounts.);
	
	%let daily_variables = %upcase(&daily_variables.);
	%let daily_amounts = %upcase(&daily_amounts.);
	
	%let never_consumer_variable = %upcase(&never_consumer_variable.);
	%let never_consumer_indicator = %upcase(&never_consumer_indicator.);
	%let never_consumer_amount = %upcase(&never_consumer_amount.);
	
	**place never consumer variable first in episodic variable list as required by the MCMC algorithm;
	%if &has_never_consumers. = 1 %then %do;
	
		%let episodic_variables = &never_consumer_variable. %sysfunc(transtrn(&episodic_variables., &never_consumer_variable., %str()));
		%let episodic_indicators = &never_consumer_indicator. %sysfunc(transtrn(&episodic_indicators., &never_consumer_indicator., %str()));
		%let episodic_amounts = &never_consumer_amount. %sysfunc(transtrn(&episodic_amounts., &never_consumer_amount., %str()));
	%end;
	
	**Output processed variable lists in dataset;	
	data &outname._vars;
	
		%do i = 1 %to %sysfunc(countw(&episodic_variables., %str( )));
		
			episodic_var&i. = "%sysfunc(scan(&episodic_variables., &i., %str( )))";
			episodic_ind&i. = "%sysfunc(scan(&episodic_indicators., &i., %str( )))";
			episodic_amt&i. = "%sysfunc(scan(&episodic_amounts., &i., %str( )))";
		%end;
		num_episodic = %sysfunc(countw(&episodic_variables., %str( )));
		
		%do i = 1 %to %sysfunc(countw(&daily_variables., %str( )));
		
			daily_var&i. = "%sysfunc(scan(&daily_variables., &i., %str( )))";
			daily_amt&i. = "%sysfunc(scan(&daily_amounts., &i., %str( )))";
		%end;
		num_daily = %sysfunc(countw(&daily_variables., %str( )));
		
		num_variables = 2*num_episodic + num_daily;
		
		has_never_consumers = &has_never_consumers.;
	run;		
%mend process_variables;

**Create datasets of covariates for each variable;
%macro process_covariates(variable_data=,
												 	default_covariates=,
												 	default_intercept=,
												 	episodic_indicator_covariates=,
												 	episodic_indicator_intercept=,
												 	episodic_amount_covariates=,
												 	episodic_amount_intercept=,
												 	daily_amount_covariates=,
												 	daily_amount_intercept=,
												 	individual_covariates=,
												 	individual_intercept=,
												 	never_consumer_covariates=,
												 	never_consumer_intercept=,
												 	outname=);
												 			 
	%local model_variables episodic_indicators episodic_amounts daily_amounts
				 num_variables num_episodic num_daily 
				 i var_j variable covariates intercept
				 has_never_consumers;

	**extract variables;
	data _NULL_;
		set &variable_data.;
		
		num_variables = 2*num_episodic + num_daily;
		
		call symputx("num_episodic", num_episodic);
		call symputx("num_daily", num_daily);
		call symputx("num_variables", num_variables);
		
		call symputx("has_never_consumers", has_never_consumers);
	run;
	
	data _NULL_;
		set &variable_data.;
		
		length episodic_indicators episodic_amounts daily_amounts $32767.;
		length model_variables $32767.;
		
		%do i = 1 %to &num_episodic.;
			episodic_indicators = catx(" ", episodic_indicators, episodic_ind&i.);
			episodic_amounts = catx(" ", episodic_amounts, episodic_amt&i.);
			
			model_variables = catx(" ", model_variables, episodic_ind&i., episodic_amt&i.);
		%end;
		
		%do i = 1 %to &num_daily.;
			daily_amounts = catx(" ", daily_amounts, daily_amt&i.);
			
			model_variables = catx(" ", model_variables, daily_amt&i.);
		%end;
		
		call symputx("episodic_indicators", episodic_indicators);
		call symputx("episodic_amounts", episodic_amounts);
		call symputx("daily_amounts", daily_amounts);
		
		call symputx("model_variables", model_variables);
	run;	
	
	**Create dataset of covariates for each variable;	
	%do var_j = 1 %to &num_variables.;
	
		%let variable = %sysfunc(scan(&model_variables., &var_j., %str( )));
		
		%let covariates = &default_covariates.;
		
		%if %sysfunc(findw(&episodic_indicators., &variable., , %str(s))) ^= 0 and &episodic_indicator_covariates. ^= %str() %then %do;
			
			%let covariates = &episodic_indicator_covariates.;
		%end;
		
		%if %sysfunc(findw(&episodic_amounts., &variable., , %str(s))) ^= 0 and &episodic_amount_covariates. ^= %str() %then %do;
			
			%let covariates = &episodic_amount_covariates.;
		%end;
		
		%if %sysfunc(findw(&daily_amounts., &variable., , %str(s))) ^= 0 and &daily_amount_covariates. ^= %str() %then %do;
			
			%let covariates = &daily_amount_covariates.;
		%end;
		
		%if &individual_covariates. ^= %str() %then %do;
		
			data _NULL_;
				set &individual_covariates.;
				
				if compare(variable, "&variable.", "i") = 0 then call symputx("covariates", covariates);
			run;
		%end;
		
		%let intercept = &default_intercept.;

		%if %sysfunc(findw(&episodic_indicators., &variable., , %str(s))) ^= 0 and &episodic_indicator_intercept. ^= %str() %then %do;
		
			%let intercept = &episodic_indicator_intercept.;
		%end;
		
		%if %sysfunc(findw(&episodic_amounts., &variable., , %str(s))) ^= 0 and &episodic_amount_intercept. ^= %str() %then %do;
		
			%let intercept = &episodic_amount_intercept.;
		%end;
		
		%if %sysfunc(findw(&daily_amounts., &variable., , %str(s))) ^= 0 and &daily_amount_intercept. ^= %str() %then %do;
		
			%let intercept = &daily_amount_intercept.;
		%end;
		
		%if &individual_intercept. ^= %str() %then %do;
		
			data _NULL_;
				set &individual_intercept.;
				
				if compare(variable, "&variable.", "i") = 0 then call symputx("intercept", intercept);
			run;
		%end;
		
		data _covars&var_j.;
		
			length variable $32.;
			variable = "&variable.";
		
			%do i = 1 %to %sysfunc(countw(&covariates., %str( )));
			
				length covariate&i. $32.;
				covariate&i. = "%sysfunc(scan(&covariates., &i., %str( )))";
			%end;
			
			if upcase("&intercept.") = "Y" then intercept = 1;
			else intercept = 0;
			
			num_covariates = %sysfunc(countw(&covariates., %str( )));
			num_coefficients = num_covariates + intercept;
		run;
	%end;
	
	data &outname._covars;
		set _covars1-_covars&num_variables.;
	run;
	
	%if &has_never_consumers. = 1 %then %do;
	
		**Never-consumer covariates;
		data &outname._covarsnc;
		
			%do i = 1 %to %sysfunc(countw(&never_consumer_covariates., %str( )));
			
				length covariate&i. $32.;
				covariate&i. = "%sysfunc(scan(&never_consumer_covariates., &i., %str( )))";
			%end;
			
			if upcase("&never_consumer_intercept.") = "Y" then intercept = 1;
			else intercept = 0;
			
			num_covariates = %sysfunc(countw(&never_consumer_covariates., %str( )));
			num_coefficients = num_covariates + intercept;
		run;
	%end;
%mend process_covariates;