Published on :
Statistical CREATION_INTERNE

Example Documentation 1 for PROC BCHOICE

This code is also available in: Deutsch Español Français
Awaiting validation
The script first creates a 'Travel' dataset from raw data to record travel times for different modes of transport (Auto, Plane, Transit), respondent's age, and the chosen mode. It then transforms this dataset into a 'long' format ('Travel2') suitable for analysis by PROC BCHOICE, where each observation represents a choice alternative for a given subject. Finally, it executes two BCHOICE models: the first evaluates the effects of transport mode and travel time, and the second introduces an interaction between mode and centered age to explore how age influences mode choice.
Data Analysis

Type : CREATION_INTERNE


The data is created directly within the script via a DATALINES block for the 'Travel' dataset. The 'Travel2' dataset is then created from 'Travel' by a transformation DATA STEP.

1 Code Block
DATA STEP Data
Explanation :
This code block creates the 'Travel' dataset from raw data provided via `datalines`. It defines the variables `AutoTime`, `PlanTime`, `TranTime` (travel times for car, plane, and transit respectively), `Age` of the individual, and `Chosen` (the transport mode actually chosen). A new variable, `AgeCtr` (centered age), is calculated by subtracting 34 from `Age`, which is useful for interpreting interaction models.
Copied!
1DATA Travel;
2 INPUT AutoTime PlanTime TranTime Age Chosen $;
3 AgeCtr=Age-34;
4 DATALINES;
510.0 4.5 10.5 32 Plane
65.5 4.0 7.5 13 Auto
74.5 6.0 5.5 41 Transit
83.5 2.0 5.0 41 Transit
91.5 4.5 4.0 47 Auto
1010.5 3.0 10.5 24 Plane
117.0 3.0 9.0 27 Auto
129.0 3.5 9.0 21 Plane
134.0 5.0 5.5 23 Auto
1422.0 4.5 22.5 30 Plane
157.5 5.5 10.0 58 Plane
1611.5 3.5 11.5 36 Transit
173.5 4.5 4.5 43 Auto
1812.0 3.0 11.0 33 Plane
1918.0 5.5 20.0 30 Plane
2023.0 5.5 21.5 28 Plane
214.0 3.0 4.5 44 Plane
225.0 2.5 7.0 37 Transit
233.5 2.0 7.0 45 Auto
2412.5 3.5 15.5 35 Plane
251.5 4.0 2.0 22 Auto
26;
2 Code Block
DATA STEP Data
Explanation :
This block transforms the 'Travel' dataset from 'wide' format (one row per individual, multiple columns for alternatives) to 'long' format required by PROC BCHOICE (one row per choice alternative for each individual). For each observation in the original dataset, it generates three new observations. `Subject` identifies the individual (`_n_` is the DATA STEP iteration number), `Mode` is the potential transport mode, `TravTime` is the corresponding travel time, and `Choice` is a binary variable indicating whether this mode was actually chosen by the individual.
Copied!
1DATA Travel2(keep=Subject Mode TravTime Age AgeCtr Choice);
2 array Times[3] AutoTime PlanTime TranTime;
3 array Allmodes[3] $ _temporary_ ('Auto' 'Plane' 'Transit');
4 SET Travel;
5 Subject = _n_;
6 DO i = 1 to 3;
7 Mode = Allmodes[i];
8 TravTime = Times[i];
9 Choice = (Chosen eq Mode);
10 OUTPUT;
11 END;
12RUN;
3 Code Block
PROC PRINT
Explanation :
This procedure displays the first 20 observations of the transformed 'Travel2' dataset. The `by Subject` option ensures that observations for each subject are grouped, and `id Subject` uses the `Subject` variable as the primary identifier in the output, improving readability.
Copied!
1PROC PRINT DATA=Travel2 (obs=20);
2 BY Subject;
3 id Subject;
4RUN;
4 Code Block
PROC BCHOICE
Explanation :
This block executes the first discrete choice analysis using PROC BCHOICE. It models the probability of `Choice` (binary variable indicating choice) as a function of `Mode` of transport and `TravTime` (travel time). `Mode` and `Subject` are declared as classification variables, `param=ref` uses the last category as a reference, and `choiceset=(Subject)` indicates that choices are grouped by subject for model estimation. The `seed=124` option ensures reproducibility of stochastic results.
Copied!
1PROC BCHOICE DATA=Travel2 seed=124;
2 class Mode Subject / param=ref order=DATA;
3 model Choice = Mode TravTime / choiceset=(Subject);
4RUN;
5 Code Block
PROC BCHOICE
Explanation :
This second BCHOICE model is similar to the first but includes a `Mode*AgeCtr` interaction term. This interaction allows exploring how the effect of transport mode on choice varies with the individual's centered age, thus adding an individual-specific component to the choice analysis. `Mode` and `Subject` remain classification variables, and `seed=124` is again used for reproducibility.
Copied!
1PROC BCHOICE DATA=Travel2 seed=124;
2 class Mode Subject / param=ref order=DATA;
3 model Choice = Mode Mode*AgeCtr TravTime / choiceset=(Subject);
4RUN;
This material is provided "as is" by We Are Cas. There are no warranties, expressed or implied, as to merchantability or fitness for a particular purpose regarding the materials or code contained herein. We Are Cas is not responsible for errors in this material as it now exists or will exist, nor does We Are Cas provide technical support for it.
Copyright Info : S A S S A M P L E L I B R A R Y