# 6 SAS/STAT Bayesian Analysis Procedures You Must Know

Expert-led Courses: Transform Your Career â€“ Enroll Now

We looked at SAS ANOVA (analysis of variance) in the previous tutorial, today we will be looking at SAS/STAT Bayesian Analysis Procedure. Moreover, we will see how Bayesian Analysis Procedure is used in SAS/STAT for computing different models. Our focus here will be to understand different procedures that can be used for Bayesian analysis through the use of examples.
So, let’s start SAS/STAT Bayesian Analysis Procedure.

### SAS/STAT Bayesian Analysis

SAS/ STAT Bayesian analysis is a statistical procedure that helps us in answering research questions about unknown parameters using probability statements.

Bayesian Analysis example- what is the probability that the average female height is between 60 and 70 inches? What is the probability that people in a particular state vote for Congress or vote BJP? What is the probability that treatment A is more cost-effective than treatment B for a specific health care provider?
Let’s Learn SAS/STAT Software Features

These statements are very common in the SAS/STAT Bayesian analysis because of the underlying assumption that all parameters are random quantities. In a SAS/STAT Bayesian analysis, a parameter is summarized by an entire distribution of values instead of one fixed value.

A posterior distribution comprises a prior distribution of a parameter and a likelihood model providing information about the parameter based on observed data.

SAS STAT bayesian analysis

### Calculating Bayesian Analysis in SAS/STAT

SAS/STAT Software uses the following procedures to compute Bayesian analysis of a sample data. Each procedure has a different syntax and is used with different type of data in different contexts. Let us explore each one of these.

How to Calculate Bayesian Analysis in SAS/STAT

#### a. SAS PROC BCHOICE

The PROC BCHOICE procedure in SAS/STAT (Bayesian choice) procedure performs Bayesian analysis for discrete choice models. Discrete choice models are derived under the assumption of utility-maximizing behavior by decision makers. When individuals are asked to make one choice among a set of alternatives, they usually determine the level of utility that each alternative offers.
SAS PROC BCHOICE Syntax-

```PROC BCHOICEÂ DATASET;
CLASS variable;
MODEL response <(response-options)>Â =Â <fixed-effects> </ model-options>;```

SAS PROC BCHOICE Example-

```data Chocolates;
Â Â  input Subj Choice brown white choco_chips;
Â Â  datalines;
1 0 0 0 0
1 0 0 0 1
1 0 0 1 0
2 0 0 0 0
2 0 0 0 1
2 0 0 1 0
2 0 0 1 1
3 0 0 0 0
3 0 0 0 1
4 0 0 0 0
4 0 0 0 1
4 0 1 0 1
5 0 0 1 0
5 0 0 1 1
5 0 1 1 1
6 0 0 0 0
6 0 0 0 1
7 0 0 0 0
7 0 1 1 0
7 0 1 1 1
8 0 0 0 0
8 0 0 0 1
8 0 0 1 0
8 0 0 1 1
8 0 1 0 0
8 1 1 0 1
8 0 1 1 0
8 0 1 1 1
9 0 0 0 0
9 0 0 0 1
9 0 0 1 0
9 0 0 1 1
9 0 1 0 0
9 1 1 0 1
9 0 1 1 0
9 0 1 1 1
10 0 0 0 0
10 0 0 0 1
;
proc print data=Chocolates (obs=16);
run;
ods graphics on;
proc bchoice data=Chocolates;
Â Â  class brown(ref='0') white(ref='0') choco_chips(ref='0') Subj;
Â Â  model Choice = brown white choco_chips / choiceset=(Subj) cprior=normal(var=1000);
run;```

Let’s Revise SAS ODS (Output Delivery Systems)
The PROC BCHOICE and MODEL statements are required statements.Â Â

SAS STAT Bayesian Analysis – SAS PROC BCHOICE

SAS STAT Bayesian Analysis –Â  –Â SAS PROC BCHOICE

SAS STAT Bayesian Analysis – SAS PROC BCHOICE

SAS STAT Bayesian Analysis –Â SAS PROC BCHOICE

SAS STAT Bayesian Analysis –Â SAS PROC BCHOICE

