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.
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!
data Travel2(keep=Subject Mode TravTime Age AgeCtr Choice);
array Times[3] AutoTime PlanTime TranTime;
array Allmodes[3] $ _temporary_ ('Auto' 'Plane' 'Transit');
set Travel;
Subject = _n_;
do i = 1 to 3;
Mode = Allmodes[i];
TravTime = Times[i];
Choice = (Chosen eq Mode);
output;
end;
run;
1
DATA Travel2(keep=Subject Mode TravTime Age AgeCtr Choice);
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!
proc print data=Travel2 (obs=20);
by Subject;
id Subject;
run;
1
PROC PRINTDATA=Travel2 (obs=20);
2
BY Subject;
3
id Subject;
4
RUN;
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!
proc bchoice data=Travel2 seed=124;
class Mode Subject / param=ref order=data;
model Choice = Mode TravTime / choiceset=(Subject);
run;
1
PROC BCHOICEDATA=Travel2 seed=124;
2
class Mode Subject / param=ref order=DATA;
3
model Choice = Mode TravTime / choiceset=(Subject);
4
RUN;
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!
proc bchoice data=Travel2 seed=124;
class Mode Subject / param=ref order=data;
model Choice = Mode Mode*AgeCtr TravTime / choiceset=(Subject);
run;
1
PROC BCHOICEDATA=Travel2 seed=124;
2
class Mode Subject / param=ref order=DATA;
3
model Choice = Mode Mode*AgeCtr TravTime / choiceset=(Subject);
4
RUN;
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.
SAS and all other SAS Institute Inc. product or service names are registered trademarks or trademarks of SAS Institute Inc. in the USA and other countries. ® indicates USA registration. WeAreCAS is an independent community site and is not affiliated with SAS Institute Inc.
This site uses technical and analytical cookies to improve your experience.
Read more.