SAS STAT Bayesian Analysis –Â SAS PROC BCHOICE

#### b. SAS PROC FMM

The PROC FMM procedure in SAS/STAT Software fits statistical models to data for which the distribution of the response is a finite mixture of distributionsâ€”that is, each response is drawn with unknown probability from one of several distributions.
Let’s Learn SAS Frequency Distribution Using SAS PROC FREQ
SAS PROC FMMÂ Syntax-

```PROC FMM dataset;
model <VARIABLES>;
output DATASET;```

Â
The PROC FMM and MODEL statements are required.
The MODEL statement defines elements of the mixture model, such as the model effects, the distribution, and the link function.
The OUTPUT statement creates a data set that contains observations statistics that are computed after fitting the model.Â By default, all variables in the original data set are included in the output data set.
In the below example, the dist= option specifies the kind of distribution we want. Because our variable is a continuous one, we have taken normal distribution here.
SAS PROC FMM Example-

```proc fmm data=sashelp.class;
model age=/ dist=normal;
output out= class class=ML;
RUN;```

Let’s Discuss SAS Arithmetic Mean â€“ SAS PROC MEANS Tutorial

SAS STAT Bayesian Analysis –Â SAS PROC FMM

SAS STAT Bayesian Analysis –Â SAS PROC FMM

SAS STAT Bayesian Analysis –Â SAS PROC FMM

#### c. SAS PROC GENMOD

The PROC GENMOD provides Bayesian analysis for distributions like binomial, gamma, Gaussian, normal and Poisson. It also provides Bayesian analysis for links like identity, log, logit, probit etc. In a Bayesian analysis, the model parameters are treated as random variables, and inference about parameters is based on the posterior distribution of the parameters, given the data.
SAS Proc GENMOD Syntax-

```PROC GENMOD dataset;
Â Â Â Â  model <dependent variable>;
bayes <options>;```

Here, MODEL statement signifies the dependent and the independent variable. In the below example, height is the dependent variable and age is the independent variable.
The Bayes statement signifies that we are performing a Bayesian analysis in SAS/STAT.
SAS Proc FMM Example-

```proc genmod data=sashelp.class;
model height=age / dist=normal;
bayes outpost=class;
run;```

Here the outpost = option saves samples (posterior) to the POST dataset.
DO You Know How to Apply Fishers Exact Test in SAS Using PROC FREQ Procedure
The first table produced in the output provides some usual classical inferences, but all the subsequent tables provide Bayesian analysis.
In the above example, the dist= option specifies the kind of distribution we want. Because our variable is a continuous one, we have taken normal distribution here.
The GENMOD procedure also uses ODS Graphics to create graphs as part of its output.Â

SAS STAT Bayesian Analysis –Â SAS PROC GENMOD

SAS STAT Bayesian Analysis –Â SAS PROC GENMOD

SAS STAT Bayesian Analysis –Â SAS PROC GENMOD

SAS STAT Bayesian Analysis –Â SAS PROC GENMOD

SAS STAT Bayesian Analysis –Â SAS PROC GENMOD

SAS STAT Bayesian Analysis –Â SAS PROC GENMOD

Let’s ExploreÂ How to Join/Combine Data Sets in SAS

#### d. SAS PROC LIFEREG

The PROC LIFEREG procedure in SAS/STAT fits parametric models to data that can be uncensored, right censored, left censored, or interval censored.Â The models for the response variable consist of a linear effect composed of the covariates and a random disturbance term.

Bayesian analysis of parametric survival models can be requested by using the BAYES statement in the LIFEREG procedure.
SAS Proc LIFEREG Syntax-

```PROC LIFEREG dataset;
model VARIABLE;```

Example-

```data one;
input y x cen;
cards;
12Â  2Â  0
12Â  3Â  1
14Â  1Â  1
67Â  3Â  1
43Â  2Â  1
78Â  1Â  1
;
ods graphics on;
proc lifereg data=one;
model y= / dist=exponential;
run;```

Let’s Discuss Types of Software in SAS Programming language

SAS STAT Bayesian Analysis –Â SAS PROC LIFEREG

SAS STAT Bayesian Analysis –Â SAS PROC LIFEREG

Let’s revise SAS Environment Setup

#### e. SAS PROC MCMC

The MCMC procedure is a general purpose simulation procedure that uses Markov chain Monte Carlo (MCMC) techniques to fit Bayesian models.

PROC MCMC draws samples from a random posterior distribution (posteriorÂ probabilityÂ distributionÂ is the probabilityÂ distributionÂ of an unknown quantity, treated as a random variable, conditional on the evidence obtained from an experiment or survey), and uses these samples to approximate the data distribution.

You need to specify only parameters, prior distributions, and a likelihood function.
SAS PROC MCMC Syntax-

```PROC MCMC dataset;
Â Â  PARMS <list of parameters>;
Â Â  PRIOR <type of distribution of each parameter>;
Â Â  MODEL <variable used as likelihood>;```

Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â
SAS PROC MCMCÂ Example-

```ods graphics on;
proc mcmc data=sashelp.class ;
Â Â  parms b0 0 b1 0;
Â Â  parms sigma2 1;
Â Â  prior b0 b1 ~ normal(mean = 5, var = 1e6);
Â Â  prior sigma2 ~ igamma(shape = 2/10, scale = 10/4);
Â Â  mu = b0 + b1*height;
Â Â  model height ~ n(mu, var = sigma2);
run;```

Â
Here, height is a likelihood function specified in the model, ~ sign indicates that we want to specify a distribution for our data. Here likelihood is a normal distribution with mean (mu) and variance (sigma2).
The b0, b1 and sigma2 are parameters used with a parms statement. These are names given by us.
The MODEL statement also automatically compensates missing data.
The PROC MCMC also supports univariate or multivariate distributions.

SAS STAT Bayesian Procedure –Â SAS PROC MCMC

SAS STAT Bayesian Procedure –Â SAS PROC MCMC

SAS STAT Bayesian Procedure –Â SAS PROC MCMC

SAS STAT Bayesian Procedure –Â SAS PROC MCMC

Related topic- SAS Functions â€“ Arithmetic, Character, Date and Time, Truncation

#### f. SAS PROC PHREG

The PROC PHREG procedure in SAS/STAT performs survival analysis of data. Many types of models have been used for survival data. Two of the more popular types of models are the accelerated failure time model (Kalbfleisch and PrenticeÂ 1980) and the Cox proportional hazards model (CoxÂ 1972). The PHREG procedure performs a regression analysis of survival data based on the Cox proportional hazards model.
SAS PROC PHREG Syntax-

```PROC PHREG Â dataset;
Â Â  model response <*censor(list)>Â =Â <effects> </ options>;
Â  bayes </ options>;```

The PROC PHREG and MODEL statements are required statements.
SAS PROC PHREGÂ Example-

```ods graphics on;
proc phreg data=sashelp.cars ;
Â Â  model horsepower*length(0) = cylinders;
Â  bayes outpost=cars;
run;```

By using ODS Graphics, PROC PHREG allows you to plot the survival curve for CYLINERS GROUP.
The BAYES statement invokes the Bayesian analysis.
the OUTPOST= option saves the posterior distribution samples in a SAS data set for post-processing.
The “Model Information” table below summarizes information about the model you fit and the size of the simulation.
Do You Know How to Enter and Read Raw Data in SAS
So, this was allÂ About SAS/STATÂ Bayesian Analysis Procedure Tutorial. Hope you like our explanation.

SAS STAT Bayesian Procedure –Â SAS PROC PHREG

SAS STAT Bayesian Procedure –Â SAS PROC PHREG

SAS STAT BayesianÂ Procedure –Â SAS PROC PHREG

### Conclusion

Hence, this was a complete description and a comprehensive understanding of all the procedure offered by SAS/STAT Bayesian Analysis. We looked at each of them:Â PROC PHREG, PROC MCMC, PROC LIFEREG, PROC GENMOD, PROC FMM, and PROC BCHOICE with their syntax, and how they can be used.

Hope you all enjoyed it. Stay tuned for more.Â Furthermore, if you have any query feel free to ask in a comment section.
Related Topic-Â Simple, Stacked & Clustered Bar Charts in SAS
For referenceÂ

Did you know we work 24x7 to provide you best tutorials